Annotation of src/usr.bin/make/util.c, Revision 1.13
1.13 ! espie 1: /* $OpenBSD: util.c,v 1.12 2000/06/23 16:39:45 espie Exp $ */
1.6 millert 2: /* $NetBSD: util.c,v 1.10 1996/12/31 17:56:04 christos Exp $ */
1.1 deraadt 3:
4: /*
5: * Missing stuff from OS's
6: */
7:
8: #include <stdio.h>
9: #include "make.h"
1.6 millert 10: #include <sys/param.h>
1.13 ! espie 11:
! 12: #ifndef lint
! 13: UNUSED
! 14: static char rcsid[] = "$OpenBSD: util.c,v 1.12 2000/06/23 16:39:45 espie Exp $";
! 15: #endif
1.1 deraadt 16:
1.7 mickey 17: #ifndef __STDC__
1.1 deraadt 18: # ifndef const
19: # define const
20: # endif
21: #endif
22:
23: #ifdef sun
24:
25:
26:
27: extern int errno, sys_nerr;
28: extern char *sys_errlist[];
29:
30: char *
1.5 millert 31: strerror(e)
32: int e;
1.1 deraadt 33: {
34: static char buf[100];
35: if (e < 0 || e >= sys_nerr) {
36: sprintf(buf, "Unknown error %d", e);
37: return buf;
38: }
39: else
40: return sys_errlist[e];
41: }
42: #endif
43:
1.4 briggs 44: #ifdef ultrix
45: #include <string.h>
46:
47: /* strdup
48: *
49: * Make a duplicate of a string.
50: * For systems which lack this function.
51: */
52: char *
53: strdup(str)
54: const char *str;
55: {
56: size_t len;
1.5 millert 57: char *p;
1.4 briggs 58:
59: if (str == NULL)
60: return NULL;
61: len = strlen(str) + 1;
62: if ((p = malloc(len)) == NULL)
63: return NULL;
64:
65: return memcpy(p, str, len);
66: }
67:
68: #endif
69:
70: #if defined(sun) || defined(__hpux) || defined(__sgi)
1.1 deraadt 71:
72: int
73: setenv(name, value, dum)
1.5 millert 74: const char *name;
1.1 deraadt 75: const char *value;
76: int dum;
77: {
78: register char *p;
79: int len = strlen(name) + strlen(value) + 2; /* = \0 */
80: char *ptr = (char*) malloc(len);
81:
82: (void) dum;
83:
84: if (ptr == NULL)
85: return -1;
1.5 millert 86:
1.1 deraadt 87: p = ptr;
88:
1.5 millert 89: while (*name)
1.1 deraadt 90: *p++ = *name++;
91:
92: *p++ = '=';
93:
1.5 millert 94: while (*value)
1.1 deraadt 95: *p++ = *value++;
96:
97: *p = '\0';
98:
99: len = putenv(ptr);
100: /* free(ptr); */
101: return len;
102: }
103: #endif
104:
105: #ifdef __hpux
106: #include <sys/types.h>
107: #include <sys/param.h>
108: #include <sys/syscall.h>
109: #include <sys/signal.h>
110: #include <sys/stat.h>
111: #include <stdio.h>
112: #include <dirent.h>
113: #include <sys/time.h>
114: #include <time.h>
115: #include <unistd.h>
116:
117:
118: int
119: killpg(pid, sig)
120: int pid, sig;
121: {
122: return kill(-pid, sig);
123: }
124:
125: void
126: srandom(seed)
127: long seed;
128: {
129: srand48(seed);
130: }
131:
132: long
133: random()
134: {
135: return lrand48();
136: }
137:
138: /* turn into bsd signals */
139: void (*
140: signal(s, a)) ()
141: int s;
142: void (*a)();
143: {
144: struct sigvec osv, sv;
145:
146: (void) sigvector(s, (struct sigvec *) 0, &osv);
147: sv = osv;
148: sv.sv_handler = a;
149: #ifdef SV_BSDSIG
150: sv.sv_flags = SV_BSDSIG;
151: #endif
152:
153: if (sigvector(s, &sv, (struct sigvec *) 0) == -1)
1.10 deraadt 154: return (SIG_ERR);
1.1 deraadt 155: return (osv.sv_handler);
156: }
157:
158: #if !defined(BSD) && !defined(d_fileno)
159: # define d_fileno d_ino
160: #endif
161:
162: #ifndef DEV_DEV_COMPARE
163: # define DEV_DEV_COMPARE(a, b) ((a) == (b))
164: #endif
165: #define ISDOT(c) ((c)[0] == '.' && (((c)[1] == '\0') || ((c)[1] == '/')))
166: #define ISDOTDOT(c) ((c)[0] == '.' && ISDOT(&((c)[1])))
167:
168:
169: /* strrcpy():
170: * Like strcpy, going backwards and returning the new pointer
171: */
172: static char *
173: strrcpy(ptr, str)
174: register char *ptr, *str;
175: {
176: register int len = strlen(str);
177:
178: while (len)
179: *--ptr = str[--len];
180:
181: return (ptr);
182: } /* end strrcpy */
183:
184:
185: char *
186: getwd(pathname)
187: char *pathname;
188: {
189: DIR *dp;
190: struct dirent *d;
191: extern int errno;
192:
193: struct stat st_root, st_cur, st_next, st_dotdot;
194: char pathbuf[MAXPATHLEN], nextpathbuf[MAXPATHLEN * 2];
195: char *pathptr, *nextpathptr, *cur_name_add;
196:
197: /* find the inode of root */
198: if (stat("/", &st_root) == -1) {
199: (void) sprintf(pathname,
200: "getwd: Cannot stat \"/\" (%s)", strerror(errno));
201: return (NULL);
202: }
203: pathbuf[MAXPATHLEN - 1] = '\0';
204: pathptr = &pathbuf[MAXPATHLEN - 1];
205: nextpathbuf[MAXPATHLEN - 1] = '\0';
206: cur_name_add = nextpathptr = &nextpathbuf[MAXPATHLEN - 1];
207:
208: /* find the inode of the current directory */
209: if (lstat(".", &st_cur) == -1) {
210: (void) sprintf(pathname,
211: "getwd: Cannot stat \".\" (%s)", strerror(errno));
212: return (NULL);
213: }
214: nextpathptr = strrcpy(nextpathptr, "../");
215:
216: /* Descend to root */
217: for (;;) {
218:
219: /* look if we found root yet */
220: if (st_cur.st_ino == st_root.st_ino &&
221: DEV_DEV_COMPARE(st_cur.st_dev, st_root.st_dev)) {
222: (void) strcpy(pathname, *pathptr != '/' ? "/" : pathptr);
223: return (pathname);
224: }
225:
226: /* open the parent directory */
227: if (stat(nextpathptr, &st_dotdot) == -1) {
228: (void) sprintf(pathname,
229: "getwd: Cannot stat directory \"%s\" (%s)",
230: nextpathptr, strerror(errno));
231: return (NULL);
232: }
233: if ((dp = opendir(nextpathptr)) == NULL) {
234: (void) sprintf(pathname,
235: "getwd: Cannot open directory \"%s\" (%s)",
236: nextpathptr, strerror(errno));
237: return (NULL);
238: }
239:
240: /* look in the parent for the entry with the same inode */
241: if (DEV_DEV_COMPARE(st_dotdot.st_dev, st_cur.st_dev)) {
242: /* Parent has same device. No need to stat every member */
1.5 millert 243: for (d = readdir(dp); d != NULL; d = readdir(dp))
1.1 deraadt 244: if (d->d_fileno == st_cur.st_ino)
245: break;
246: }
247: else {
1.5 millert 248: /*
249: * Parent has a different device. This is a mount point so we
250: * need to stat every member
1.1 deraadt 251: */
252: for (d = readdir(dp); d != NULL; d = readdir(dp)) {
253: if (ISDOT(d->d_name) || ISDOTDOT(d->d_name))
254: continue;
255: (void) strcpy(cur_name_add, d->d_name);
256: if (lstat(nextpathptr, &st_next) == -1) {
257: (void) sprintf(pathname, "getwd: Cannot stat \"%s\" (%s)",
258: d->d_name, strerror(errno));
259: (void) closedir(dp);
260: return (NULL);
261: }
262: /* check if we found it yet */
263: if (st_next.st_ino == st_cur.st_ino &&
1.5 millert 264: DEV_DEV_COMPARE(st_next.st_dev, st_cur.st_dev))
1.1 deraadt 265: break;
266: }
267: }
268: if (d == NULL) {
269: (void) sprintf(pathname, "getwd: Cannot find \".\" in \"..\"");
270: (void) closedir(dp);
271: return (NULL);
272: }
273: st_cur = st_dotdot;
274: pathptr = strrcpy(pathptr, d->d_name);
275: pathptr = strrcpy(pathptr, "/");
276: nextpathptr = strrcpy(nextpathptr, "../");
277: (void) closedir(dp);
278: *cur_name_add = '\0';
279: }
280: } /* end getwd */
281:
282:
283: char *sys_siglist[] = {
284: "Signal 0",
285: "Hangup", /* SIGHUP */
286: "Interrupt", /* SIGINT */
287: "Quit", /* SIGQUIT */
288: "Illegal instruction", /* SIGILL */
289: "Trace/BPT trap", /* SIGTRAP */
290: "IOT trap", /* SIGIOT */
291: "EMT trap", /* SIGEMT */
292: "Floating point exception", /* SIGFPE */
293: "Killed", /* SIGKILL */
294: "Bus error", /* SIGBUS */
295: "Segmentation fault", /* SIGSEGV */
296: "Bad system call", /* SIGSYS */
297: "Broken pipe", /* SIGPIPE */
298: "Alarm clock", /* SIGALRM */
299: "Terminated", /* SIGTERM */
300: "User defined signal 1", /* SIGUSR1 */
301: "User defined signal 2", /* SIGUSR2 */
302: "Child exited", /* SIGCLD */
303: "Power-fail restart", /* SIGPWR */
304: "Virtual timer expired", /* SIGVTALRM */
305: "Profiling timer expired", /* SIGPROF */
306: "I/O possible", /* SIGIO */
307: "Window size changes", /* SIGWINDOW */
308: "Stopped (signal)", /* SIGSTOP */
309: "Stopped", /* SIGTSTP */
310: "Continued", /* SIGCONT */
311: "Stopped (tty input)", /* SIGTTIN */
312: "Stopped (tty output)", /* SIGTTOU */
313: "Urgent I/O condition", /* SIGURG */
314: "Remote lock lost (NFS)", /* SIGLOST */
315: "Signal 31", /* reserved */
316: "DIL signal" /* SIGDIL */
317: };
318:
319: int
320: utimes(file, tvp)
321: char *file;
322: struct timeval tvp[2];
323: {
324: struct utimbuf t;
325:
326: t.actime = tvp[0].tv_sec;
327: t.modtime = tvp[1].tv_sec;
328: return(utime(file, &t));
329: }
330:
331:
332: #endif /* __hpux */
1.2 deraadt 333:
334: #if defined(sun) && defined(__svr4__)
335: #include <signal.h>
336:
337: /* turn into bsd signals */
338: void (*
339: signal(s, a)) ()
340: int s;
341: void (*a)();
342: {
343: struct sigaction sa, osa;
344:
1.8 deraadt 345: memset(&sa, 0, sizeof sa);
1.2 deraadt 346: sa.sa_handler = a;
347: sigemptyset(&sa.sa_mask);
348: sa.sa_flags = SA_RESTART;
349:
350: if (sigaction(s, &sa, &osa) == -1)
351: return SIG_ERR;
352: else
353: return osa.sa_handler;
354: }
355:
1.6 millert 356: #endif
357:
358: #ifndef BSD4_4
359: #ifdef __STDC__
360: #include <stdarg.h>
361: #else
362: #include <varargs.h>
363: #endif
364:
365: #ifdef _IOSTRG
366: #define STRFLAG (_IOSTRG|_IOWRT) /* no _IOWRT: avoid stdio bug */
367: #else
368: #define STRFLAG (_IOREAD) /* XXX: Assume svr4 stdio */
369: #endif
370:
371: int
372: vsnprintf(s, n, fmt, args)
373: char *s;
374: size_t n;
375: const char *fmt;
376: va_list args;
377: {
378: FILE fakebuf;
379:
380: fakebuf._flag = STRFLAG;
381: /*
382: * Some os's are char * _ptr, others are unsigned char *_ptr...
383: * We cast to void * to make everyone happy.
384: */
385: fakebuf._ptr = (void *) s;
386: fakebuf._cnt = n-1;
387: fakebuf._file = -1;
388: _doprnt(fmt, args, &fakebuf);
389: fakebuf._cnt++;
390: putc('\0', &fakebuf);
391: if (fakebuf._cnt<0)
392: fakebuf._cnt = 0;
393: return (n-fakebuf._cnt-1);
394: }
395:
396: int
397: #ifdef __STDC__
398: snprintf(char *s, size_t n, const char *fmt, ...)
399: #else
400: snprintf(va_alist)
401: va_dcl
402: #endif
403: {
404: va_list ap;
405: int rv;
406: #ifdef __STDC__
407: va_start(ap, fmt);
408: #else
409: char *s;
410: size_t n;
411: const char *fmt;
412:
413: va_start(ap);
414:
415: s = va_arg(ap, char *);
416: n = va_arg(ap, size_t);
417: fmt = va_arg(ap, const char *);
418: #endif
419: rv = vsnprintf(s, n, fmt, ap);
420: va_end(ap);
421: return rv;
1.11 espie 422: }
423: #endif
424:
425: #ifdef NEED_STRSTR
426: char *
427: strstr(string, substring)
428: const char *string; /* String to search. */
429: const char *substring; /* Substring to find in string */
430: {
431: const char *a, *b;
432:
433: /*
434: * First scan quickly through the two strings looking for a single-
435: * character match. When it's found, then compare the rest of the
436: * substring.
437: */
438:
439: for (b = substring; *string != 0; string += 1) {
440: if (*string != *b)
441: continue;
442: a = string;
443: for (;;) {
444: if (*b == 0)
445: return (char *)string;
446: if (*a++ != *b++)
447: break;
448: }
449: b = substring;
450: }
451: return NULL;
1.12 espie 452: }
453: #endif
454:
455: #ifdef NEED_FGETLN
456: char *
457: fgetln(stream, len)
458: FILE *stream;
459: size_t *len;
460: {
461: static char *buffer = NULL;
462: static size_t buflen = 0;
463:
464: if (buflen == 0) {
465: buflen = 512;
466: buffer = emalloc(buflen+1);
467: }
468: if (fgets(buffer, buflen+1, stream) == NULL)
469: return NULL;
470: *len = strlen(buffer);
471: while (*len == buflen && buffer[*len-1] != '\n') {
472: buffer = erealloc(buffer, 2*buflen + 1);
473: if (fgets(buffer + buflen, buflen + 1, stream) == NULL)
474: return NULL;
475: *len += strlen(buffer + buflen);
476: buflen *= 2;
477: }
478: return buffer;
1.6 millert 479: }
1.2 deraadt 480: #endif