1 /* $NetBSD: util.c,v 1.53 2012/06/04 22:45:05 sjg Exp $ */
4 * Missing stuff from OS's
6 * $Id: util.c,v 1.32 2012/06/06 20:08:44 sjg Exp $
12 static char rcsid[] = "$NetBSD: util.c,v 1.53 2012/06/04 22:45:05 sjg Exp $";
15 __RCSID("$NetBSD: util.c,v 1.53 2012/06/04 22:45:05 sjg Exp $");
23 #if !defined(HAVE_STRERROR)
24 extern int errno, sys_nerr;
25 extern char *sys_errlist[];
31 if (e < 0 || e >= sys_nerr) {
32 snprintf(buf, sizeof(buf), "Unknown error %d", e);
36 return sys_errlist[e];
40 #if !defined(HAVE_GETENV) || !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV)
41 extern char **environ;
44 findenv(const char *name, int *offset)
50 for (i = 0; (q = environ[i]); i++) {
52 if (p == NULL || p - q != len)
54 if (strncmp(name, q, len) == 0) {
64 getenv(const char *name)
68 return(findenv(name, &offset));
72 unsetenv(const char *name)
77 if (name == NULL || *name == '\0' || strchr(name, '=') != NULL) {
82 while (findenv(name, &offset)) { /* if set multiple times */
83 for (p = &environ[offset];; ++p)
91 setenv(const char *name, const char *value, int rewrite)
98 if (name == NULL || value == NULL) {
103 if (*value == '=') /* no `=' in value */
105 l_value = strlen(value);
107 /* find if already exists */
108 if ((c = findenv(name, &offset))) {
111 if (strlen(c) >= l_value) /* old larger; copy over */
113 } else { /* create new slot */
114 size = sizeof(char *) * (offset + 2);
115 if (savedEnv == environ) { /* just increase size */
116 if ((newenv = realloc(savedEnv, size)) == NULL)
119 } else { /* get new space */
121 * We don't free here because we don't know if
122 * the first allocation is valid on all OS's
124 if ((savedEnv = malloc(size)) == NULL)
126 (void)memcpy(savedEnv, environ, size - sizeof(char *));
129 environ[offset + 1] = NULL;
131 for (cc = name; *cc && *cc != '='; ++cc) /* no `=' in name */
134 /* name + `=' + value */
135 if ((environ[offset] = malloc(size + l_value + 2)) == NULL)
138 (void)memcpy(c, name, size);
142 (void)memcpy(c, value, l_value + 1);
148 main(int argc, char *argv[])
150 setenv(argv[1], argv[2], 0);
151 printf("%s\n", getenv(argv[1]));
153 printf("%s\n", getenv(argv[1]));
161 #if defined(__hpux__) || defined(__hpux)
163 * Like strcpy, going backwards and returning the new pointer
166 strrcpy(char *ptr, char *str)
168 int len = strlen(str);
177 char *sys_siglist[] = {
179 "Hangup", /* SIGHUP */
180 "Interrupt", /* SIGINT */
181 "Quit", /* SIGQUIT */
182 "Illegal instruction", /* SIGILL */
183 "Trace/BPT trap", /* SIGTRAP */
184 "IOT trap", /* SIGIOT */
185 "EMT trap", /* SIGEMT */
186 "Floating point exception", /* SIGFPE */
187 "Killed", /* SIGKILL */
188 "Bus error", /* SIGBUS */
189 "Segmentation fault", /* SIGSEGV */
190 "Bad system call", /* SIGSYS */
191 "Broken pipe", /* SIGPIPE */
192 "Alarm clock", /* SIGALRM */
193 "Terminated", /* SIGTERM */
194 "User defined signal 1", /* SIGUSR1 */
195 "User defined signal 2", /* SIGUSR2 */
196 "Child exited", /* SIGCLD */
197 "Power-fail restart", /* SIGPWR */
198 "Virtual timer expired", /* SIGVTALRM */
199 "Profiling timer expired", /* SIGPROF */
200 "I/O possible", /* SIGIO */
201 "Window size changes", /* SIGWINDOW */
202 "Stopped (signal)", /* SIGSTOP */
203 "Stopped", /* SIGTSTP */
204 "Continued", /* SIGCONT */
205 "Stopped (tty input)", /* SIGTTIN */
206 "Stopped (tty output)", /* SIGTTOU */
207 "Urgent I/O condition", /* SIGURG */
208 "Remote lock lost (NFS)", /* SIGLOST */
209 "Signal 31", /* reserved */
210 "DIL signal" /* SIGDIL */
212 #endif /* __hpux__ || __hpux */
214 #if defined(__hpux__) || defined(__hpux)
215 #include <sys/types.h>
216 #include <sys/syscall.h>
217 #include <sys/signal.h>
218 #include <sys/stat.h>
220 #include <sys/time.h>
224 killpg(int pid, int sig)
226 return kill(-pid, sig);
229 #if !defined(__hpux__) && !defined(__hpux)
243 #if !defined(__hpux__) && !defined(__hpux)
245 utimes(char *file, struct timeval tvp[2])
249 t.actime = tvp[0].tv_sec;
250 t.modtime = tvp[1].tv_sec;
251 return(utime(file, &t));
255 #if !defined(BSD) && !defined(d_fileno)
256 # define d_fileno d_ino
259 #ifndef DEV_DEV_COMPARE
260 # define DEV_DEV_COMPARE(a, b) ((a) == (b))
262 #define ISDOT(c) ((c)[0] == '.' && (((c)[1] == '\0') || ((c)[1] == '/')))
263 #define ISDOTDOT(c) ((c)[0] == '.' && ISDOT(&((c)[1])))
266 getwd(char *pathname)
272 struct stat st_root, st_cur, st_next, st_dotdot;
273 char pathbuf[MAXPATHLEN], nextpathbuf[MAXPATHLEN * 2];
274 char *pathptr, *nextpathptr, *cur_name_add;
276 /* find the inode of root */
277 if (stat("/", &st_root) == -1) {
278 (void)sprintf(pathname,
279 "getwd: Cannot stat \"/\" (%s)", strerror(errno));
282 pathbuf[MAXPATHLEN - 1] = '\0';
283 pathptr = &pathbuf[MAXPATHLEN - 1];
284 nextpathbuf[MAXPATHLEN - 1] = '\0';
285 cur_name_add = nextpathptr = &nextpathbuf[MAXPATHLEN - 1];
287 /* find the inode of the current directory */
288 if (lstat(".", &st_cur) == -1) {
289 (void)sprintf(pathname,
290 "getwd: Cannot stat \".\" (%s)", strerror(errno));
293 nextpathptr = strrcpy(nextpathptr, "../");
295 /* Descend to root */
298 /* look if we found root yet */
299 if (st_cur.st_ino == st_root.st_ino &&
300 DEV_DEV_COMPARE(st_cur.st_dev, st_root.st_dev)) {
301 (void)strcpy(pathname, *pathptr != '/' ? "/" : pathptr);
305 /* open the parent directory */
306 if (stat(nextpathptr, &st_dotdot) == -1) {
307 (void)sprintf(pathname,
308 "getwd: Cannot stat directory \"%s\" (%s)",
309 nextpathptr, strerror(errno));
312 if ((dp = opendir(nextpathptr)) == NULL) {
313 (void)sprintf(pathname,
314 "getwd: Cannot open directory \"%s\" (%s)",
315 nextpathptr, strerror(errno));
319 /* look in the parent for the entry with the same inode */
320 if (DEV_DEV_COMPARE(st_dotdot.st_dev, st_cur.st_dev)) {
321 /* Parent has same device. No need to stat every member */
322 for (d = readdir(dp); d != NULL; d = readdir(dp))
323 if (d->d_fileno == st_cur.st_ino)
328 * Parent has a different device. This is a mount point so we
329 * need to stat every member
331 for (d = readdir(dp); d != NULL; d = readdir(dp)) {
332 if (ISDOT(d->d_name) || ISDOTDOT(d->d_name))
334 (void)strcpy(cur_name_add, d->d_name);
335 if (lstat(nextpathptr, &st_next) == -1) {
336 (void)sprintf(pathname,
337 "getwd: Cannot stat \"%s\" (%s)",
338 d->d_name, strerror(errno));
342 /* check if we found it yet */
343 if (st_next.st_ino == st_cur.st_ino &&
344 DEV_DEV_COMPARE(st_next.st_dev, st_cur.st_dev))
349 (void)sprintf(pathname,
350 "getwd: Cannot find \".\" in \"..\"");
355 pathptr = strrcpy(pathptr, d->d_name);
356 pathptr = strrcpy(pathptr, "/");
357 nextpathptr = strrcpy(nextpathptr, "../");
359 *cur_name_add = '\0';
365 #if !defined(HAVE_GETCWD)
375 /* force posix signals */
377 bmake_signal(int s, void (*a)(int)))(int)
379 struct sigaction sa, osa;
382 sigemptyset(&sa.sa_mask);
383 sa.sa_flags = SA_RESTART;
385 if (sigaction(s, &sa, &osa) == -1)
388 return osa.sa_handler;
391 #if !defined(HAVE_VSNPRINTF) || !defined(HAVE_VASPRINTF)
395 #if !defined(HAVE_VSNPRINTF)
396 #if !defined(__osf__)
398 #define STRFLAG (_IOSTRG|_IOWRT) /* no _IOWRT: avoid stdio bug */
401 #define STRFLAG (_IOREAD) /* XXX: Assume svr4 stdio */
407 vsnprintf(char *s, size_t n, const char *fmt, va_list args)
412 fakebuf._flag = STRFLAG;
414 * Some os's are char * _ptr, others are unsigned char *_ptr...
415 * We cast to void * to make everyone happy.
417 fakebuf._ptr = (void *)s;
420 _doprnt(fmt, args, &fakebuf);
422 putc('\0', &fakebuf);
425 return (n-fakebuf._cnt-1);
427 #ifndef _PATH_DEVNULL
428 # define _PATH_DEVNULL "/dev/null"
431 * Rats... we don't want to clobber anything...
432 * do a printf to /dev/null to see how much space we need.
435 int need = 0; /* XXX what's a useful error return? */
438 nullfp = fopen(_PATH_DEVNULL, "w");
440 need = vfprintf(nullfp, fmt, args);
442 (void)vsprintf(s, fmt, args);
449 #if !defined(HAVE_SNPRINTF)
451 snprintf(char *s, size_t n, const char *fmt, ...)
457 rv = vsnprintf(s, n, fmt, ap);
463 #if !defined(HAVE_STRFTIME)
465 strftime(char *buf, size_t len, const char *fmt, const struct tm *tm)
467 static char months[][4] = {
468 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
469 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
487 if (len == 0) return buf - b;
494 s = snprintf(buf, len, "%d", tm->tm_hour);
497 s = snprintf(buf, len, "%02d", tm->tm_min);
500 s = snprintf(buf, len, "%02d", tm->tm_sec);
503 if (tm->tm_mon >= 12)
505 s = snprintf(buf, len, "%s", months[tm->tm_mon]);
508 s = snprintf(buf, len, "%02d", tm->tm_mday);
511 s = snprintf(buf, len, "%d", 1900 + tm->tm_year);
514 s = snprintf(buf, len, "Unsupported format %c",
524 #if !defined(HAVE_KILLPG)
525 #if !defined(__hpux__) && !defined(__hpux)
527 killpg(int pid, int sig)
529 return kill(-pid, sig);
534 #if !defined(HAVE_WARNX)
536 vwarnx(const char *fmt, va_list args)
538 fprintf(stderr, "%s: ", progname);
540 vfprintf(stderr, fmt, args);
541 fprintf(stderr, ": ");
546 #if !defined(HAVE_WARN)
548 vwarn(const char *fmt, va_list args)
551 fprintf(stderr, "%s\n", strerror(errno));
555 #if !defined(HAVE_ERR)
557 verr(int eval, const char *fmt, va_list args)
564 #if !defined(HAVE_ERRX)
566 verrx(int eval, const char *fmt, va_list args)
573 #if !defined(HAVE_ERR)
575 err(int eval, const char *fmt, ...)
585 #if !defined(HAVE_ERRX)
587 errx(int eval, const char *fmt, ...)
592 verrx(eval, fmt, ap);
597 #if !defined(HAVE_WARN)
599 warn(const char *fmt, ...)
609 #if !defined(HAVE_WARNX)
611 warnx(const char *fmt, ...)