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