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