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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
#include "common.h"
/*
* check for shell characters in a String
*/
static char *illegalchars = "\r\n";
extern int
shellchars(char *cp)
{
char *sp;
for(sp=illegalchars; *sp; sp++)
if(strchr(cp, *sp))
return 1;
return 0;
}
static char *specialchars = " ()<>{};=\\'\`^&|";
static char *escape = "%%";
int
hexchar(int x)
{
x &= 0xf;
if(x < 10)
return '0' + x;
else
return 'A' + x - 10;
}
/*
* rewrite a string to escape shell characters
*/
extern String*
escapespecial(String *s)
{
String *ns;
char *sp;
for(sp = specialchars; *sp; sp++)
if(strchr(s_to_c(s), *sp))
break;
if(*sp == 0)
return s;
ns = s_new();
for(sp = s_to_c(s); *sp; sp++){
if(strchr(specialchars, *sp)){
s_append(ns, escape);
s_putc(ns, hexchar(*sp>>4));
s_putc(ns, hexchar(*sp));
} else
s_putc(ns, *sp);
}
s_terminate(ns);
s_free(s);
return ns;
}
uint
hex2uint(char x)
{
if(x >= '0' && x <= '9')
return x - '0';
if(x >= 'A' && x <= 'F')
return (x - 'A') + 10;
if(x >= 'a' && x <= 'f')
return (x - 'a') + 10;
return -512;
}
/*
* rewrite a string to remove shell characters escapes
*/
extern String*
unescapespecial(String *s)
{
char *sp;
uint c, n;
String *ns;
if(strstr(s_to_c(s), escape) == 0)
return s;
n = strlen(escape);
ns = s_new();
for(sp = s_to_c(s); *sp; sp++){
if(strncmp(sp, escape, n) == 0){
c = (hex2uint(sp[n])<<4) | hex2uint(sp[n+1]);
if(c & 0x80)
s_putc(ns, *sp);
else {
s_putc(ns, c);
sp += n+2-1;
}
} else
s_putc(ns, *sp);
}
s_terminate(ns);
s_free(s);
return ns;
}
int
returnable(char *path)
{
return strcmp(path, "/dev/null") != 0;
}
int
temperror(void)
{
char err[ERRMAX];
rerrstr(err, sizeof(err));
return strstr(err, "too much activity") != nil || strstr(err, "temporary problem") != nil;
}
|