Rev 8103 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 8103 | Rev 8149 | ||
---|---|---|---|
Line 23... | Line 23... | ||
23 | { |
23 | { |
24 | fprintf(stderr,"%s: %s\n",progname, s); |
24 | fprintf(stderr,"%s: %s\n",progname, s); |
25 | exit(1); |
25 | exit(1); |
26 | } |
26 | } |
27 | 27 | ||
28 | /* |
- | |
29 | void *xmalloc(size_t n) |
- | |
30 | { |
- | |
31 | void *p; |
- | |
32 | p=malloc(n); |
- | |
33 | if(p==NULL) error("Malloc failure."); |
- | |
34 | return p; |
- | |
35 | } |
- | |
36 | */ |
- | |
37 | unsigned long int texint(void *bp, int l) |
28 | unsigned long int texint(void *bp, int l) |
38 | { |
29 | { |
39 | unsigned long int o, t; |
30 | unsigned long int o, t; |
40 | unsigned char c, *p; |
31 | unsigned char c, *p; |
41 | int i; |
32 | int i; |
42 | o=t=0; p=bp; |
33 | o=t=0; p=bp; |
43 | for(i=0;i<l;i++) { |
34 | for(i=0;i<l;i++) { |
44 | c=p[i]; t=c; o<<=8; o+=t; |
35 | c=p[i]; t=c; o<<=8; o+=t; |
45 | } |
36 | } |
46 | return o; |
37 | return o; |
47 | } |
38 | } |
48 | 39 | ||
49 | long int texintsigned(void *bp, int l) |
40 | long int texintsigned(void *bp, int l) |
50 | { |
41 | { |
51 | long int o, t; |
42 | long int o, t; |
52 | signed char c, *p; |
43 | signed char c, *p; |
53 | int i; |
44 | int i; |
54 | if(l<=0) return 0; |
45 | if(l<=0) return 0; |
55 | p=bp; o=*p; |
46 | p=bp; o=*p; |
56 | for(i=1;i<l;i++) { |
47 | for(i=1;i<l;i++) { |
57 | c=p[i]; t=c; o<<=8; o|=t&255; |
48 | c=p[i]; t=c; o<<=8; o|=t&255; |
58 | } |
49 | } |
59 | return o; |
50 | return o; |
60 | } |
51 | } |
61 | - | ||
62 | /* Strips leading spaces */ |
- | |
63 | /* |
- | |
64 | char *find_word_start(char *p) |
- | |
65 | { |
- | |
66 | int i; |
- | |
67 | for(i=0; isspace(*p); p++,i++); |
- | |
68 | return p; |
- | |
69 | } |
- | |
70 | */ |
- | |
71 | /* Points to the end of the word */ |
- | |
72 | /* |
- | |
73 | char *find_word_end(char *p) |
- | |
74 | { |
- | |
75 | int i; |
- | |
76 | for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++); |
- | |
77 | return p; |
- | |
78 | } |
- | |
79 | */ |
- | |
80 | - | ||
81 | /* searches a list. Returns index if found, (-1-index of insertion) if nomatch. |
- | |
82 | * Uses binary search, list must be sorted. */ |
- | |
83 | 52 | ||
84 | /* |
- | |
85 | int search_list(void *list, int items, size_t item_size, const char *str) |
- | |
86 | { |
- | |
87 | int i = 0; |
- | |
88 | while (items > 0) |
- | |
89 | { |
- | |
90 | int m = items / 2, j = i + m; |
- | |
91 | int k = strcmp(*(char **)(list + j * item_size), str); |
- | |
92 | if (k == 0) return j; |
- | |
93 | if (k > 0) items = m; else {i = j + 1; items -= (m + 1);} |
- | |
94 | } |
- | |
95 | return ~i; |
- | |
96 | } |
- | |
97 | */ |
- | |
98 |
|
53 | /* get the content of a file, and store in buf. */ |
99 | int getfile(char *fname, unsigned char **buf) |
54 | int getfile(char *fname, unsigned char **buf) |
100 | { |
55 | { |
101 | FILE *f; |
56 | FILE *f; |
102 | long l, l2; |
57 | long l, l2; |
103 | 58 | ||
Line 110... | Line 65... | ||
110 | l2=fread(*buf,1,l,f); fclose(f); |
65 | l2=fread(*buf,1,l,f); fclose(f); |
111 | if(l!=l2) { |
66 | if(l!=l2) { |
112 | free(*buf); return -1; |
67 | free(*buf); return -1; |
113 | } |
68 | } |
114 | return l; |
69 | return l; |
115 | } |
70 | } |
116 | 71 | ||
117 | int execredirected(char *cmdf, char *inf, char *outf, char *errf, char *arg[]) |
72 | int execredirected(char *cmdf, char *inf, char *outf, char *errf, char *arg[]) |
118 | { |
73 | { |
119 | pid_t pid; |
74 | pid_t pid; |
120 | int status; |
75 | int status; |
121 | 76 | ||
122 | fflush(NULL); /* flush all output streams before forking |
77 | fflush(NULL); /* flush all output streams before forking |
123 | * otherwise they will be doubled */ |
78 | * otherwise they will be doubled */ |
124 | pid=fork(); if(pid==-1) return -1; |
79 | pid=fork(); if(pid==-1) return -1; |
125 | if(!pid) { /* child */ |
80 | if(!pid) { /* child */ |
126 | if(inf!=NULL && freopen(inf,"r",stdin) == NULL) |
81 | if(inf!=NULL && freopen(inf,"r",stdin) == NULL) |
127 | error("freopen failure"); |
82 | error("freopen failure"); |
128 | if(outf!=NULL && freopen(outf,"w",stdout)) |
83 | if(outf!=NULL && freopen(outf,"w",stdout)) |
129 | error("freopen failure"); |
84 | error("freopen failure"); |
130 | if(errf!=NULL && freopen(errf,"w",stderr)) |
85 | if(errf!=NULL && freopen(errf,"w",stderr)) |
131 | error("freopen failure"); |
86 | error("freopen failure"); |
132 | if(wrapexec) { |
87 | if(wrapexec) { |
Line 140... | Line 95... | ||
140 | status=0; waitpid(pid,&status,0); |
95 | status=0; waitpid(pid,&status,0); |
141 | return WEXITSTATUS(status); |
96 | return WEXITSTATUS(status); |
142 | } |
97 | } |
143 | } |
98 | } |
144 | 99 | ||
145 |
|
100 | /* system(), but with variable parms |
146 | |
101 | * Uses sh to execute command. |
- | 102 | */ |
|
147 | int call_sh(char *s,...) |
103 | int call_sh(char *s,...) |
148 | { |
104 | { |
149 | va_list vp; |
105 | va_list vp; |
150 | char buf[MAX_LINELEN+1]; |
106 | char buf[MAX_LINELEN+1]; |
151 | char *parmbuf[4]; |
107 | char *parmbuf[4]; |
Line 157... | Line 113... | ||
157 | parmbuf[0]="sh"; parmbuf[1]="-c"; parmbuf[2]=buf; parmbuf[3]=NULL; |
113 | parmbuf[0]="sh"; parmbuf[1]="-c"; parmbuf[2]=buf; parmbuf[3]=NULL; |
158 | wrapexec=0; |
114 | wrapexec=0; |
159 | t=execredirected("sh",NULL,NULL,NULL,parmbuf); |
115 | t=execredirected("sh",NULL,NULL,NULL,parmbuf); |
160 | sync(); return t; |
116 | sync(); return t; |
161 | } |
117 | } |
162 | - | ||
163 | /* recursively generate a directory structure */ |
- | |
164 | /* |
- | |
165 | void mkdirs(char *s) |
- | |
166 | { |
- | |
167 | struct stat st; |
- | |
168 | char *buf; |
- | |
169 | if(stat(s,&st)==-1) { |
- | |
170 | if(strrchr(s,'/')!=NULL) { |
- | |
171 | buf=xmalloc(strlen(s)+1); |
- | |
172 | ovlstrcpy(buf,s); *strrchr(buf,'/')=0; |
- | |
173 | mkdirs(buf); free(buf); |
- | |
174 | } |
- | |
175 | mkdir(s,-1); |
- | |
176 | } |
- | |
177 | } |
- | |
178 | */ |
- | |
179 | 118 |