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