util.c revision 1.53 1 1.53 sjg /* $NetBSD: util.c,v 1.53 2012/06/04 22:45:05 sjg 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.53 sjg static char rcsid[] = "$NetBSD: util.c,v 1.53 2012/06/04 22:45:05 sjg Exp $";
9 1.15 lukem #else
10 1.12 christos #include <sys/cdefs.h>
11 1.4 christos #ifndef lint
12 1.53 sjg __RCSID("$NetBSD: util.c,v 1.53 2012/06/04 22:45:05 sjg 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.50 sjg #include <signal.h>
22 1.32 wiz
23 1.2 glass #include "make.h"
24 1.1 cgd
25 1.35 ross #if !defined(MAKE_NATIVE) && !defined(HAVE_STRERROR)
26 1.1 cgd extern int errno, sys_nerr;
27 1.1 cgd extern char *sys_errlist[];
28 1.1 cgd
29 1.1 cgd char *
30 1.32 wiz strerror(int e)
31 1.1 cgd {
32 1.1 cgd static char buf[100];
33 1.1 cgd if (e < 0 || e >= sys_nerr) {
34 1.23 christos snprintf(buf, sizeof(buf), "Unknown error %d", e);
35 1.1 cgd return buf;
36 1.1 cgd }
37 1.1 cgd else
38 1.1 cgd return sys_errlist[e];
39 1.1 cgd }
40 1.1 cgd #endif
41 1.1 cgd
42 1.35 ross #if !defined(MAKE_NATIVE) && !defined(HAVE_SETENV)
43 1.46 christos extern char **environ;
44 1.46 christos
45 1.46 christos static char *
46 1.46 christos findenv(const char *name, int *offset)
47 1.1 cgd {
48 1.46 christos size_t i, len;
49 1.46 christos char *p, *q;
50 1.1 cgd
51 1.53 sjg len = strlen(name);
52 1.46 christos for (i = 0; (q = environ[i]); i++) {
53 1.51 mbalmer p = strchr(q, '=');
54 1.53 sjg if (p == NULL || p - q != len)
55 1.46 christos continue;
56 1.53 sjg if (strncmp(name, q, len) == 0) {
57 1.46 christos *offset = i;
58 1.46 christos return q + len + 1;
59 1.46 christos }
60 1.46 christos }
61 1.46 christos *offset = i;
62 1.46 christos return NULL;
63 1.46 christos }
64 1.1 cgd
65 1.52 sjg char *
66 1.52 sjg getenv(const char *name)
67 1.52 sjg {
68 1.52 sjg int offset;
69 1.52 sjg
70 1.52 sjg return(findenv(name, &offset));
71 1.52 sjg }
72 1.52 sjg
73 1.46 christos int
74 1.46 christos unsetenv(const char *name)
75 1.46 christos {
76 1.46 christos char **p;
77 1.46 christos int offset;
78 1.8 christos
79 1.46 christos if (name == NULL || *name == '\0' || strchr(name, '=') != NULL) {
80 1.46 christos errno = EINVAL;
81 1.46 christos return -1;
82 1.46 christos }
83 1.1 cgd
84 1.46 christos while (findenv(name, &offset)) { /* if set multiple times */
85 1.46 christos for (p = &environ[offset];; ++p)
86 1.46 christos if (!(*p = *(p + 1)))
87 1.46 christos break;
88 1.46 christos }
89 1.46 christos return 0;
90 1.46 christos }
91 1.1 cgd
92 1.46 christos int
93 1.46 christos setenv(const char *name, const char *value, int rewrite)
94 1.46 christos {
95 1.46 christos char *c, **newenv;
96 1.46 christos const char *cc;
97 1.46 christos size_t l_value, size;
98 1.46 christos int offset;
99 1.46 christos
100 1.46 christos if (name == NULL || value == NULL) {
101 1.46 christos errno = EINVAL;
102 1.46 christos return -1;
103 1.46 christos }
104 1.1 cgd
105 1.46 christos if (*value == '=') /* no `=' in value */
106 1.46 christos ++value;
107 1.46 christos l_value = strlen(value);
108 1.46 christos
109 1.46 christos /* find if already exists */
110 1.46 christos if ((c = findenv(name, &offset))) {
111 1.46 christos if (!rewrite)
112 1.46 christos return 0;
113 1.46 christos if (strlen(c) >= l_value) /* old larger; copy over */
114 1.46 christos goto copy;
115 1.46 christos } else { /* create new slot */
116 1.46 christos size = sizeof(char *) * (offset + 2);
117 1.52 sjg if (savedEnv == environ) { /* just increase size */
118 1.52 sjg if ((newenv = realloc(savedEnv, size)) == NULL)
119 1.46 christos return -1;
120 1.52 sjg savedEnv = newenv;
121 1.46 christos } else { /* get new space */
122 1.46 christos /*
123 1.46 christos * We don't free here because we don't know if
124 1.46 christos * the first allocation is valid on all OS's
125 1.46 christos */
126 1.52 sjg if ((savedEnv = malloc(size)) == NULL)
127 1.46 christos return -1;
128 1.52 sjg (void)memcpy(savedEnv, environ, size - sizeof(char *));
129 1.46 christos }
130 1.52 sjg environ = savedEnv;
131 1.46 christos environ[offset + 1] = NULL;
132 1.46 christos }
133 1.46 christos for (cc = name; *cc && *cc != '='; ++cc) /* no `=' in name */
134 1.46 christos continue;
135 1.46 christos size = cc - name;
136 1.46 christos /* name + `=' + value */
137 1.46 christos if ((environ[offset] = malloc(size + l_value + 2)) == NULL)
138 1.46 christos return -1;
139 1.46 christos c = environ[offset];
140 1.46 christos (void)memcpy(c, name, size);
141 1.46 christos c += size;
142 1.46 christos *c++ = '=';
143 1.46 christos copy:
144 1.46 christos (void)memcpy(c, value, l_value + 1);
145 1.46 christos return 0;
146 1.46 christos }
147 1.1 cgd
148 1.46 christos #ifdef TEST
149 1.46 christos int
150 1.46 christos main(int argc, char *argv[])
151 1.46 christos {
152 1.46 christos setenv(argv[1], argv[2], 0);
153 1.46 christos printf("%s\n", getenv(argv[1]));
154 1.46 christos unsetenv(argv[1]);
155 1.46 christos printf("%s\n", getenv(argv[1]));
156 1.46 christos return 0;
157 1.46 christos }
158 1.46 christos #endif
159 1.1 cgd
160 1.1 cgd #endif
161 1.1 cgd
162 1.26 christos #if defined(__hpux__) || defined(__hpux)
163 1.26 christos /* strrcpy():
164 1.26 christos * Like strcpy, going backwards and returning the new pointer
165 1.26 christos */
166 1.26 christos static char *
167 1.32 wiz strrcpy(char *ptr, char *str)
168 1.26 christos {
169 1.32 wiz int len = strlen(str);
170 1.26 christos
171 1.26 christos while (len)
172 1.26 christos *--ptr = str[--len];
173 1.26 christos
174 1.26 christos return (ptr);
175 1.26 christos } /* end strrcpy */
176 1.26 christos
177 1.26 christos char *sys_siglist[] = {
178 1.26 christos "Signal 0",
179 1.26 christos "Hangup", /* SIGHUP */
180 1.26 christos "Interrupt", /* SIGINT */
181 1.26 christos "Quit", /* SIGQUIT */
182 1.26 christos "Illegal instruction", /* SIGILL */
183 1.26 christos "Trace/BPT trap", /* SIGTRAP */
184 1.26 christos "IOT trap", /* SIGIOT */
185 1.26 christos "EMT trap", /* SIGEMT */
186 1.26 christos "Floating point exception", /* SIGFPE */
187 1.26 christos "Killed", /* SIGKILL */
188 1.26 christos "Bus error", /* SIGBUS */
189 1.26 christos "Segmentation fault", /* SIGSEGV */
190 1.26 christos "Bad system call", /* SIGSYS */
191 1.26 christos "Broken pipe", /* SIGPIPE */
192 1.26 christos "Alarm clock", /* SIGALRM */
193 1.26 christos "Terminated", /* SIGTERM */
194 1.26 christos "User defined signal 1", /* SIGUSR1 */
195 1.26 christos "User defined signal 2", /* SIGUSR2 */
196 1.26 christos "Child exited", /* SIGCLD */
197 1.26 christos "Power-fail restart", /* SIGPWR */
198 1.26 christos "Virtual timer expired", /* SIGVTALRM */
199 1.26 christos "Profiling timer expired", /* SIGPROF */
200 1.26 christos "I/O possible", /* SIGIO */
201 1.26 christos "Window size changes", /* SIGWINDOW */
202 1.26 christos "Stopped (signal)", /* SIGSTOP */
203 1.26 christos "Stopped", /* SIGTSTP */
204 1.26 christos "Continued", /* SIGCONT */
205 1.26 christos "Stopped (tty input)", /* SIGTTIN */
206 1.26 christos "Stopped (tty output)", /* SIGTTOU */
207 1.26 christos "Urgent I/O condition", /* SIGURG */
208 1.26 christos "Remote lock lost (NFS)", /* SIGLOST */
209 1.26 christos "Signal 31", /* reserved */
210 1.26 christos "DIL signal" /* SIGDIL */
211 1.26 christos };
212 1.26 christos #endif /* __hpux__ || __hpux */
213 1.26 christos
214 1.33 lukem #if defined(__hpux__) || defined(__hpux)
215 1.1 cgd #include <sys/types.h>
216 1.1 cgd #include <sys/syscall.h>
217 1.1 cgd #include <sys/signal.h>
218 1.1 cgd #include <sys/stat.h>
219 1.1 cgd #include <dirent.h>
220 1.1 cgd #include <sys/time.h>
221 1.1 cgd #include <unistd.h>
222 1.1 cgd
223 1.1 cgd int
224 1.44 christos killpg(int pid, int sig)
225 1.1 cgd {
226 1.1 cgd return kill(-pid, sig);
227 1.1 cgd }
228 1.1 cgd
229 1.33 lukem #if !defined(__hpux__) && !defined(__hpux)
230 1.1 cgd void
231 1.32 wiz srandom(long seed)
232 1.1 cgd {
233 1.1 cgd srand48(seed);
234 1.1 cgd }
235 1.1 cgd
236 1.1 cgd long
237 1.32 wiz random(void)
238 1.1 cgd {
239 1.1 cgd return lrand48();
240 1.1 cgd }
241 1.31 christos #endif
242 1.1 cgd
243 1.33 lukem #if !defined(__hpux__) && !defined(__hpux)
244 1.26 christos int
245 1.32 wiz utimes(char *file, struct timeval tvp[2])
246 1.26 christos {
247 1.26 christos struct utimbuf t;
248 1.26 christos
249 1.26 christos t.actime = tvp[0].tv_sec;
250 1.26 christos t.modtime = tvp[1].tv_sec;
251 1.26 christos return(utime(file, &t));
252 1.26 christos }
253 1.31 christos #endif
254 1.26 christos
255 1.1 cgd #if !defined(BSD) && !defined(d_fileno)
256 1.1 cgd # define d_fileno d_ino
257 1.1 cgd #endif
258 1.1 cgd
259 1.1 cgd #ifndef DEV_DEV_COMPARE
260 1.1 cgd # define DEV_DEV_COMPARE(a, b) ((a) == (b))
261 1.1 cgd #endif
262 1.1 cgd #define ISDOT(c) ((c)[0] == '.' && (((c)[1] == '\0') || ((c)[1] == '/')))
263 1.1 cgd #define ISDOTDOT(c) ((c)[0] == '.' && ISDOT(&((c)[1])))
264 1.1 cgd
265 1.32 wiz char *
266 1.32 wiz getwd(char *pathname)
267 1.1 cgd {
268 1.1 cgd DIR *dp;
269 1.1 cgd struct dirent *d;
270 1.1 cgd extern int errno;
271 1.1 cgd
272 1.1 cgd struct stat st_root, st_cur, st_next, st_dotdot;
273 1.1 cgd char pathbuf[MAXPATHLEN], nextpathbuf[MAXPATHLEN * 2];
274 1.1 cgd char *pathptr, *nextpathptr, *cur_name_add;
275 1.1 cgd
276 1.1 cgd /* find the inode of root */
277 1.1 cgd if (stat("/", &st_root) == -1) {
278 1.37 christos (void)sprintf(pathname,
279 1.1 cgd "getwd: Cannot stat \"/\" (%s)", strerror(errno));
280 1.47 dsl return NULL;
281 1.1 cgd }
282 1.1 cgd pathbuf[MAXPATHLEN - 1] = '\0';
283 1.1 cgd pathptr = &pathbuf[MAXPATHLEN - 1];
284 1.1 cgd nextpathbuf[MAXPATHLEN - 1] = '\0';
285 1.1 cgd cur_name_add = nextpathptr = &nextpathbuf[MAXPATHLEN - 1];
286 1.1 cgd
287 1.1 cgd /* find the inode of the current directory */
288 1.1 cgd if (lstat(".", &st_cur) == -1) {
289 1.37 christos (void)sprintf(pathname,
290 1.1 cgd "getwd: Cannot stat \".\" (%s)", strerror(errno));
291 1.47 dsl return NULL;
292 1.1 cgd }
293 1.1 cgd nextpathptr = strrcpy(nextpathptr, "../");
294 1.1 cgd
295 1.1 cgd /* Descend to root */
296 1.1 cgd for (;;) {
297 1.1 cgd
298 1.1 cgd /* look if we found root yet */
299 1.1 cgd if (st_cur.st_ino == st_root.st_ino &&
300 1.1 cgd DEV_DEV_COMPARE(st_cur.st_dev, st_root.st_dev)) {
301 1.37 christos (void)strcpy(pathname, *pathptr != '/' ? "/" : pathptr);
302 1.1 cgd return (pathname);
303 1.1 cgd }
304 1.1 cgd
305 1.1 cgd /* open the parent directory */
306 1.1 cgd if (stat(nextpathptr, &st_dotdot) == -1) {
307 1.37 christos (void)sprintf(pathname,
308 1.1 cgd "getwd: Cannot stat directory \"%s\" (%s)",
309 1.1 cgd nextpathptr, strerror(errno));
310 1.47 dsl return NULL;
311 1.1 cgd }
312 1.1 cgd if ((dp = opendir(nextpathptr)) == NULL) {
313 1.37 christos (void)sprintf(pathname,
314 1.1 cgd "getwd: Cannot open directory \"%s\" (%s)",
315 1.1 cgd nextpathptr, strerror(errno));
316 1.47 dsl return NULL;
317 1.1 cgd }
318 1.1 cgd
319 1.1 cgd /* look in the parent for the entry with the same inode */
320 1.1 cgd if (DEV_DEV_COMPARE(st_dotdot.st_dev, st_cur.st_dev)) {
321 1.1 cgd /* Parent has same device. No need to stat every member */
322 1.8 christos for (d = readdir(dp); d != NULL; d = readdir(dp))
323 1.1 cgd if (d->d_fileno == st_cur.st_ino)
324 1.1 cgd break;
325 1.1 cgd }
326 1.1 cgd else {
327 1.8 christos /*
328 1.8 christos * Parent has a different device. This is a mount point so we
329 1.8 christos * need to stat every member
330 1.1 cgd */
331 1.1 cgd for (d = readdir(dp); d != NULL; d = readdir(dp)) {
332 1.1 cgd if (ISDOT(d->d_name) || ISDOTDOT(d->d_name))
333 1.1 cgd continue;
334 1.37 christos (void)strcpy(cur_name_add, d->d_name);
335 1.1 cgd if (lstat(nextpathptr, &st_next) == -1) {
336 1.37 christos (void)sprintf(pathname,
337 1.23 christos "getwd: Cannot stat \"%s\" (%s)",
338 1.23 christos d->d_name, strerror(errno));
339 1.37 christos (void)closedir(dp);
340 1.47 dsl return NULL;
341 1.1 cgd }
342 1.1 cgd /* check if we found it yet */
343 1.1 cgd if (st_next.st_ino == st_cur.st_ino &&
344 1.8 christos DEV_DEV_COMPARE(st_next.st_dev, st_cur.st_dev))
345 1.1 cgd break;
346 1.1 cgd }
347 1.1 cgd }
348 1.1 cgd if (d == NULL) {
349 1.37 christos (void)sprintf(pathname,
350 1.23 christos "getwd: Cannot find \".\" in \"..\"");
351 1.37 christos (void)closedir(dp);
352 1.47 dsl return NULL;
353 1.1 cgd }
354 1.1 cgd st_cur = st_dotdot;
355 1.1 cgd pathptr = strrcpy(pathptr, d->d_name);
356 1.1 cgd pathptr = strrcpy(pathptr, "/");
357 1.1 cgd nextpathptr = strrcpy(nextpathptr, "../");
358 1.37 christos (void)closedir(dp);
359 1.1 cgd *cur_name_add = '\0';
360 1.1 cgd }
361 1.1 cgd } /* end getwd */
362 1.1 cgd #endif /* __hpux */
363 1.5 christos
364 1.50 sjg /* force posix signals */
365 1.5 christos void (*
366 1.50 sjg bmake_signal(int s, void (*a)(int)))(int)
367 1.5 christos {
368 1.5 christos struct sigaction sa, osa;
369 1.5 christos
370 1.5 christos sa.sa_handler = a;
371 1.5 christos sigemptyset(&sa.sa_mask);
372 1.5 christos sa.sa_flags = SA_RESTART;
373 1.5 christos
374 1.5 christos if (sigaction(s, &sa, &osa) == -1)
375 1.5 christos return SIG_ERR;
376 1.5 christos else
377 1.5 christos return osa.sa_handler;
378 1.5 christos }
379 1.10 christos
380 1.35 ross #if !defined(MAKE_NATIVE) && !defined(HAVE_VSNPRINTF)
381 1.10 christos #include <stdarg.h>
382 1.10 christos
383 1.25 drochner #if !defined(__osf__)
384 1.10 christos #ifdef _IOSTRG
385 1.10 christos #define STRFLAG (_IOSTRG|_IOWRT) /* no _IOWRT: avoid stdio bug */
386 1.10 christos #else
387 1.11 christos #if 0
388 1.10 christos #define STRFLAG (_IOREAD) /* XXX: Assume svr4 stdio */
389 1.10 christos #endif
390 1.25 drochner #endif /* _IOSTRG */
391 1.25 drochner #endif /* __osf__ */
392 1.10 christos
393 1.10 christos int
394 1.32 wiz vsnprintf(char *s, size_t n, const char *fmt, va_list args)
395 1.10 christos {
396 1.19 christos #ifdef STRFLAG
397 1.10 christos FILE fakebuf;
398 1.10 christos
399 1.10 christos fakebuf._flag = STRFLAG;
400 1.10 christos /*
401 1.10 christos * Some os's are char * _ptr, others are unsigned char *_ptr...
402 1.10 christos * We cast to void * to make everyone happy.
403 1.10 christos */
404 1.39 christos fakebuf._ptr = (void *)s;
405 1.10 christos fakebuf._cnt = n-1;
406 1.10 christos fakebuf._file = -1;
407 1.10 christos _doprnt(fmt, args, &fakebuf);
408 1.10 christos fakebuf._cnt++;
409 1.10 christos putc('\0', &fakebuf);
410 1.10 christos if (fakebuf._cnt<0)
411 1.10 christos fakebuf._cnt = 0;
412 1.10 christos return (n-fakebuf._cnt-1);
413 1.11 christos #else
414 1.37 christos (void)vsprintf(s, fmt, args);
415 1.11 christos return strlen(s);
416 1.11 christos #endif
417 1.10 christos }
418 1.10 christos
419 1.10 christos int
420 1.10 christos snprintf(char *s, size_t n, const char *fmt, ...)
421 1.10 christos {
422 1.10 christos va_list ap;
423 1.10 christos int rv;
424 1.32 wiz
425 1.10 christos va_start(ap, fmt);
426 1.10 christos rv = vsnprintf(s, n, fmt, ap);
427 1.10 christos va_end(ap);
428 1.10 christos return rv;
429 1.16 christos }
430 1.16 christos
431 1.35 ross #if !defined(MAKE_NATIVE) && !defined(HAVE_STRFTIME)
432 1.29 reinoud size_t
433 1.32 wiz strftime(char *buf, size_t len, const char *fmt, 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.48 dholland s = snprintf(buf, len, "%02d", tm->tm_mday);
477 1.16 christos break;
478 1.16 christos case 'Y':
479 1.48 dholland s = snprintf(buf, len, "%d", 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