1 /* $NetBSD: util.c,v 1.76 2021/02/03 08:00:36 rillig Exp $ */
4 * Missing stuff from OS's
6 * $Id: util.c,v 1.46 2021/02/05 20:02:29 sjg Exp $
16 MAKE_RCSID("$NetBSD: util.c,v 1.76 2021/02/03 08:00:36 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);
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)
296 * Parent has a different device. This is a mount point so we
297 * need to stat every member
299 for (d = readdir(dp); d != NULL; d = readdir(dp)) {
300 if (ISDOT(d->d_name) || ISDOTDOT(d->d_name))
302 (void)strcpy(cur_name_add, d->d_name);
303 if (lstat(nextpathptr, &st_next) == -1) {
304 (void)sprintf(pathname,
305 "getwd: Cannot stat \"%s\" (%s)",
306 d->d_name, strerror(errno));
310 /* check if we found it yet */
311 if (st_next.st_ino == st_cur.st_ino &&
312 DEV_DEV_COMPARE(st_next.st_dev, st_cur.st_dev))
317 (void)sprintf(pathname,
318 "getwd: Cannot find \".\" in \"..\"");
323 pathptr = strrcpy(pathptr, d->d_name);
324 pathptr = strrcpy(pathptr, "/");
325 nextpathptr = strrcpy(nextpathptr, "../");
327 *cur_name_add = '\0';
333 #if !defined(HAVE_GETCWD)
343 /* force posix signals */
345 bmake_signal(int s, SignalProc a)
347 struct sigaction sa, osa;
350 sigemptyset(&sa.sa_mask);
351 sa.sa_flags = SA_RESTART;
353 if (sigaction(s, &sa, &osa) == -1)
356 return osa.sa_handler;
359 #if !defined(HAVE_VSNPRINTF) || !defined(HAVE_VASPRINTF)
363 #if !defined(HAVE_VSNPRINTF)
364 #if !defined(__osf__)
366 #define STRFLAG (_IOSTRG|_IOWRT) /* no _IOWRT: avoid stdio bug */
369 #define STRFLAG (_IOREAD) /* XXX: Assume svr4 stdio */
375 vsnprintf(char *s, size_t n, const char *fmt, va_list args)
380 fakebuf._flag = STRFLAG;
382 * Some os's are char * _ptr, others are unsigned char *_ptr...
383 * We cast to void * to make everyone happy.
385 fakebuf._ptr = (void *)s;
386 fakebuf._cnt = n - 1;
388 _doprnt(fmt, args, &fakebuf);
390 putc('\0', &fakebuf);
391 if (fakebuf._cnt < 0)
393 return n - fakebuf._cnt - 1;
395 #ifndef _PATH_DEVNULL
396 # define _PATH_DEVNULL "/dev/null"
399 * Rats... we don't want to clobber anything...
400 * do a printf to /dev/null to see how much space we need.
403 int need = 0; /* XXX what's a useful error return? */
406 nullfp = fopen(_PATH_DEVNULL, "w");
408 need = vfprintf(nullfp, fmt, args);
410 (void)vsprintf(s, fmt, args);
417 #if !defined(HAVE_SNPRINTF)
419 snprintf(char *s, size_t n, const char *fmt, ...)
425 rv = vsnprintf(s, n, fmt, ap);
431 #if !defined(HAVE_STRFTIME)
433 strftime(char *buf, size_t len, const char *fmt, const struct tm *tm)
435 static char months[][4] = {
436 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
437 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
455 if (len == 0) return buf - b;
462 s = snprintf(buf, len, "%d", tm->tm_hour);
465 s = snprintf(buf, len, "%02d", tm->tm_min);
468 s = snprintf(buf, len, "%02d", tm->tm_sec);
471 if (tm->tm_mon >= 12)
473 s = snprintf(buf, len, "%s", months[tm->tm_mon]);
476 s = snprintf(buf, len, "%02d", tm->tm_mday);
479 s = snprintf(buf, len, "%d", 1900 + tm->tm_year);
482 s = snprintf(buf, len, "Unsupported format %c",
493 #if !defined(HAVE_KILLPG)
494 #if !defined(__hpux__) && !defined(__hpux)
496 killpg(int pid, int sig)
498 return kill(-pid, sig);
503 #if !defined(HAVE_WARNX)
505 vwarnx(const char *fmt, va_list args)
507 fprintf(stderr, "%s: ", progname);
509 vfprintf(stderr, fmt, args);
510 fprintf(stderr, ": ");
515 #if !defined(HAVE_WARN)
517 vwarn(const char *fmt, va_list args)
520 fprintf(stderr, "%s\n", strerror(errno));
524 #if !defined(HAVE_ERR)
526 verr(int eval, const char *fmt, va_list args)
533 #if !defined(HAVE_ERRX)
535 verrx(int eval, const char *fmt, va_list args)
542 #if !defined(HAVE_ERR)
544 err(int eval, const char *fmt, ...)
554 #if !defined(HAVE_ERRX)
556 errx(int eval, const char *fmt, ...)
561 verrx(eval, fmt, ap);
566 #if !defined(HAVE_WARN)
568 warn(const char *fmt, ...)
578 #if !defined(HAVE_WARNX)
580 warnx(const char *fmt, ...)