1 /* $NetBSD: util.c,v 1.6 2010/04/20 13:37:49 joerg Exp $ */
4 * Missing stuff from OS's
6 * $Id: util.c,v 1.7 2010/05/13 18:43:08 joerg Exp $
12 static char rcsid[] = "$NetBSD: util.c,v 1.6 2010/04/20 13:37:49 joerg Exp $";
15 __RCSID("$NetBSD: util.c,v 1.6 2010/04/20 13:37:49 joerg Exp $");
22 #if !defined(HAVE_STRERROR)
23 extern int errno, sys_nerr;
24 extern char *sys_errlist[];
30 if (e < 0 || e >= sys_nerr) {
31 snprintf(buf, sizeof(buf), "Unknown error %d", e);
35 return sys_errlist[e];
39 #if !defined(HAVE_SETENV) || !defined(HAVE_UNSETENV)
40 extern char **environ;
43 findenv(const char *name, int *offset)
48 for (i = 0; (q = environ[i]); i++) {
49 char *p = strchr(q, '=');
52 if (strncmp(name, q, len = p - q) == 0) {
62 #if !defined(HAVE_UNSETENV)
64 unsetenv(const char *name)
69 if (name == NULL || *name == '\0' || strchr(name, '=') != NULL) {
74 while (findenv(name, &offset)) { /* if set multiple times */
75 for (p = &environ[offset];; ++p)
83 #if !defined(HAVE_SETENV)
85 setenv(const char *name, const char *value, int rewrite)
87 static char **saveenv; /* copy of previously allocated space */
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 (saveenv == environ) { /* just increase size */
111 if ((newenv = realloc(saveenv, 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 ((saveenv = malloc(size)) == NULL)
121 (void)memcpy(saveenv, 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(__hpux__) && !defined(__hpux)
238 /* turn into bsd signals */
240 signal(int s, void (*a)(int)))(int)
242 struct sigvec osv, sv;
244 (void)sigvector(s, NULL, &osv);
248 sv.sv_flags = SV_BSDSIG;
251 if (sigvector(s, &sv, NULL) == -1)
253 return (osv.sv_handler);
256 #if !defined(__hpux__) && !defined(__hpux)
258 utimes(char *file, struct timeval tvp[2])
262 t.actime = tvp[0].tv_sec;
263 t.modtime = tvp[1].tv_sec;
264 return(utime(file, &t));
268 #if !defined(BSD) && !defined(d_fileno)
269 # define d_fileno d_ino
272 #ifndef DEV_DEV_COMPARE
273 # define DEV_DEV_COMPARE(a, b) ((a) == (b))
275 #define ISDOT(c) ((c)[0] == '.' && (((c)[1] == '\0') || ((c)[1] == '/')))
276 #define ISDOTDOT(c) ((c)[0] == '.' && ISDOT(&((c)[1])))
279 getwd(char *pathname)
285 struct stat st_root, st_cur, st_next, st_dotdot;
286 char pathbuf[MAXPATHLEN], nextpathbuf[MAXPATHLEN * 2];
287 char *pathptr, *nextpathptr, *cur_name_add;
289 /* find the inode of root */
290 if (stat("/", &st_root) == -1) {
291 (void)sprintf(pathname,
292 "getwd: Cannot stat \"/\" (%s)", strerror(errno));
295 pathbuf[MAXPATHLEN - 1] = '\0';
296 pathptr = &pathbuf[MAXPATHLEN - 1];
297 nextpathbuf[MAXPATHLEN - 1] = '\0';
298 cur_name_add = nextpathptr = &nextpathbuf[MAXPATHLEN - 1];
300 /* find the inode of the current directory */
301 if (lstat(".", &st_cur) == -1) {
302 (void)sprintf(pathname,
303 "getwd: Cannot stat \".\" (%s)", strerror(errno));
306 nextpathptr = strrcpy(nextpathptr, "../");
308 /* Descend to root */
311 /* look if we found root yet */
312 if (st_cur.st_ino == st_root.st_ino &&
313 DEV_DEV_COMPARE(st_cur.st_dev, st_root.st_dev)) {
314 (void)strcpy(pathname, *pathptr != '/' ? "/" : pathptr);
318 /* open the parent directory */
319 if (stat(nextpathptr, &st_dotdot) == -1) {
320 (void)sprintf(pathname,
321 "getwd: Cannot stat directory \"%s\" (%s)",
322 nextpathptr, strerror(errno));
325 if ((dp = opendir(nextpathptr)) == NULL) {
326 (void)sprintf(pathname,
327 "getwd: Cannot open directory \"%s\" (%s)",
328 nextpathptr, strerror(errno));
332 /* look in the parent for the entry with the same inode */
333 if (DEV_DEV_COMPARE(st_dotdot.st_dev, st_cur.st_dev)) {
334 /* Parent has same device. No need to stat every member */
335 for (d = readdir(dp); d != NULL; d = readdir(dp))
336 if (d->d_fileno == st_cur.st_ino)
341 * Parent has a different device. This is a mount point so we
342 * need to stat every member
344 for (d = readdir(dp); d != NULL; d = readdir(dp)) {
345 if (ISDOT(d->d_name) || ISDOTDOT(d->d_name))
347 (void)strcpy(cur_name_add, d->d_name);
348 if (lstat(nextpathptr, &st_next) == -1) {
349 (void)sprintf(pathname,
350 "getwd: Cannot stat \"%s\" (%s)",
351 d->d_name, strerror(errno));
355 /* check if we found it yet */
356 if (st_next.st_ino == st_cur.st_ino &&
357 DEV_DEV_COMPARE(st_next.st_dev, st_cur.st_dev))
362 (void)sprintf(pathname,
363 "getwd: Cannot find \".\" in \"..\"");
368 pathptr = strrcpy(pathptr, d->d_name);
369 pathptr = strrcpy(pathptr, "/");
370 nextpathptr = strrcpy(nextpathptr, "../");
372 *cur_name_add = '\0';
378 #if !defined(HAVE_GETCWD)
388 #if !defined(FORCE_POSIX_SIGNALS)
390 * If FORCE_POSIX_SIGNALS is defined
391 * then sigcompat will have done this.
393 #if defined(sun) && (defined(__svr4__) || defined(__SVR4))
396 /* turn into bsd signals */
398 signal(int s, void (*a)(int)))(int)
400 struct sigaction sa, osa;
403 sigemptyset(&sa.sa_mask);
404 sa.sa_flags = SA_RESTART;
406 if (sigaction(s, &sa, &osa) == -1)
409 return osa.sa_handler;
414 #if !defined(HAVE_VSNPRINTF) || !defined(HAVE_VASPRINTF)
418 #if !defined(HAVE_VSNPRINTF)
419 #if !defined(__osf__)
421 #define STRFLAG (_IOSTRG|_IOWRT) /* no _IOWRT: avoid stdio bug */
424 #define STRFLAG (_IOREAD) /* XXX: Assume svr4 stdio */
430 vsnprintf(char *s, size_t n, const char *fmt, va_list args)
435 fakebuf._flag = STRFLAG;
437 * Some os's are char * _ptr, others are unsigned char *_ptr...
438 * We cast to void * to make everyone happy.
440 fakebuf._ptr = (void *)s;
443 _doprnt(fmt, args, &fakebuf);
445 putc('\0', &fakebuf);
448 return (n-fakebuf._cnt-1);
450 #ifndef _PATH_DEVNULL
451 # define _PATH_DEVNULL "/dev/null"
454 * Rats... we don't want to clobber anything...
455 * do a printf to /dev/null to see how much space we need.
458 int need = 0; /* XXX what's a useful error return? */
461 nullfp = fopen(_PATH_DEVNULL, "w");
463 need = vfprintf(nullfp, fmt, args);
465 (void)vsprintf(s, fmt, args);
472 #if !defined(HAVE_SNPRINTF)
474 snprintf(char *s, size_t n, const char *fmt, ...)
480 rv = vsnprintf(s, n, fmt, ap);
486 #if !defined(HAVE_STRFTIME)
488 strftime(char *buf, size_t len, const char *fmt, const struct tm *tm)
490 static char months[][4] = {
491 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
492 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
510 if (len == 0) return buf - b;
517 s = snprintf(buf, len, "%d", tm->tm_hour);
520 s = snprintf(buf, len, "%02d", tm->tm_min);
523 s = snprintf(buf, len, "%02d", tm->tm_sec);
526 if (tm->tm_mon >= 12)
528 s = snprintf(buf, len, "%s", months[tm->tm_mon]);
531 s = snprintf(buf, len, "%02d", tm->tm_mday);
534 s = snprintf(buf, len, "%d", 1900 + tm->tm_year);
537 s = snprintf(buf, len, "Unsupported format %c",
547 #if !defined(HAVE_KILLPG)
548 #if !defined(__hpux__) && !defined(__hpux)
550 killpg(int pid, int sig)
552 return kill(-pid, sig);