1 /* $NetBSD: util.c,v 1.68 2020/11/16 18:29:49 rillig Exp $ */
4 * Missing stuff from OS's
6 * $Id: util.c,v 1.41 2020/11/18 03:58:32 sjg Exp $
16 MAKE_RCSID("$NetBSD: util.c,v 1.68 2020/11/16 18:29:49 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)
157 * Like strcpy, going backwards and returning the new pointer
160 strrcpy(char *ptr, char *str)
162 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 /* force posix signals */
344 bmake_signal(int s, SignalProc a)
346 struct sigaction sa, osa;
349 sigemptyset(&sa.sa_mask);
350 sa.sa_flags = SA_RESTART;
352 if (sigaction(s, &sa, &osa) == -1)
355 return osa.sa_handler;
358 #if !defined(HAVE_VSNPRINTF) || !defined(HAVE_VASPRINTF)
362 #if !defined(HAVE_VSNPRINTF)
363 #if !defined(__osf__)
365 #define STRFLAG (_IOSTRG|_IOWRT) /* no _IOWRT: avoid stdio bug */
368 #define STRFLAG (_IOREAD) /* XXX: Assume svr4 stdio */
374 vsnprintf(char *s, size_t n, const char *fmt, va_list args)
379 fakebuf._flag = STRFLAG;
381 * Some os's are char * _ptr, others are unsigned char *_ptr...
382 * We cast to void * to make everyone happy.
384 fakebuf._ptr = (void *)s;
385 fakebuf._cnt = n - 1;
387 _doprnt(fmt, args, &fakebuf);
389 putc('\0', &fakebuf);
390 if (fakebuf._cnt < 0)
392 return n - fakebuf._cnt - 1;
394 #ifndef _PATH_DEVNULL
395 # define _PATH_DEVNULL "/dev/null"
398 * Rats... we don't want to clobber anything...
399 * do a printf to /dev/null to see how much space we need.
402 int need = 0; /* XXX what's a useful error return? */
405 nullfp = fopen(_PATH_DEVNULL, "w");
407 need = vfprintf(nullfp, fmt, args);
409 (void)vsprintf(s, fmt, args);
416 #if !defined(HAVE_SNPRINTF)
418 snprintf(char *s, size_t n, const char *fmt, ...)
424 rv = vsnprintf(s, n, fmt, ap);
430 #if !defined(HAVE_STRFTIME)
432 strftime(char *buf, size_t len, const char *fmt, const struct tm *tm)
434 static char months[][4] = {
435 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
436 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
454 if (len == 0) return buf - b;
461 s = snprintf(buf, len, "%d", tm->tm_hour);
464 s = snprintf(buf, len, "%02d", tm->tm_min);
467 s = snprintf(buf, len, "%02d", tm->tm_sec);
470 if (tm->tm_mon >= 12)
472 s = snprintf(buf, len, "%s", months[tm->tm_mon]);
475 s = snprintf(buf, len, "%02d", tm->tm_mday);
478 s = snprintf(buf, len, "%d", 1900 + tm->tm_year);
481 s = snprintf(buf, len, "Unsupported format %c",
492 #if !defined(HAVE_KILLPG)
493 #if !defined(__hpux__) && !defined(__hpux)
495 killpg(int pid, int sig)
497 return kill(-pid, sig);
502 #if !defined(HAVE_WARNX)
504 vwarnx(const char *fmt, va_list args)
506 fprintf(stderr, "%s: ", progname);
508 vfprintf(stderr, fmt, args);
509 fprintf(stderr, ": ");
514 #if !defined(HAVE_WARN)
516 vwarn(const char *fmt, va_list args)
519 fprintf(stderr, "%s\n", strerror(errno));
523 #if !defined(HAVE_ERR)
525 verr(int eval, const char *fmt, va_list args)
532 #if !defined(HAVE_ERRX)
534 verrx(int eval, const char *fmt, va_list args)
541 #if !defined(HAVE_ERR)
543 err(int eval, const char *fmt, ...)
553 #if !defined(HAVE_ERRX)
555 errx(int eval, const char *fmt, ...)
560 verrx(eval, fmt, ap);
565 #if !defined(HAVE_WARN)
567 warn(const char *fmt, ...)
577 #if !defined(HAVE_WARNX)
579 warnx(const char *fmt, ...)