]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/urlencode.c
replace urlencode with c version that isnt broken for utf-8
[plan9front.git] / sys / src / cmd / urlencode.c
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4
5 Biobuf  bin;
6 Biobuf  bout;
7 int     dflag;
8
9 char    hex[] = "0123456789abcdef";
10 char    Hex[] = "0123456789ABCDEF";
11
12 int
13 hexdigit(int c)
14 {
15         char *p;
16
17         if(c >= 0){
18                 if((p = strchr(Hex, c)) != 0)
19                         return p - Hex;
20                 if((p = strchr(hex, c)) != 0)
21                         return p - hex;
22         }
23         return -1;
24 }
25
26 void
27 usage(void)
28 {
29         fprint(2, "Usage: %s [ -d ] [ file ]\n", argv0);
30         exits("usage");
31 }
32
33 void
34 main(int argc, char *argv[])
35 {
36         int c;
37
38         ARGBEGIN {
39         case 'd':
40                 dflag = 1;
41                 break;
42         default:
43                 usage();
44         } ARGEND;
45         if(argc == 1){
46                 close(0);
47                 if(open(*argv, OREAD) < 0)
48                         sysfatal("%r");
49         } else if(argc > 1)
50                 usage();
51
52         Binit(&bin, 0, OREAD);
53         Binit(&bout, 1, OWRITE);
54
55         if(dflag){
56                 while((c = Bgetc(&bin)) >= 0){
57                         if(c == '%'){
58                                 int c1, c2, x1, x2;
59
60                                 if((c1 = Bgetc(&bin)) < 0)
61                                         break;
62                                 if((x1 = hexdigit(c1)) < 0){
63                                         Bungetc(&bin);
64                                         Bputc(&bout, c);
65                                         continue;
66                                 }
67                                 if((c2 = Bgetc(&bin)) < 0)
68                                         break;
69                                 if((x2 = hexdigit(c2)) < 0){
70                                         Bungetc(&bin);
71                                         Bputc(&bout, c);
72                                         Bputc(&bout, c1);
73                                         continue;
74                                 }
75                                 c = x1<<4 | x2;
76                         }
77                         Bputc(&bout, c);
78                 }
79         } else {
80                 while((c = Bgetc(&bin)) >= 0){
81                         if(strchr("/$-_@.!*'(),", c)
82                         || 'a'<=c && c<='z'
83                         || 'A'<=c && c<='Z'
84                         || '0'<=c && c<='9')
85                                 Bputc(&bout, c);
86                         else if(c == ' ')
87                                 Bputc(&bout, '+');
88                         else {
89                                 Bputc(&bout, '%');
90                                 Bputc(&bout, Hex[c>>4]);
91                                 Bputc(&bout, Hex[c&15]);
92                         }
93                 }
94         }
95
96         Bflush(&bout);
97         exits(0);
98 }