forked from wuyongzheng/urlencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurlencode.c
More file actions
71 lines (64 loc) · 1.44 KB
/
urlencode.c
File metadata and controls
71 lines (64 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <string.h>
void print_encode_char (char c)
{
/* RFC3986:
* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
*/
if (c >= 'A' && c <= 'Z')
putchar(c);
else if (c >= 'a' && c <= 'z')
putchar(c);
else if (c >= '0' && c <= '9')
putchar(c);
else if (c == '-' || c == '_' || c == '.' || c == '~')
putchar(c);
else
printf("%%%02X", (unsigned int)(unsigned char)c);
}
void process_stdio (void)
{
int c;
while ((c = getchar()) != EOF) {
print_encode_char((char)c);
}
}
void print_encode_str (const char *str)
{
while (*str) {
print_encode_char(*str);
str ++;
}
}
int main (int argc, char *argv[])
{
if (argc == 2 && (strcmp(argv[1], "-h") == 0 ||
strcmp(argv[1], "--help") == 0)) {
printf("Usage:\n");
printf("1. %s val\n", argv[0]);
printf(" Output application/x-www-form-urlencoded of val\n");
printf("2. %s name1 val1 name2 val2 ...\n", argv[0]);
printf(" Generate name1=val1&name2=val2 style output.\n");
printf(" There must be even number of arguments.\n");
printf("3. %s\n", argv[0]);
printf(" Same as 1, but read from stdin.\n");
return 0;
}
if (argc == 1)
process_stdio();
else if (argc == 2)
print_encode_str(argv[1]);
else {
int i;
print_encode_str(argv[1]);
putchar('=');
print_encode_str(argv[2]);
for (i = 3; i + 1 < argc; i += 2) {
putchar('&');
print_encode_str(argv[i]);
putchar('=');
print_encode_str(argv[i + 1]);
}
}
return 0;
}