1 /* $NetBSD: util.c,v 1.2 2004/01/14 11:20:30 grant Exp $ */
4 * Missing stuff from OS's
6 * $Id: util.c,v 1.1.1.1 2004/03/11 13:04:13 grant Exp $
10 static char rcsid[] = "$NetBSD: util.c,v 1.2 2004/01/14 11:20:30 grant Exp $";
12 #include <sys/cdefs.h>
14 __RCSID("$NetBSD: util.c,v 1.2 2004/01/14 11:20:30 grant Exp $");
21 #include <sys/param.h>
32 #if !defined(HAVE_STRERROR)
34 extern int errno, sys_nerr;
35 extern char *sys_errlist[];
42 if (e < 0 || e >= sys_nerr) {
43 snprintf(buf, sizeof(buf), "Unknown error %d", e);
47 return sys_errlist[e];
51 #if !defined(HAVE_STRDUP)
54 * Make a duplicate of a string.
55 * For systems which lack this function.
66 len = strlen(str) + 1;
69 return memcpy(p, str, len);
74 #if !defined(HAVE_SETENV)
76 setenv(name, value, dum)
82 int len = strlen(name) + strlen(value) + 2; /* = \0 */
83 char *ptr = (char*) emalloc(len);
108 #if defined(__hpux) && !defined(_HPUX_SOURCE)
109 #include <sys/types.h>
110 #include <sys/param.h>
111 #include <sys/syscall.h>
112 #include <sys/signal.h>
113 #include <sys/stat.h>
116 #include <sys/time.h>
125 return kill(-pid, sig);
142 setpriority(which, who, niceval)
143 int which, who, niceval;
145 #ifdef SYS_setpriority
146 return syscall(SYS_setpriority, which, who, niceval);
161 return setresuid(euid, ruid, -1);
168 return setresgid(egid, rgid, -1);
170 #endif /* __hpux && !_HPUX_SOURCE */
172 #if defined(__hpux__) || defined(__hpux)
175 * Like strcpy, going backwards and returning the new pointer
179 register char *ptr, *str;
181 register int len = strlen(str);
190 char *sys_siglist[] = {
192 "Hangup", /* SIGHUP */
193 "Interrupt", /* SIGINT */
194 "Quit", /* SIGQUIT */
195 "Illegal instruction", /* SIGILL */
196 "Trace/BPT trap", /* SIGTRAP */
197 "IOT trap", /* SIGIOT */
198 "EMT trap", /* SIGEMT */
199 "Floating point exception", /* SIGFPE */
200 "Killed", /* SIGKILL */
201 "Bus error", /* SIGBUS */
202 "Segmentation fault", /* SIGSEGV */
203 "Bad system call", /* SIGSYS */
204 "Broken pipe", /* SIGPIPE */
205 "Alarm clock", /* SIGALRM */
206 "Terminated", /* SIGTERM */
207 "User defined signal 1", /* SIGUSR1 */
208 "User defined signal 2", /* SIGUSR2 */
209 "Child exited", /* SIGCLD */
210 "Power-fail restart", /* SIGPWR */
211 "Virtual timer expired", /* SIGVTALRM */
212 "Profiling timer expired", /* SIGPROF */
213 "I/O possible", /* SIGIO */
214 "Window size changes", /* SIGWINDOW */
215 "Stopped (signal)", /* SIGSTOP */
216 "Stopped", /* SIGTSTP */
217 "Continued", /* SIGCONT */
218 "Stopped (tty input)", /* SIGTTIN */
219 "Stopped (tty output)", /* SIGTTOU */
220 "Urgent I/O condition", /* SIGURG */
221 "Remote lock lost (NFS)", /* SIGLOST */
222 "Signal 31", /* reserved */
223 "DIL signal" /* SIGDIL */
225 #endif /* __hpux__ || __hpux */
227 #if defined(__hpux) && !defined(_HPUX_SOURCE)
232 struct timeval tvp[2];
236 t.actime = tvp[0].tv_sec;
237 t.modtime = tvp[1].tv_sec;
238 return(utime(file, &t));
241 #if !defined(BSD) && !defined(d_fileno)
242 # define d_fileno d_ino
245 #ifndef DEV_DEV_COMPARE
246 # define DEV_DEV_COMPARE(a, b) ((a) == (b))
248 #define ISDOT(c) ((c)[0] == '.' && (((c)[1] == '\0') || ((c)[1] == '/')))
249 #define ISDOTDOT(c) ((c)[0] == '.' && ISDOT(&((c)[1])))
259 struct stat st_root, st_cur, st_next, st_dotdot;
260 char pathbuf[MAXPATHLEN], nextpathbuf[MAXPATHLEN * 2];
261 char *pathptr, *nextpathptr, *cur_name_add;
263 /* find the inode of root */
264 if (stat("/", &st_root) == -1) {
265 (void) snprintf(pathname, sizeof(pathname),
266 "getwd: Cannot stat \"/\" (%s)", strerror(errno));
269 pathbuf[MAXPATHLEN - 1] = '\0';
270 pathptr = &pathbuf[MAXPATHLEN - 1];
271 nextpathbuf[MAXPATHLEN - 1] = '\0';
272 cur_name_add = nextpathptr = &nextpathbuf[MAXPATHLEN - 1];
274 /* find the inode of the current directory */
275 if (lstat(".", &st_cur) == -1) {
276 (void) snprintf(pathname, sizeof(pathname),
277 "getwd: Cannot stat \".\" (%s)", strerror(errno));
280 nextpathptr = strrcpy(nextpathptr, "../");
282 /* Descend to root */
285 /* look if we found root yet */
286 if (st_cur.st_ino == st_root.st_ino &&
287 DEV_DEV_COMPARE(st_cur.st_dev, st_root.st_dev)) {
288 (void) strcpy(pathname, *pathptr != '/' ? "/" : pathptr);
292 /* open the parent directory */
293 if (stat(nextpathptr, &st_dotdot) == -1) {
294 (void) snprintf(pathname, sizeof(pathname),
295 "getwd: Cannot stat directory \"%s\" (%s)",
296 nextpathptr, strerror(errno));
299 if ((dp = opendir(nextpathptr)) == NULL) {
300 (void) snprintf(pathname, sizeof(pathname),
301 "getwd: Cannot open directory \"%s\" (%s)",
302 nextpathptr, strerror(errno));
306 /* look in the parent for the entry with the same inode */
307 if (DEV_DEV_COMPARE(st_dotdot.st_dev, st_cur.st_dev)) {
308 /* Parent has same device. No need to stat every member */
309 for (d = readdir(dp); d != NULL; d = readdir(dp))
310 if (d->d_fileno == st_cur.st_ino)
315 * Parent has a different device. This is a mount point so we
316 * need to stat every member
318 for (d = readdir(dp); d != NULL; d = readdir(dp)) {
319 if (ISDOT(d->d_name) || ISDOTDOT(d->d_name))
321 (void) strcpy(cur_name_add, d->d_name);
322 if (lstat(nextpathptr, &st_next) == -1) {
323 (void) snprintf(pathname, sizeof(pathname),
324 "getwd: Cannot stat \"%s\" (%s)",
325 d->d_name, strerror(errno));
329 /* check if we found it yet */
330 if (st_next.st_ino == st_cur.st_ino &&
331 DEV_DEV_COMPARE(st_next.st_dev, st_cur.st_dev))
336 (void) snprintf(pathname, sizeof(pathname),
337 "getwd: Cannot find \".\" in \"..\"");
342 pathptr = strrcpy(pathptr, d->d_name);
343 pathptr = strrcpy(pathptr, "/");
344 nextpathptr = strrcpy(nextpathptr, "../");
346 *cur_name_add = '\0';
350 #endif /* __hpux && !_HPUX_SOURCE */
352 #if !defined(HAVE_GETCWD)
363 #if !defined(HAVE_VSNPRINTF)
364 #if !defined(BSD4_4) && !defined(SUNOS_5_7) && !defined(__linux__)
371 #if !defined(__osf__)
373 #define STRFLAG (_IOSTRG|_IOWRT) /* no _IOWRT: avoid stdio bug */
376 #define STRFLAG (_IOREAD) /* XXX: Assume svr4 stdio */
382 vsnprintf(s, n, fmt, args)
391 fakebuf._flag = STRFLAG;
393 * Some os's are char * _ptr, others are unsigned char *_ptr...
394 * We cast to void * to make everyone happy.
396 fakebuf._ptr = (void *) s;
399 _doprnt(fmt, args, &fakebuf);
401 putc('\0', &fakebuf);
404 return (n-fakebuf._cnt-1);
406 (void) vsprintf(s, fmt, args);
413 #if !defined(HAVE_SNPRINTF)
416 snprintf(char *s, size_t n, const char *fmt, ...)
433 s = va_arg(ap, char *);
434 n = va_arg(ap, size_t);
435 fmt = va_arg(ap, const char *);
437 rv = vsnprintf(s, n, fmt, ap);
443 #if !defined(HAVE_STRFTIME)
445 strftime(buf, len, fmt, tm)
451 static char months[][4] = {
452 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
453 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
471 if (len == 0) return buf - b;
478 s = snprintf(buf, len, "%d", tm->tm_hour);
481 s = snprintf(buf, len, "%02d", tm->tm_min);
484 s = snprintf(buf, len, "%02d", tm->tm_sec);
487 if (tm->tm_mon >= 12)
489 s = snprintf(buf, len, "%s", months[tm->tm_mon]);
492 s = snprintf(buf, len, "%s", tm->tm_mday);
495 s = snprintf(buf, len, "%s", 1900 + tm->tm_year);
498 s = snprintf(buf, len, "Unsupported format %c",