Rev 8082 | Rev 8120 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 8082 | Rev 8100 | ||
---|---|---|---|
Line 14... | Line 14... | ||
14 | * along with this program; if not, write to the Free Software |
14 | * along with this program; if not, write to the Free Software |
15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
16 | */ |
16 | */ |
17 | 17 | ||
18 | /* Common routines in interfaces */ |
18 | /* Common routines in interfaces */ |
19 | #include "../Lib/basicstr.c" |
- | |
20 | 19 | ||
21 | #define ch_root "bin/ch..root" |
20 | #define ch_root "bin/ch..root" |
22 | 21 | ||
23 | int mypid; |
22 | int mypid; |
24 | int must_chroot=0; |
23 | int must_chroot=0; |
Line 49... | Line 48... | ||
49 | void check_parm(char *p); |
48 | void check_parm(char *p); |
50 | void output(char *p); |
49 | void output(char *p); |
51 | void about(void); |
50 | void about(void); |
52 | char *dynsetup(char *p, char *end); |
51 | char *dynsetup(char *p, char *end); |
53 | 52 | ||
- | 53 | /* |
|
54 | void *xmalloc(size_t n) |
54 | void *xmalloc(size_t n) |
55 | { |
55 | { |
56 | void *p; |
56 | void *p; |
57 | p=malloc(n); |
57 | p=malloc(n); |
58 | if(p==NULL) { |
58 | if(p==NULL) { |
59 | fprintf(stderr, "%s: Malloc failure.\n",progname); |
59 | fprintf(stderr, "%s: Malloc failure.\n",progname); |
60 | exit(1); |
60 | exit(1); |
61 | } |
61 | } |
62 | return p; |
62 | return p; |
63 | } |
63 | } |
64 | 64 | */ |
|
65 | /* strip trailing spaces; return string end. */ |
65 | /* strip trailing spaces; return string end. */ |
66 | char * |
66 | char *strip_trailing_spaces2(char *p) |
67 | { |
67 | { |
68 | char *pp; |
68 | char *pp; |
69 | if(*p==0) return p; |
69 | if(*p==0) return p; |
70 | for(pp=p+strlen(p)-1; pp>=p && isspace(*pp); *(pp--)=0); |
70 | for(pp=p+strlen(p)-1; pp>=p && isspace(*pp); *(pp--)=0); |
71 | return pp; |
71 | return pp; |
72 | } |
72 | } |
73 | 73 | ||
74 | /* Points to the end of the word */ |
74 | /* Points to the end of the word */ |
- | 75 | /* |
|
75 | char *find_word_end(char *p) |
76 | char *find_word_end(char *p) |
76 |
|
77 | { |
77 | int i; |
78 | int i; |
78 | for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++); |
79 | for(i=0;!isspace(*p) && *p!=0 && i<MAX_LINELEN; p++,i++); |
79 | return p; |
80 | return p; |
80 | } |
81 | } |
81 | 82 | */ |
|
82 | /* Strips leading spaces */ |
83 | /* Strips leading spaces */ |
- | 84 | /* |
|
83 | char *find_word_start(char *p) |
85 | char *find_word_start(char *p) |
84 | { |
86 | { |
85 | int i; |
87 | int i; |
86 | for(i=0; isspace(*p) && i<MAX_LINELEN; p++,i++); |
88 | for(i=0; isspace(*p) && i<MAX_LINELEN; p++,i++); |
87 | return p; |
89 | return p; |
88 | } |
90 | } |
89 | 91 | */ |
|
90 | /* Find first occurrence of word */ |
92 | /* Find first occurrence of word */ |
91 | char * |
93 | char *wordchr2(char *p, char *w) |
92 | { |
94 | { |
93 | char *r; |
95 | char *r; |
94 | 96 | ||
95 | if(*w==0) return NULL; |
97 | if(*w==0) return NULL; |
96 | for(r=strstr(p,w);r!=NULL && |
98 | for(r=strstr(p,w);r!=NULL && |
97 | ( (r>p && !isspace(*(r-1))) || (!isspace(*(r+strlen(w))) && *(r+strlen(w))!=0) ); |
99 | ( (r>p && !isspace(*(r-1))) || (!isspace(*(r+strlen(w))) && *(r+strlen(w))!=0) ); |
98 | r=strstr(r+1,w)); |
100 | r=strstr(r+1,w)); |
99 | return r; |
101 | return r; |
100 | } |
102 | } |
101 | 103 | ||
102 |
|
104 | /* Returns the pointer or NULL. */ |
- | 105 | /* |
|
103 | char *varchr(char *p, char *v) |
106 | char *varchr(char *p, char *v) |
104 | { |
107 | { |
105 | char *pp; int n=strlen(v); |
108 | char *pp; int n=strlen(v); |
106 | for(pp=strstr(p,v); pp!=NULL; pp=strstr(pp+1,v)) { |
109 | for(pp=strstr(p,v); pp!=NULL; pp=strstr(pp+1,v)) { |
107 | if((pp==p || (!isalnum(*(pp-1)) && *(pp-1)!='_')) && |
110 | if((pp==p || (!isalnum(*(pp-1)) && *(pp-1)!='_')) && |
108 | ((!isalnum(*(pp+n)) && *(pp+n)!='_') || *(pp+n)==0)) break; |
111 | ((!isalnum(*(pp+n)) && *(pp+n)!='_') || *(pp+n)==0)) break; |
109 | } |
112 | } |
110 | return pp; |
113 | return pp; |
111 | } |
114 | } |
112 | 115 | */ |
|
113 | /* find matching parenthesis */ |
116 | /* find matching parenthesis */ |
114 | char * |
117 | char *find_matching2(char *p, char c) |
115 | { |
118 | { |
116 | char *pp; |
119 | char *pp; |
117 | int parenth, brak, brace; |
120 | int parenth, brak, brace; |
118 | parenth=brak=brace=0; |
121 | parenth=brak=brace=0; |
119 | for(pp=p; *pp!=0; pp++) { |
122 | for(pp=p; *pp!=0; pp++) { |
Line 136... | Line 139... | ||
136 | } |
139 | } |
137 | 140 | ||
138 | /* searches a list. Returns index if found, (-1-index of insertion) if nomatch. |
141 | /* searches a list. Returns index if found, (-1-index of insertion) if nomatch. |
139 | * Uses binary search, list must be sorted. */ |
142 | * Uses binary search, list must be sorted. */ |
140 | 143 | ||
- | 144 | /* |
|
141 | int search_list(void *list, int items, size_t item_size, const char *str) |
145 | int search_list(void *list, int items, size_t item_size, const char *str) |
142 | { |
146 | { |
143 | int i = 0; |
147 | int i = 0; |
144 | while (items > 0) |
148 | while (items > 0) |
145 | { |
149 | { |
Line 148... | Line 152... | ||
148 | if (k == 0) return j; |
152 | if (k == 0) return j; |
149 | if (k > 0) items = m; else {i = j + 1; items -= (m + 1);} |
153 | if (k > 0) items = m; else {i = j + 1; items -= (m + 1);} |
150 | } |
154 | } |
151 | return ~i; |
155 | return ~i; |
152 | } |
156 | } |
- | 157 | */ |
|
153 | 158 | ||
154 | /* Read/write to a file with variable parms to print filename */ |
159 | /* Read/write to a file with variable parms to print filename */ |
155 | void accessfile(char *content, char *type, char *s,...) |
160 | void accessfile(char *content, char *type, char *s,...) |
156 | { |
161 | { |
157 | va_list vp; |
162 | va_list vp; |
Line 204... | Line 209... | ||
204 | char buf[MAX_LINELEN+1], pidbuf[32]; |
209 | char buf[MAX_LINELEN+1], pidbuf[32]; |
205 | int l; |
210 | int l; |
206 | snprintf(pidbuf,sizeof(pidbuf),"%u",pid); |
211 | snprintf(pidbuf,sizeof(pidbuf),"%u",pid); |
207 | accessfile(buf,"r",pidfname); l=strlen(buf); |
212 | accessfile(buf,"r",pidfname); l=strlen(buf); |
208 | if(l>=MAX_LINELEN-64) return; |
213 | if(l>=MAX_LINELEN-64) return; |
209 | if( |
214 | if(wordchr2(buf,pidbuf)==NULL) { |
210 | snprintf(buf+l,sizeof(buf)-l," %s",pidbuf); |
215 | snprintf(buf+l,sizeof(buf)-l," %s",pidbuf); |
211 | accessfile(buf,"w",pidfname); |
216 | accessfile(buf,"w",pidfname); |
212 | } |
217 | } |
213 | } |
218 | } |
214 | 219 | ||
Line 216... | Line 221... | ||
216 | void rmpid(int pid) |
221 | void rmpid(int pid) |
217 | { |
222 | { |
218 | char buf[MAX_LINELEN+1], pidbuf[32], *p; |
223 | char buf[MAX_LINELEN+1], pidbuf[32], *p; |
219 | snprintf(pidbuf,sizeof(pidbuf),"%u",pid); |
224 | snprintf(pidbuf,sizeof(pidbuf),"%u",pid); |
220 | accessfile(buf,"r",pidfname); |
225 | accessfile(buf,"r",pidfname); |
221 | p= |
226 | p=wordchr2(buf,pidbuf); |
222 | if(p!=NULL) { |
227 | if(p!=NULL) { |
223 | ovlstrcpy(p,find_word_start(find_word_end(p))); |
228 | ovlstrcpy(p,find_word_start(find_word_end(p))); |
224 | accessfile(buf,"w",pidfname); |
229 | accessfile(buf,"w",pidfname); |
225 | } |
230 | } |
226 | } |
231 | } |