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