1 /* $NetBSD: util.c,v 1.64 2020/10/06 21:51:33 rillig Exp $ */
4 * Missing stuff from OS's
6 * $Id: util.c,v 1.39 2020/10/10 19:42:02 sjg Exp $
16 MAKE_RCSID("$NetBSD: util.c,v 1.64 2020/10/06 21:51:33 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);
31 return sys_errlist[e];
35 #if !defined(HAVE_GETENV) || !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV)
36 extern char **environ;
39 findenv(const char *name, int *offset)
45 for (i = 0; (q = environ[i]); i++) {
47 if (p == NULL || p - q != len)
49 if (strncmp(name, q, len) == 0) {
59 getenv(const char *name)
63 return findenv(name, &offset);
67 unsetenv(const char *name)
72 if (name == NULL || *name == '\0' || strchr(name, '=') != NULL) {
77 while (findenv(name, &offset)) { /* if set multiple times */
78 for (p = &environ[offset];; ++p)
86 setenv(const char *name, const char *value, int rewrite)
93 if (name == NULL || value == NULL) {
98 if (*value == '=') /* no `=' in value */
100 l_value = strlen(value);
102 /* find if already exists */
103 if ((c = findenv(name, &offset))) {
106 if (strlen(c) >= l_value) /* old larger; copy over */
108 } else { /* create new slot */
109 size = sizeof(char *) * (offset + 2);
110 if (savedEnv == environ) { /* just increase size */
111 if ((newenv = realloc(savedEnv, size)) == NULL)
114 } else { /* get new space */
116 * We don't free here because we don't know if
117 * the first allocation is valid on all OS's
119 if ((savedEnv = malloc(size)) == NULL)
121 (void)memcpy(savedEnv, environ, size - sizeof(char *));
124 environ[offset + 1] = NULL;
126 for (cc = name; *cc && *cc != '='; ++cc) /* no `=' in name */
129 /* name + `=' + value */
130 if ((environ[offset] = malloc(size + l_value + 2)) == NULL)
133 (void)memcpy(c, name, size);
137 (void)memcpy(c, value, l_value + 1);
143 main(int argc, char *argv[])
145 setenv(argv[1], argv[2], 0);
146 printf("%s\n", getenv(argv[1]));
148 printf("%s\n", getenv(argv[1]));
156 #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);
172 char *sys_siglist[] = {
174 "Hangup", /* SIGHUP */
175 "Interrupt", /* SIGINT */
176 "Quit", /* SIGQUIT */
177 "Illegal instruction", /* SIGILL */
178 "Trace/BPT trap", /* SIGTRAP */
179 "IOT trap", /* SIGIOT */
180 "EMT trap", /* SIGEMT */
181 "Floating point exception", /* SIGFPE */
182 "Killed", /* SIGKILL */
183 "Bus error", /* SIGBUS */
184 "Segmentation fault", /* SIGSEGV */
185 "Bad system call", /* SIGSYS */
186 "Broken pipe", /* SIGPIPE */
187 "Alarm clock", /* SIGALRM */
188 "Terminated", /* SIGTERM */
189 "User defined signal 1", /* SIGUSR1 */
190 "User defined signal 2", /* SIGUSR2 */
191 "Child exited", /* SIGCLD */
192 "Power-fail restart", /* SIGPWR */
193 "Virtual timer expired", /* SIGVTALRM */
194 "Profiling timer expired", /* SIGPROF */
195 "I/O possible", /* SIGIO */
196 "Window size changes", /* SIGWINDOW */
197 "Stopped (signal)", /* SIGSTOP */
198 "Stopped", /* SIGTSTP */
199 "Continued", /* SIGCONT */
200 "Stopped (tty input)", /* SIGTTIN */
201 "Stopped (tty output)", /* SIGTTOU */
202 "Urgent I/O condition", /* SIGURG */
203 "Remote lock lost (NFS)", /* SIGLOST */
204 "Signal 31", /* reserved */
205 "DIL signal" /* SIGDIL */
207 #endif /* __hpux__ || __hpux */
209 #if defined(__hpux__) || defined(__hpux)
210 #include <sys/types.h>
211 #include <sys/syscall.h>
212 #include <sys/signal.h>
213 #include <sys/stat.h>
215 #include <sys/time.h>
219 killpg(int pid, int sig)
221 return kill(-pid, sig);
224 #if !defined(BSD) && !defined(d_fileno)
225 # define d_fileno d_ino
228 #ifndef DEV_DEV_COMPARE
229 # define DEV_DEV_COMPARE(a, b) ((a) == (b))
231 #define ISDOT(c) ((c)[0] == '.' && (((c)[1] == '\0') || ((c)[1] == '/')))
232 #define ISDOTDOT(c) ((c)[0] == '.' && ISDOT(&((c)[1])))
235 getwd(char *pathname)
241 struct stat st_root, st_cur, st_next, st_dotdot;
242 char pathbuf[MAXPATHLEN], nextpathbuf[MAXPATHLEN * 2];
243 char *pathptr, *nextpathptr, *cur_name_add;
245 /* find the inode of root */
246 if (stat("/", &st_root) == -1) {
247 (void)sprintf(pathname,
248 "getwd: Cannot stat \"/\" (%s)", strerror(errno));
251 pathbuf[MAXPATHLEN - 1] = '\0';
252 pathptr = &pathbuf[MAXPATHLEN - 1];
253 nextpathbuf[MAXPATHLEN - 1] = '\0';
254 cur_name_add = nextpathptr = &nextpathbuf[MAXPATHLEN - 1];
256 /* find the inode of the current directory */
257 if (lstat(".", &st_cur) == -1) {
258 (void)sprintf(pathname,
259 "getwd: Cannot stat \".\" (%s)", strerror(errno));
262 nextpathptr = strrcpy(nextpathptr, "../");
264 /* Descend to root */
267 /* look if we found root yet */
268 if (st_cur.st_ino == st_root.st_ino &&
269 DEV_DEV_COMPARE(st_cur.st_dev, st_root.st_dev)) {
270 (void)strcpy(pathname, *pathptr != '/' ? "/" : pathptr);
274 /* open the parent directory */
275 if (stat(nextpathptr, &st_dotdot) == -1) {
276 (void)sprintf(pathname,
277 "getwd: Cannot stat directory \"%s\" (%s)",
278 nextpathptr, strerror(errno));
281 if ((dp = opendir(nextpathptr)) == NULL) {
282 (void)sprintf(pathname,
283 "getwd: Cannot open directory \"%s\" (%s)",
284 nextpathptr, strerror(errno));
288 /* look in the parent for the entry with the same inode */
289 if (DEV_DEV_COMPARE(st_dotdot.st_dev, st_cur.st_dev)) {
290 /* Parent has same device. No need to stat every member */
291 for (d = readdir(dp); d != NULL; d = readdir(dp))
292 if (d->d_fileno == st_cur.st_ino)
297 * Parent has a different device. This is a mount point so we
298 * need to stat every member
300 for (d = readdir(dp); d != NULL; d = readdir(dp)) {
301 if (ISDOT(d->d_name) || ISDOTDOT(d->d_name))
303 (void)strcpy(cur_name_add, d->d_name);
304 if (lstat(nextpathptr, &st_next) == -1) {
305 (void)sprintf(pathname,
306 "getwd: Cannot stat \"%s\" (%s)",
307 d->d_name, strerror(errno));
311 /* check if we found it yet */
312 if (st_next.st_ino == st_cur.st_ino &&
313 DEV_DEV_COMPARE(st_next.st_dev, st_cur.st_dev))
318 (void)sprintf(pathname,
319 "getwd: Cannot find \".\" in \"..\"");
324 pathptr = strrcpy(pathptr, d->d_name);
325 pathptr = strrcpy(pathptr, "/");
326 nextpathptr = strrcpy(nextpathptr, "../");
328 *cur_name_add = '\0';
334 #if !defined(HAVE_GETCWD)
344 /* force posix signals */
346 bmake_signal(int s, SignalProc a)
348 struct sigaction sa, osa;
351 sigemptyset(&sa.sa_mask);
352 sa.sa_flags = SA_RESTART;
354 if (sigaction(s, &sa, &osa) == -1)
357 return osa.sa_handler;
360 #if !defined(HAVE_VSNPRINTF) || !defined(HAVE_VASPRINTF)
364 #if !defined(HAVE_VSNPRINTF)
365 #if !defined(__osf__)
367 #define STRFLAG (_IOSTRG|_IOWRT) /* no _IOWRT: avoid stdio bug */
370 #define STRFLAG (_IOREAD) /* XXX: Assume svr4 stdio */
376 vsnprintf(char *s, size_t n, const char *fmt, va_list args)
381 fakebuf._flag = STRFLAG;
383 * Some os's are char * _ptr, others are unsigned char *_ptr...
384 * We cast to void * to make everyone happy.
386 fakebuf._ptr = (void *)s;
389 _doprnt(fmt, args, &fakebuf);
391 putc('\0', &fakebuf);
394 return n-fakebuf._cnt-1;
396 #ifndef _PATH_DEVNULL
397 # define _PATH_DEVNULL "/dev/null"
400 * Rats... we don't want to clobber anything...
401 * do a printf to /dev/null to see how much space we need.
404 int need = 0; /* XXX what's a useful error return? */
407 nullfp = fopen(_PATH_DEVNULL, "w");
409 need = vfprintf(nullfp, fmt, args);
411 (void)vsprintf(s, fmt, args);
418 #if !defined(HAVE_SNPRINTF)
420 snprintf(char *s, size_t n, const char *fmt, ...)
426 rv = vsnprintf(s, n, fmt, ap);
432 #if !defined(HAVE_STRFTIME)
434 strftime(char *buf, size_t len, const char *fmt, const struct tm *tm)
436 static char months[][4] = {
437 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
438 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
456 if (len == 0) return buf - b;
463 s = snprintf(buf, len, "%d", tm->tm_hour);
466 s = snprintf(buf, len, "%02d", tm->tm_min);
469 s = snprintf(buf, len, "%02d", tm->tm_sec);
472 if (tm->tm_mon >= 12)
474 s = snprintf(buf, len, "%s", months[tm->tm_mon]);
477 s = snprintf(buf, len, "%02d", tm->tm_mday);
480 s = snprintf(buf, len, "%d", 1900 + tm->tm_year);
483 s = snprintf(buf, len, "Unsupported format %c",
494 #if !defined(HAVE_KILLPG)
495 #if !defined(__hpux__) && !defined(__hpux)
497 killpg(int pid, int sig)
499 return kill(-pid, sig);
504 #if !defined(HAVE_WARNX)
506 vwarnx(const char *fmt, va_list args)
508 fprintf(stderr, "%s: ", progname);
510 vfprintf(stderr, fmt, args);
511 fprintf(stderr, ": ");
516 #if !defined(HAVE_WARN)
518 vwarn(const char *fmt, va_list args)
521 fprintf(stderr, "%s\n", strerror(errno));
525 #if !defined(HAVE_ERR)
527 verr(int eval, const char *fmt, va_list args)
534 #if !defined(HAVE_ERRX)
536 verrx(int eval, const char *fmt, va_list args)
543 #if !defined(HAVE_ERR)
545 err(int eval, const char *fmt, ...)
555 #if !defined(HAVE_ERRX)
557 errx(int eval, const char *fmt, ...)
562 verrx(eval, fmt, ap);
567 #if !defined(HAVE_WARN)
569 warn(const char *fmt, ...)
579 #if !defined(HAVE_WARNX)
581 warnx(const char *fmt, ...)