1 /* $NetBSD: util.c,v 1.78 2021/12/15 12:58:01 rillig Exp $ */
4 * Missing stuff from OS's
6 * $Id: util.c,v 1.50 2021/12/21 18:47:24 sjg Exp $
16 MAKE_RCSID("$NetBSD: util.c,v 1.78 2021/12/15 12:58:01 rillig Exp $");
18 #if !defined(MAKE_NATIVE) && !defined(HAVE_STRERROR)
19 extern int errno, sys_nerr;
20 extern char *sys_errlist[];
26 if (e < 0 || e >= sys_nerr) {
27 snprintf(buf, sizeof buf, "Unknown error %d", e);
30 return sys_errlist[e];
34 #if !defined(HAVE_GETENV) || !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV)
35 extern char **environ;
38 findenv(const char *name, int *offset)
44 for (i = 0; (q = environ[i]); i++) {
46 if (p == NULL || p - q != len)
48 if (strncmp(name, q, len) == 0) {
58 getenv(const char *name)
62 return findenv(name, &offset);
66 unsetenv(const char *name)
71 if (name == NULL || *name == '\0' || strchr(name, '=') != NULL) {
76 while (findenv(name, &offset)) { /* if set multiple times */
77 for (p = &environ[offset];; p++)
85 setenv(const char *name, const char *value, int rewrite)
92 if (name == NULL || value == NULL) {
97 if (*value == '=') /* no `=' in value */
99 l_value = strlen(value);
101 /* find if already exists */
102 if ((c = findenv(name, &offset))) {
105 if (strlen(c) >= l_value) /* old larger; copy over */
107 } else { /* create new slot */
108 size = sizeof(char *) * (offset + 2);
109 if (savedEnv == environ) { /* just increase size */
110 if ((newenv = realloc(savedEnv, size)) == NULL)
113 } else { /* get new space */
115 * We don't free here because we don't know if
116 * the first allocation is valid on all OS's
118 if ((savedEnv = malloc(size)) == NULL)
120 (void)memcpy(savedEnv, environ, size - sizeof(char *));
123 environ[offset + 1] = NULL;
125 for (cc = name; *cc && *cc != '='; cc++) /* no `=' in name */
128 /* name + `=' + value */
129 if ((environ[offset] = malloc(size + l_value + 2)) == NULL)
132 (void)memcpy(c, name, size);
136 (void)memcpy(c, value, l_value + 1);
142 main(int argc, char *argv[])
144 setenv(argv[1], argv[2], 0);
145 printf("%s\n", getenv(argv[1]));
147 printf("%s\n", getenv(argv[1]));
155 #if defined(__hpux__) || defined(__hpux)
158 * Like strcpy, going backwards and returning the new pointer
161 strrcpy(char *ptr, char *str)
163 int len = strlen(str);
171 char *sys_siglist[] = {
173 "Hangup", /* SIGHUP */
174 "Interrupt", /* SIGINT */
175 "Quit", /* SIGQUIT */
176 "Illegal instruction", /* SIGILL */
177 "Trace/BPT trap", /* SIGTRAP */
178 "IOT trap", /* SIGIOT */
179 "EMT trap", /* SIGEMT */
180 "Floating point exception", /* SIGFPE */
181 "Killed", /* SIGKILL */
182 "Bus error", /* SIGBUS */
183 "Segmentation fault", /* SIGSEGV */
184 "Bad system call", /* SIGSYS */
185 "Broken pipe", /* SIGPIPE */
186 "Alarm clock", /* SIGALRM */
187 "Terminated", /* SIGTERM */
188 "User defined signal 1", /* SIGUSR1 */
189 "User defined signal 2", /* SIGUSR2 */
190 "Child exited", /* SIGCLD */
191 "Power-fail restart", /* SIGPWR */
192 "Virtual timer expired", /* SIGVTALRM */
193 "Profiling timer expired", /* SIGPROF */
194 "I/O possible", /* SIGIO */
195 "Window size changes", /* SIGWINDOW */
196 "Stopped (signal)", /* SIGSTOP */
197 "Stopped", /* SIGTSTP */
198 "Continued", /* SIGCONT */
199 "Stopped (tty input)", /* SIGTTIN */
200 "Stopped (tty output)", /* SIGTTOU */
201 "Urgent I/O condition", /* SIGURG */
202 "Remote lock lost (NFS)", /* SIGLOST */
203 "Signal 31", /* reserved */
204 "DIL signal" /* SIGDIL */
206 #endif /* __hpux__ || __hpux */
208 #if defined(__hpux__) || defined(__hpux)
209 #include <sys/types.h>
210 #include <sys/syscall.h>
211 #include <sys/signal.h>
212 #include <sys/stat.h>
214 #include <sys/time.h>
218 killpg(int pid, int sig)
220 return kill(-pid, sig);
223 #if !defined(BSD) && !defined(d_fileno)
224 # define d_fileno d_ino
227 #ifndef DEV_DEV_COMPARE
228 # define DEV_DEV_COMPARE(a, b) ((a) == (b))
230 #define ISDOT(c) ((c)[0] == '.' && (((c)[1] == '\0') || ((c)[1] == '/')))
231 #define ISDOTDOT(c) ((c)[0] == '.' && ISDOT(&((c)[1])))
234 getwd(char *pathname)
240 struct stat st_root, st_cur, st_next, st_dotdot;
241 char pathbuf[MAXPATHLEN], nextpathbuf[MAXPATHLEN * 2];
242 char *pathptr, *nextpathptr, *cur_name_add;
244 /* find the inode of root */
245 if (stat("/", &st_root) == -1) {
246 (void)sprintf(pathname,
247 "getwd: Cannot stat \"/\" (%s)", strerror(errno));
250 pathbuf[MAXPATHLEN - 1] = '\0';
251 pathptr = &pathbuf[MAXPATHLEN - 1];
252 nextpathbuf[MAXPATHLEN - 1] = '\0';
253 cur_name_add = nextpathptr = &nextpathbuf[MAXPATHLEN - 1];
255 /* find the inode of the current directory */
256 if (lstat(".", &st_cur) == -1) {
257 (void)sprintf(pathname,
258 "getwd: Cannot stat \".\" (%s)", strerror(errno));
261 nextpathptr = strrcpy(nextpathptr, "../");
263 /* Descend to root */
266 /* look if we found root yet */
267 if (st_cur.st_ino == st_root.st_ino &&
268 DEV_DEV_COMPARE(st_cur.st_dev, st_root.st_dev)) {
269 (void)strcpy(pathname, *pathptr != '/' ? "/" : pathptr);
273 /* open the parent directory */
274 if (stat(nextpathptr, &st_dotdot) == -1) {
275 (void)sprintf(pathname,
276 "getwd: Cannot stat directory \"%s\" (%s)",
277 nextpathptr, strerror(errno));
280 if ((dp = opendir(nextpathptr)) == NULL) {
281 (void)sprintf(pathname,
282 "getwd: Cannot open directory \"%s\" (%s)",
283 nextpathptr, strerror(errno));
287 /* look in the parent for the entry with the same inode */
288 if (DEV_DEV_COMPARE(st_dotdot.st_dev, st_cur.st_dev)) {
289 /* Parent has same device. No need to stat every member */
290 for (d = readdir(dp); d != NULL; d = readdir(dp))
291 if (d->d_fileno == st_cur.st_ino)
295 * Parent has a different device. This is a mount point so we
296 * need to stat every member
298 for (d = readdir(dp); d != NULL; d = readdir(dp)) {
299 if (ISDOT(d->d_name) || ISDOTDOT(d->d_name))
301 (void)strcpy(cur_name_add, d->d_name);
302 if (lstat(nextpathptr, &st_next) == -1) {
303 (void)sprintf(pathname,
304 "getwd: Cannot stat \"%s\" (%s)",
305 d->d_name, strerror(errno));
309 /* check if we found it yet */
310 if (st_next.st_ino == st_cur.st_ino &&
311 DEV_DEV_COMPARE(st_next.st_dev, st_cur.st_dev))
316 (void)sprintf(pathname,
317 "getwd: Cannot find \".\" in \"..\"");
322 pathptr = strrcpy(pathptr, d->d_name);
323 pathptr = strrcpy(pathptr, "/");
324 nextpathptr = strrcpy(nextpathptr, "../");
326 *cur_name_add = '\0';
332 #if !defined(HAVE_GETCWD)
342 #if !defined(HAVE_SIGACTION)
346 /* force posix signals */
348 bmake_signal(int s, SignalProc a)
350 struct sigaction sa, osa;
353 sigemptyset(&sa.sa_mask);
354 sa.sa_flags = SA_RESTART;
356 if (sigaction(s, &sa, &osa) == -1)
359 return osa.sa_handler;
362 #if !defined(HAVE_VSNPRINTF) || !defined(HAVE_VASPRINTF)
366 #if !defined(HAVE_VSNPRINTF)
367 #if !defined(__osf__)
369 #define STRFLAG (_IOSTRG|_IOWRT) /* no _IOWRT: avoid stdio bug */
372 #define STRFLAG (_IOREAD) /* XXX: Assume svr4 stdio */
378 vsnprintf(char *s, size_t n, const char *fmt, va_list args)
383 fakebuf._flag = STRFLAG;
385 * Some os's are char * _ptr, others are unsigned char *_ptr...
386 * We cast to void * to make everyone happy.
388 fakebuf._ptr = (void *)s;
389 fakebuf._cnt = n - 1;
391 _doprnt(fmt, args, &fakebuf);
393 putc('\0', &fakebuf);
394 if (fakebuf._cnt < 0)
396 return n - fakebuf._cnt - 1;
398 #ifndef _PATH_DEVNULL
399 # define _PATH_DEVNULL "/dev/null"
402 * Rats... we don't want to clobber anything...
403 * do a printf to /dev/null to see how much space we need.
406 int need = 0; /* XXX what's a useful error return? */
409 nullfp = fopen(_PATH_DEVNULL, "w");
411 need = vfprintf(nullfp, fmt, args);
413 (void)vsprintf(s, fmt, args);
420 #if !defined(HAVE_SNPRINTF)
422 snprintf(char *s, size_t n, const char *fmt, ...)
428 rv = vsnprintf(s, n, fmt, ap);
434 #if !defined(HAVE_STRFTIME)
436 strftime(char *buf, size_t len, const char *fmt, const struct tm *tm)
438 static char months[][4] = {
439 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
440 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
458 if (len == 0) return buf - b;
465 s = snprintf(buf, len, "%d", tm->tm_hour);
468 s = snprintf(buf, len, "%02d", tm->tm_min);
471 s = snprintf(buf, len, "%02d", tm->tm_sec);
474 if (tm->tm_mon >= 12)
476 s = snprintf(buf, len, "%s", months[tm->tm_mon]);
479 s = snprintf(buf, len, "%02d", tm->tm_mday);
482 s = snprintf(buf, len, "%d", 1900 + tm->tm_year);
485 s = snprintf(buf, len, "Unsupported format %c",
496 #if !defined(HAVE_KILLPG)
497 #if !defined(__hpux__) && !defined(__hpux)
499 killpg(int pid, int sig)
501 return kill(-pid, sig);
506 #if !defined(HAVE_WARNX)
508 vwarnx(const char *fmt, va_list args)
510 fprintf(stderr, "%s: ", progname);
512 vfprintf(stderr, fmt, args);
513 fprintf(stderr, ": ");
518 #if !defined(HAVE_WARN)
520 vwarn(const char *fmt, va_list args)
523 fprintf(stderr, "%s\n", strerror(errno));
527 #if !defined(HAVE_ERR)
529 verr(int eval, const char *fmt, va_list args)
536 #if !defined(HAVE_ERRX)
538 verrx(int eval, const char *fmt, va_list args)
545 #if !defined(HAVE_ERR)
547 err(int eval, const char *fmt, ...)
557 #if !defined(HAVE_ERRX)
559 errx(int eval, const char *fmt, ...)
564 verrx(eval, fmt, ap);
569 #if !defined(HAVE_WARN)
571 warn(const char *fmt, ...)
581 #if !defined(HAVE_WARNX)
583 warnx(const char *fmt, ...)
593 #ifdef HAVE_INTTYPES_H
594 #include <inttypes.h>
595 #elif defined(HAVE_STDINT_H)
603 # ifdef HAVE_LONG_LONG_INT
604 # define NUM_TYPE long long
605 # elif defined(_INT64_T_DECLARED) || defined(int64_t)
606 # define NUM_TYPE int64_t
611 #if !defined(HAVE_STRTOLL)
613 #define _FUNCNAME strtoll
614 #define __INT NUM_TYPE
618 # define __INT_MIN LLONG_MIN
619 # define __INT_MAX LLONG_MAX
620 #elif defined(INT64_MAX)
621 # define __INT_MIN INT64_MIN
622 # define __INT_MAX INT64_MAX
625 # define _DIAGASSERT(e)
628 # define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))
635 #if !defined(HAVE_STRTOL)
637 #define _FUNCNAME strtol
641 #define __INT_MIN LONG_MIN
642 #define __INT_MAX LONG_MAX
644 # define _DIAGASSERT(e)
647 # define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))
652 #if !defined(HAVE_STRTOUL)
654 #define _FUNCNAME strtoul
655 #define __INT unsigned long
659 #define __INT_MAX ULONG_MAX
661 # define _DIAGASSERT(e)
664 # define __UNCONST(a) ((void *)(unsigned long)(const void *)(a))