1772
|
1 /* osk.c Various function OSK requires.
|
|
2 Copyright (C) 1994 Boisy G. Pitre
|
|
3
|
|
4 This file is part of the OS-9 UUCP package, UUCPbb.
|
|
5
|
|
6 This program is free software; you can redistribute it and/or modify
|
|
7 it under the terms of the GNU General Public License as published by
|
|
8 the Free Software Foundation; either version 2 of the License, or
|
|
9 (at your option) any later version.
|
|
10
|
|
11 This program is distributed in the hope that it will be useful,
|
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 GNU General Public License for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with this program; if not, write to the Free Software
|
|
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
19
|
|
20 The author of UUCPbb, Bob Billson, can be contacted at:
|
|
21 bob@kc2wz.bubble.org or uunet!kc2wz!bob or by snail mail:
|
|
22 21 Bates Way, Westfield, NJ 07090
|
|
23 */
|
|
24
|
|
25 #ifdef _OSK
|
|
26 /********************************************************************\
|
|
27 * Functions which are in the CoCo C libraries but not OSK's or are *
|
|
28 * unique to OSK. Written by Boisy Pitre. *
|
|
29 \********************************************************************/
|
|
30
|
|
31 #include "uucp.h"
|
|
32 #include "password.h"
|
|
33
|
|
34 char *strend();
|
|
35
|
|
36 #ifndef _UCC
|
|
37 /* MWC 3.2 doesn't have strtok() or strpbrk(), so we provide it */
|
|
38 char *strtok();
|
|
39 char *strpbrk();
|
|
40
|
|
41 unsigned char chartable[16];
|
|
42 unsigned char cval;
|
|
43
|
|
44 static char *p_last1 = "";
|
|
45
|
|
46 char *strtok(s1,s2)
|
|
47 char *s1;
|
|
48 char *s2;
|
|
49 {
|
|
50 register unsigned char *p;
|
|
51 register unsigned char c;
|
|
52 register int i;
|
|
53
|
|
54 if (s1 == NULL)
|
|
55 s1 = p_last1;
|
|
56
|
|
57 if (++cval == 0) {
|
|
58 memset(chartable, sizeof(chartable), 0);
|
|
59 cval++;
|
|
60 }
|
|
61 c = cval;
|
|
62 for (p = (unsigned char *)s2; *p;)
|
|
63 chartable[*p++] = c;
|
|
64
|
|
65 for (p = (unsigned char *)s1; *p; p++)
|
|
66 if (chartable[*p] != c)
|
|
67 break;
|
|
68 if (*p == '\0')
|
|
69 return((char *)NULL);
|
|
70 s1 = (char *)p;
|
|
71
|
|
72 for (; *p; p++)
|
|
73 if (chartable[*p] == c)
|
|
74 break;
|
|
75
|
|
76 p_last1 = (char *)p;
|
|
77
|
|
78 if (*p != '\0') {
|
|
79 *p_last1++ = '\0';
|
|
80 }
|
|
81 return(s1);
|
|
82 }
|
|
83
|
|
84
|
|
85 char *strpbrk(line, delims)
|
|
86 char *line, *delims;
|
|
87 {
|
|
88 char *p = line, *q;
|
|
89
|
|
90 while (*p != '\0') {
|
|
91 for (q = delims; q != '\0'; q++) {
|
|
92 if (*p == *q) return(p);
|
|
93 }
|
|
94 }
|
|
95 return(NULL);
|
|
96 }
|
|
97 #endif /* !_UCC */
|
|
98
|
|
99
|
|
100
|
|
101 char *strend (s)
|
|
102 char *s;
|
|
103 {
|
|
104 while (*s)
|
|
105 ++s;
|
|
106
|
|
107 return (s);
|
|
108 }
|
|
109
|
|
110
|
|
111
|
|
112 int min (v1, v2)
|
|
113 int v1, v2;
|
|
114 {
|
|
115 return ((v1 < v2) ? v1 : v2);
|
|
116 }
|
|
117
|
|
118
|
|
119
|
|
120 int max (v1, v2)
|
|
121 int v1, v2;
|
|
122 {
|
|
123 return ((v1 > v2) ? v1 : v2);
|
|
124 }
|
|
125
|
|
126
|
|
127
|
|
128 asetuid (uid)
|
|
129 int uid;
|
|
130 {
|
|
131 setuid (uid);
|
|
132 }
|
|
133
|
|
134
|
|
135
|
|
136 /* strnucmp - string compare w/ case masking on a set number of characters.
|
|
137 compares two strings regardless of their case up to 'n' characters
|
|
138 and returns:
|
|
139 -1 str1 < str2
|
|
140 0 str1 = str2
|
|
141 1 str1 > str2
|
|
142
|
|
143 NOTE: strnucmp() will replace an occurence of \n (carriage return) with a
|
|
144 NULL! This allows you to pass in strings that are carriage
|
|
145 return-terminated without worry, but be cautious if that's NOT what you
|
|
146 want to do.
|
|
147 */
|
|
148
|
|
149 int strnucmp (str1, str2, length)
|
|
150 register char *str1, *str2;
|
|
151 register int length;
|
|
152 {
|
|
153 int pl1, pl2;
|
|
154 register char *p;
|
|
155
|
|
156 if ((p = strchr (str1,'\n')) != NULL) *p ='\0';
|
|
157 if ((p = strchr (str2,'\n')) != NULL) *p ='\0';
|
|
158 pl1 = pl2 = length;
|
|
159
|
|
160 while ((pl1 > 1) && (toupper (*str1) == toupper (*str2))) {
|
|
161 --pl1;
|
|
162 ++str1;
|
|
163 ++str2;
|
|
164 }
|
|
165
|
|
166 if (*str1 == *str2) return(0);
|
|
167 if (*str1 > *str2) return(1);
|
|
168 return(-1);
|
|
169 }
|
|
170
|
|
171
|
|
172
|
|
173 int strucmp (str1, str2)
|
|
174 register char *str1, *str2;
|
|
175 {
|
|
176 register char *p;
|
|
177
|
|
178 if ((p = strchr (str1,'\n')) != NULL) *p ='\0';
|
|
179 if ((p = strchr (str2,'\n')) != NULL) *p ='\0';
|
|
180
|
|
181 while (toupper (*str1) == toupper (*str2) && *str1 && *str2) {
|
|
182 ++str1;
|
|
183 ++str2;
|
|
184 }
|
|
185
|
|
186 if (*str1 == *str2) return(0);
|
|
187 if (*str1 > *str2) return(1);
|
|
188 return(-1);
|
|
189 }
|
|
190
|
|
191
|
|
192
|
|
193 #ifdef _OSK
|
|
194 /* Function to return an integer value of a GID/UID string (i.e. "30.122")
|
|
195 For OSK only
|
|
196 */
|
|
197 int uIDtoInt (uidstr)
|
|
198 char *uidstr;
|
|
199 {
|
|
200 char *p, tmpstr[32];
|
|
201
|
|
202 strcpy(tmpstr, uidstr);
|
|
203
|
|
204 if ((p = strchr(tmpstr, '.')) != 0) {
|
|
205 *p = '\0';
|
|
206 return (atoi(tmpstr) * 65536 + atoi (++p));
|
|
207 }
|
|
208 return(-1);
|
|
209 }
|
|
210
|
|
211
|
|
212
|
|
213 /* Function to return an a GID/UID string (i.e. "30.122") from an int
|
|
214 For OSK only
|
|
215 It is the caller's responsibility to free the allocated memory.
|
|
216 */
|
|
217 char *InttouID (uid)
|
|
218 int uid;
|
|
219 {
|
|
220 char *p, tmpstr[32];
|
|
221 int group, user;
|
|
222
|
|
223 p = (char *)malloc(sizeof(char) * 12);
|
|
224 group = uid / 65536;
|
|
225 user = uid % 65536;
|
|
226 sprintf(p, "%d.%d", group, user);
|
|
227 return(p);
|
|
228 }
|
|
229 #endif
|
|
230
|
|
231
|
|
232
|
|
233 /*#define MAIN /* Only use when testing routines */
|
|
234
|
|
235 #ifdef MAIN
|
|
236 #define DEBUG
|
|
237 #endif
|
|
238
|
|
239 /*
|
|
240 * Popen, and Pclose, for OS-9.
|
|
241 *
|
|
242 * Simmule Turner - simmy@nu.cs.fsu.edu - 70651,67
|
|
243 *
|
|
244 * V 1.3 10/22/88 - Forgot to close pipe on an error.
|
|
245 * V 1.2 06/28/88 - Removed shell as parent process.
|
|
246 * - It forks command directly now.
|
|
247 * V 1.1 06/28/88 - Uses a shell to run child process SrT
|
|
248 * - Fixed bug found by PWL, SrT
|
|
249 * - Improved error checking, cleaned up code.
|
|
250 * V 1.0 06/25/88 - Initial coding SrT
|
|
251 *
|
|
252 */
|
|
253
|
|
254 #define ERR (-1)
|
|
255 #define PIPEMAX _NFILE
|
|
256
|
|
257 #define RESTORE close(path);dup(save);close(save);
|
|
258
|
|
259 static int _pid[PIPEMAX];
|
|
260
|
|
261 FILE *popen(command, type)
|
|
262 char *command, *type;
|
|
263 {
|
|
264 char *argv[32], cmd[256];
|
|
265 FILE *_pfp;
|
|
266 int l, path, pipe, pcnt, save;
|
|
267
|
|
268 path = (*type == 'w') ? STDIN : STDOUT;
|
|
269
|
|
270 if ((pipe = open ("/pipe",READ+WRITE)) == ERR)
|
|
271 return (NULL);
|
|
272 pcnt = pipe;
|
|
273
|
|
274 if ((save = dup (path)) == ERR) {
|
|
275 close (pipe);
|
|
276 return (NULL);
|
|
277 }
|
|
278 close (path);
|
|
279
|
|
280 if (dup (pipe) == ERR) {
|
|
281 dup (save);
|
|
282 close (save);
|
|
283 close (pipe); /* BUG fix 10/22/88 SrT */
|
|
284 return (NULL);
|
|
285 }
|
|
286
|
|
287 strcpy(cmd, command);
|
|
288 parse_cmd (argv, cmd);
|
|
289 # ifdef _UCC
|
|
290 if ((_pid[pcnt] = os9exec (os9fork, argv[0], argv, _environ, 0, 0, 32)) == ERR) {
|
|
291 # else /* C 3.2 */
|
|
292 if ((_pid[pcnt] = os9exec (os9fork, argv[0], argv, environ, 0, 0, 32)) == ERR) {
|
|
293 # endif
|
|
294 { RESTORE }
|
|
295 close (pipe);
|
|
296 _pid[pcnt] = 0;
|
|
297 return (NULL);
|
|
298 }
|
|
299
|
|
300 { RESTORE }
|
|
301
|
|
302 if ((_pfp = fdopen (pipe,type)) == NULL) {
|
|
303 close (pipe);
|
|
304 while (((l=wait(0)) != _pid[pcnt]) && l != ERR)
|
|
305 ;
|
|
306 _pid[pcnt] = 0;
|
|
307 return (NULL);
|
|
308 }
|
|
309
|
|
310 return (_pfp);
|
|
311 }
|
|
312
|
|
313
|
|
314
|
|
315 int pclose (stream)
|
|
316 FILE *stream;
|
|
317 {
|
|
318 register int i;
|
|
319 int f,status;
|
|
320
|
|
321 f = fileno (stream);
|
|
322 fclose (stream);
|
|
323 #ifdef DEBUG
|
|
324 fprintf (stderr,"Pclose: Fileno=%d PID=%d\n",f,_pid[f]);
|
|
325 #endif
|
|
326 while (((i=wait (&status)) != _pid[f]) && i != ERR)
|
|
327 ;
|
|
328 _pid[f]= 0;
|
|
329 return ((i == ERR) ? ERR : status);
|
|
330 }
|
|
331
|
|
332
|
|
333
|
|
334 #ifdef MAIN
|
|
335
|
|
336 #define LINSIZ 200
|
|
337 main() {
|
|
338 char line[LINSIZ];
|
|
339 FILE *popen(), *fp;
|
|
340 int status;
|
|
341
|
|
342 /* Test the read side of popen
|
|
343 * SrT 06/25/88 */
|
|
344
|
|
345 if (( fp = popen ("procs e","r")) != NULL) {
|
|
346 while (fgets (line,LINSIZ,fp) != NULL)
|
|
347 printf ("%s",line);
|
|
348 if ((status = pclose (fp)) == ERR) {
|
|
349 fprintf (stderr,"***ERR: closing read pipe ERR #%03d\n",
|
|
350 status&0xff);
|
|
351 exit(1);
|
|
352 }
|
|
353 printf ("Read status =%d\n",status&0xff);
|
|
354 }
|
|
355 else {
|
|
356 fprintf(stderr,"***ERR: opening read pipe\n");
|
|
357 exit(1);
|
|
358 }
|
|
359
|
|
360 /* Test the write side of popen
|
|
361 * SrT 06/25/88 */
|
|
362 if (( fp = popen ("woof one two three four","w")) != NULL) {
|
|
363 while (fgets (line,LINSIZ,stdin) != NULL)
|
|
364 fprintf (fp,"%s",line);
|
|
365 if ((status = pclose(fp)) == ERR) {
|
|
366 fprintf (stderr,"***ERR: closing write pipe ERR #%03d\n",
|
|
367 status&0xff);
|
|
368 exit(1);
|
|
369 }
|
|
370 printf ("Write status =%d\n",status&0xff);
|
|
371 }
|
|
372 else {
|
|
373 fprintf(stderr,"***ERR: opening write pipe\n");
|
|
374 exit(1);
|
|
375 }
|
|
376 }
|
|
377 #endif
|
|
378 #endif
|