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