]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/cmd/urlencode.c
cc, ?[acl]: fix gethunk() and move common memory allocator code to cc/compat
[plan9front.git] / sys / src / cmd / urlencode.c
index 561ca91574e4789ae1e2ed6e4af90fbcc678860e..92488584e337f68382058bfd4a1f71941e7d6aa2 100644 (file)
@@ -14,7 +14,7 @@ hexdigit(int c)
 {
        char *p;
 
-       if(c >= 0){
+       if(c > 0){
                if((p = strchr(Hex, c)) != 0)
                        return p - Hex;
                if((p = strchr(hex, c)) != 0)
@@ -26,7 +26,7 @@ hexdigit(int c)
 void
 usage(void)
 {
-       fprint(2, "Usage: %s [ -d ] [ file ]\n", argv0);
+       fprint(2, "usage: %s [ -d ] [ file ]\n", argv0);
        exits("usage");
 }
 
@@ -42,10 +42,14 @@ main(int argc, char *argv[])
        default:
                usage();
        } ARGEND;
+
        if(argc == 1){
-               close(0);
-               if(open(*argv, OREAD) < 0)
+               int fd;
+
+               fd = open(*argv, OREAD);
+               if(fd < 0)
                        sysfatal("%r");
+               if(fd != 0) dup(fd, 0);
        } else if(argc > 1)
                usage();
 
@@ -73,12 +77,13 @@ main(int argc, char *argv[])
                                        continue;
                                }
                                c = x1<<4 | x2;
-                       }
+                       } else if(c == '+')
+                               c = ' ';
                        Bputc(&bout, c);
                }
        } else {
                while((c = Bgetc(&bin)) >= 0){
-                       if(strchr("/$-_@.!*'(),", c)
+                       if(c>0 && strchr("/$-_@.!*'(),", c)
                        || 'a'<=c && c<='z'
                        || 'A'<=c && c<='Z'
                        || '0'<=c && c<='9')