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