1 /* $NetBSD: util.c,v 1.58 2020/08/01 14:47:49 rillig Exp $ */
4 * Missing stuff from OS's
6 * $Id: util.c,v 1.36 2020/08/01 23:08:14 sjg Exp $
8 #if defined(__MINT__) || defined(__linux__)
15 static char rcsid[] = "$NetBSD: util.c,v 1.58 2020/08/01 14:47:49 rillig Exp $";
18 __RCSID("$NetBSD: util.c,v 1.58 2020/08/01 14:47:49 rillig Exp $");
26 #if !defined(HAVE_STRERROR)
27 extern int errno, sys_nerr;
28 extern char *sys_errlist[];
34 if (e < 0 || e >= sys_nerr) {
35 snprintf(buf, sizeof(buf), "Unknown error %d", e);
39 return sys_errlist[e];
43 #if !defined(HAVE_GETENV) || !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV)
44 extern char **environ;
47 findenv(const char *name, int *offset)
53 for (i = 0; (q = environ[i]); i++) {
55 if (p == NULL || p - q != len)
57 if (strncmp(name, q, len) == 0) {
67 getenv(const char *name)
71 return findenv(name, &offset);
75 unsetenv(const char *name)
80 if (name == NULL || *name == '\0' || strchr(name, '=') != NULL) {
85 while (findenv(name, &offset)) { /* if set multiple times */
86 for (p = &environ[offset];; ++p)
94 setenv(const char *name, const char *value, int rewrite)
101 if (name == NULL || value == NULL) {
106 if (*value == '=') /* no `=' in value */
108 l_value = strlen(value);
110 /* find if already exists */
111 if ((c = findenv(name, &offset))) {
114 if (strlen(c) >= l_value) /* old larger; copy over */
116 } else { /* create new slot */
117 size = sizeof(char *) * (offset + 2);
118 if (savedEnv == environ) { /* just increase size */
119 if ((newenv = realloc(savedEnv, size)) == NULL)
122 } else { /* get new space */
124 * We don't free here because we don't know if
125 * the first allocation is valid on all OS's
127 if ((savedEnv = malloc(size)) == NULL)
129 (void)memcpy(savedEnv, environ, size - sizeof(char *));
132 environ[offset + 1] = NULL;
134 for (cc = name; *cc && *cc != '='; ++cc) /* no `=' in name */
137 /* name + `=' + value */
138 if ((environ[offset] = malloc(size + l_value + 2)) == NULL)
141 (void)memcpy(c, name, size);
145 (void)memcpy(c, value, l_value + 1);
151 main(int argc, char *argv[])
153 setenv(argv[1], argv[2], 0);
154 printf("%s\n", getenv(argv[1]));
156 printf("%s\n", getenv(argv[1]));
164 #if defined(__hpux__) || defined(__hpux)
166 * Like strcpy, going backwards and returning the new pointer
169 strrcpy(char *ptr, char *str)
171 int len = strlen(str);
180 char *sys_siglist[] = {
182 "Hangup", /* SIGHUP */
183 "Interrupt", /* SIGINT */
184 "Quit", /* SIGQUIT */
185 "Illegal instruction", /* SIGILL */
186 "Trace/BPT trap", /* SIGTRAP */
187 "IOT trap", /* SIGIOT */
188 "EMT trap", /* SIGEMT */
189 "Floating point exception", /* SIGFPE */
190 "Killed", /* SIGKILL */
191 "Bus error", /* SIGBUS */
192 "Segmentation fault", /* SIGSEGV */
193 "Bad system call", /* SIGSYS */
194 "Broken pipe", /* SIGPIPE */
195 "Alarm clock", /* SIGALRM */
196 "Terminated", /* SIGTERM */
197 "User defined signal 1", /* SIGUSR1 */
198 "User defined signal 2", /* SIGUSR2 */
199 "Child exited", /* SIGCLD */
200 "Power-fail restart", /* SIGPWR */
201 "Virtual timer expired", /* SIGVTALRM */
202 "Profiling timer expired", /* SIGPROF */
203 "I/O possible", /* SIGIO */
204 "Window size changes", /* SIGWINDOW */
205 "Stopped (signal)", /* SIGSTOP */
206 "Stopped", /* SIGTSTP */
207 "Continued", /* SIGCONT */
208 "Stopped (tty input)", /* SIGTTIN */
209 "Stopped (tty output)", /* SIGTTOU */
210 "Urgent I/O condition", /* SIGURG */
211 "Remote lock lost (NFS)", /* SIGLOST */
212 "Signal 31", /* reserved */
213 "DIL signal" /* SIGDIL */
215 #endif /* __hpux__ || __hpux */
217 #if defined(__hpux__) || defined(__hpux)
218 #include <sys/types.h>
219 #include <sys/syscall.h>
220 #include <sys/signal.h>
221 #include <sys/stat.h>
223 #include <sys/time.h>
227 killpg(int pid, int sig)
229 return kill(-pid, sig);
232 #if !defined(BSD) && !defined(d_fileno)
233 # define d_fileno d_ino
236 #ifndef DEV_DEV_COMPARE
237 # define DEV_DEV_COMPARE(a, b) ((a) == (b))
239 #define ISDOT(c) ((c)[0] == '.' && (((c)[1] == '\0') || ((c)[1] == '/')))
240 #define ISDOTDOT(c) ((c)[0] == '.' && ISDOT(&((c)[1])))
243 getwd(char *pathname)
249 struct stat st_root, st_cur, st_next, st_dotdot;
250 char pathbuf[MAXPATHLEN], nextpathbuf[MAXPATHLEN * 2];
251 char *pathptr, *nextpathptr, *cur_name_add;
253 /* find the inode of root */
254 if (stat("/", &st_root) == -1) {
255 (void)sprintf(pathname,
256 "getwd: Cannot stat \"/\" (%s)", strerror(errno));
259 pathbuf[MAXPATHLEN - 1] = '\0';
260 pathptr = &pathbuf[MAXPATHLEN - 1];
261 nextpathbuf[MAXPATHLEN - 1] = '\0';
262 cur_name_add = nextpathptr = &nextpathbuf[MAXPATHLEN - 1];
264 /* find the inode of the current directory */
265 if (lstat(".", &st_cur) == -1) {
266 (void)sprintf(pathname,
267 "getwd: Cannot stat \".\" (%s)", strerror(errno));
270 nextpathptr = strrcpy(nextpathptr, "../");
272 /* Descend to root */
275 /* look if we found root yet */
276 if (st_cur.st_ino == st_root.st_ino &&
277 DEV_DEV_COMPARE(st_cur.st_dev, st_root.st_dev)) {
278 (void)strcpy(pathname, *pathptr != '/' ? "/" : pathptr);
282 /* open the parent directory */
283 if (stat(nextpathptr, &st_dotdot) == -1) {
284 (void)sprintf(pathname,
285 "getwd: Cannot stat directory \"%s\" (%s)",
286 nextpathptr, strerror(errno));
289 if ((dp = opendir(nextpathptr)) == NULL) {
290 (void)sprintf(pathname,
291 "getwd: Cannot open directory \"%s\" (%s)",
292 nextpathptr, strerror(errno));
296 /* look in the parent for the entry with the same inode */
297 if (DEV_DEV_COMPARE(st_dotdot.st_dev, st_cur.st_dev)) {
298 /* Parent has same device. No need to stat every member */
299 for (d = readdir(dp); d != NULL; d = readdir(dp))
300 if (d->d_fileno == st_cur.st_ino)
305 * Parent has a different device. This is a mount point so we
306 * need to stat every member
308 for (d = readdir(dp); d != NULL; d = readdir(dp)) {
309 if (ISDOT(d->d_name) || ISDOTDOT(d->d_name))
311 (void)strcpy(cur_name_add, d->d_name);
312 if (lstat(nextpathptr, &st_next) == -1) {
313 (void)sprintf(pathname,
314 "getwd: Cannot stat \"%s\" (%s)",
315 d->d_name, strerror(errno));
319 /* check if we found it yet */
320 if (st_next.st_ino == st_cur.st_ino &&
321 DEV_DEV_COMPARE(st_next.st_dev, st_cur.st_dev))
326 (void)sprintf(pathname,
327 "getwd: Cannot find \".\" in \"..\"");
332 pathptr = strrcpy(pathptr, d->d_name);
333 pathptr = strrcpy(pathptr, "/");
334 nextpathptr = strrcpy(nextpathptr, "../");
336 *cur_name_add = '\0';
342 #if !defined(HAVE_GETCWD)
352 /* force posix signals */
354 bmake_signal(int s, void (*a)(int)))(int)
356 struct sigaction sa, osa;
359 sigemptyset(&sa.sa_mask);
360 sa.sa_flags = SA_RESTART;
362 if (sigaction(s, &sa, &osa) == -1)
365 return osa.sa_handler;
368 #if !defined(HAVE_VSNPRINTF) || !defined(HAVE_VASPRINTF)
372 #if !defined(HAVE_VSNPRINTF)
373 #if !defined(__osf__)
375 #define STRFLAG (_IOSTRG|_IOWRT) /* no _IOWRT: avoid stdio bug */
378 #define STRFLAG (_IOREAD) /* XXX: Assume svr4 stdio */
384 vsnprintf(char *s, size_t n, const char *fmt, va_list args)
389 fakebuf._flag = STRFLAG;
391 * Some os's are char * _ptr, others are unsigned char *_ptr...
392 * We cast to void * to make everyone happy.
394 fakebuf._ptr = (void *)s;
397 _doprnt(fmt, args, &fakebuf);
399 putc('\0', &fakebuf);
402 return n-fakebuf._cnt-1;
404 #ifndef _PATH_DEVNULL
405 # define _PATH_DEVNULL "/dev/null"
408 * Rats... we don't want to clobber anything...
409 * do a printf to /dev/null to see how much space we need.
412 int need = 0; /* XXX what's a useful error return? */
415 nullfp = fopen(_PATH_DEVNULL, "w");
417 need = vfprintf(nullfp, fmt, args);
419 (void)vsprintf(s, fmt, args);
426 #if !defined(HAVE_SNPRINTF)
428 snprintf(char *s, size_t n, const char *fmt, ...)
434 rv = vsnprintf(s, n, fmt, ap);
440 #if !defined(HAVE_STRFTIME)
442 strftime(char *buf, size_t len, const char *fmt, const struct tm *tm)
444 static char months[][4] = {
445 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
446 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
464 if (len == 0) return buf - b;
471 s = snprintf(buf, len, "%d", tm->tm_hour);
474 s = snprintf(buf, len, "%02d", tm->tm_min);
477 s = snprintf(buf, len, "%02d", tm->tm_sec);
480 if (tm->tm_mon >= 12)
482 s = snprintf(buf, len, "%s", months[tm->tm_mon]);
485 s = snprintf(buf, len, "%02d", tm->tm_mday);
488 s = snprintf(buf, len, "%d", 1900 + tm->tm_year);
491 s = snprintf(buf, len, "Unsupported format %c",
501 #if !defined(HAVE_KILLPG)
502 #if !defined(__hpux__) && !defined(__hpux)
504 killpg(int pid, int sig)
506 return kill(-pid, sig);
511 #if !defined(HAVE_WARNX)
513 vwarnx(const char *fmt, va_list args)
515 fprintf(stderr, "%s: ", progname);
517 vfprintf(stderr, fmt, args);
518 fprintf(stderr, ": ");
523 #if !defined(HAVE_WARN)
525 vwarn(const char *fmt, va_list args)
528 fprintf(stderr, "%s\n", strerror(errno));
532 #if !defined(HAVE_ERR)
534 verr(int eval, const char *fmt, va_list args)
541 #if !defined(HAVE_ERRX)
543 verrx(int eval, const char *fmt, va_list args)
550 #if !defined(HAVE_ERR)
552 err(int eval, const char *fmt, ...)
562 #if !defined(HAVE_ERRX)
564 errx(int eval, const char *fmt, ...)
569 verrx(eval, fmt, ap);
574 #if !defined(HAVE_WARN)
576 warn(const char *fmt, ...)
586 #if !defined(HAVE_WARNX)
588 warnx(const char *fmt, ...)