1 /* $NetBSD: util.c,v 1.50 2010/06/03 15:40:16 sjg Exp $ */
4 * Missing stuff from OS's
6 * $Id: util.c,v 1.1.1.8 2010/09/07 14:12:10 joerg Exp $
12 static char rcsid[] = "$NetBSD: util.c,v 1.50 2010/06/03 15:40:16 sjg Exp $";
15 __RCSID("$NetBSD: util.c,v 1.50 2010/06/03 15:40:16 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_SETENV) || !defined(HAVE_UNSETENV)
41 extern char **environ;
44 findenv(const char *name, int *offset)
49 for (i = 0; (q = environ[i]); i++) {
50 char *p = strchr(q, '=');
53 if (strncmp(name, q, len = p - q) == 0) {
63 #if !defined(HAVE_UNSETENV)
65 unsetenv(const char *name)
70 if (name == NULL || *name == '\0' || strchr(name, '=') != NULL) {
75 while (findenv(name, &offset)) { /* if set multiple times */
76 for (p = &environ[offset];; ++p)
84 #if !defined(HAVE_SETENV)
86 setenv(const char *name, const char *value, int rewrite)
88 static char **saveenv; /* copy of previously allocated space */
94 if (name == NULL || value == NULL) {
99 if (*value == '=') /* no `=' in value */
101 l_value = strlen(value);
103 /* find if already exists */
104 if ((c = findenv(name, &offset))) {
107 if (strlen(c) >= l_value) /* old larger; copy over */
109 } else { /* create new slot */
110 size = sizeof(char *) * (offset + 2);
111 if (saveenv == environ) { /* just increase size */
112 if ((newenv = realloc(saveenv, size)) == NULL)
115 } else { /* get new space */
117 * We don't free here because we don't know if
118 * the first allocation is valid on all OS's
120 if ((saveenv = malloc(size)) == NULL)
122 (void)memcpy(saveenv, environ, size - sizeof(char *));
125 environ[offset + 1] = NULL;
127 for (cc = name; *cc && *cc != '='; ++cc) /* no `=' in name */
130 /* name + `=' + value */
131 if ((environ[offset] = malloc(size + l_value + 2)) == NULL)
134 (void)memcpy(c, name, size);
138 (void)memcpy(c, value, l_value + 1);
144 main(int argc, char *argv[])
146 setenv(argv[1], argv[2], 0);
147 printf("%s\n", getenv(argv[1]));
149 printf("%s\n", getenv(argv[1]));
157 #if defined(__hpux__) || defined(__hpux)
159 * Like strcpy, going backwards and returning the new pointer
162 strrcpy(char *ptr, char *str)
164 int len = strlen(str);
173 char *sys_siglist[] = {
175 "Hangup", /* SIGHUP */
176 "Interrupt", /* SIGINT */
177 "Quit", /* SIGQUIT */
178 "Illegal instruction", /* SIGILL */
179 "Trace/BPT trap", /* SIGTRAP */
180 "IOT trap", /* SIGIOT */
181 "EMT trap", /* SIGEMT */
182 "Floating point exception", /* SIGFPE */
183 "Killed", /* SIGKILL */
184 "Bus error", /* SIGBUS */
185 "Segmentation fault", /* SIGSEGV */
186 "Bad system call", /* SIGSYS */
187 "Broken pipe", /* SIGPIPE */
188 "Alarm clock", /* SIGALRM */
189 "Terminated", /* SIGTERM */
190 "User defined signal 1", /* SIGUSR1 */
191 "User defined signal 2", /* SIGUSR2 */
192 "Child exited", /* SIGCLD */
193 "Power-fail restart", /* SIGPWR */
194 "Virtual timer expired", /* SIGVTALRM */
195 "Profiling timer expired", /* SIGPROF */
196 "I/O possible", /* SIGIO */
197 "Window size changes", /* SIGWINDOW */
198 "Stopped (signal)", /* SIGSTOP */
199 "Stopped", /* SIGTSTP */
200 "Continued", /* SIGCONT */
201 "Stopped (tty input)", /* SIGTTIN */
202 "Stopped (tty output)", /* SIGTTOU */
203 "Urgent I/O condition", /* SIGURG */
204 "Remote lock lost (NFS)", /* SIGLOST */
205 "Signal 31", /* reserved */
206 "DIL signal" /* SIGDIL */
208 #endif /* __hpux__ || __hpux */
210 #if defined(__hpux__) || defined(__hpux)
211 #include <sys/types.h>
212 #include <sys/syscall.h>
213 #include <sys/signal.h>
214 #include <sys/stat.h>
216 #include <sys/time.h>
220 killpg(int pid, int sig)
222 return kill(-pid, sig);
225 #if !defined(__hpux__) && !defined(__hpux)
239 #if !defined(__hpux__) && !defined(__hpux)
241 utimes(char *file, struct timeval tvp[2])
245 t.actime = tvp[0].tv_sec;
246 t.modtime = tvp[1].tv_sec;
247 return(utime(file, &t));
251 #if !defined(BSD) && !defined(d_fileno)
252 # define d_fileno d_ino
255 #ifndef DEV_DEV_COMPARE
256 # define DEV_DEV_COMPARE(a, b) ((a) == (b))
258 #define ISDOT(c) ((c)[0] == '.' && (((c)[1] == '\0') || ((c)[1] == '/')))
259 #define ISDOTDOT(c) ((c)[0] == '.' && ISDOT(&((c)[1])))
262 getwd(char *pathname)
268 struct stat st_root, st_cur, st_next, st_dotdot;
269 char pathbuf[MAXPATHLEN], nextpathbuf[MAXPATHLEN * 2];
270 char *pathptr, *nextpathptr, *cur_name_add;
272 /* find the inode of root */
273 if (stat("/", &st_root) == -1) {
274 (void)sprintf(pathname,
275 "getwd: Cannot stat \"/\" (%s)", strerror(errno));
278 pathbuf[MAXPATHLEN - 1] = '\0';
279 pathptr = &pathbuf[MAXPATHLEN - 1];
280 nextpathbuf[MAXPATHLEN - 1] = '\0';
281 cur_name_add = nextpathptr = &nextpathbuf[MAXPATHLEN - 1];
283 /* find the inode of the current directory */
284 if (lstat(".", &st_cur) == -1) {
285 (void)sprintf(pathname,
286 "getwd: Cannot stat \".\" (%s)", strerror(errno));
289 nextpathptr = strrcpy(nextpathptr, "../");
291 /* Descend to root */
294 /* look if we found root yet */
295 if (st_cur.st_ino == st_root.st_ino &&
296 DEV_DEV_COMPARE(st_cur.st_dev, st_root.st_dev)) {
297 (void)strcpy(pathname, *pathptr != '/' ? "/" : pathptr);
301 /* open the parent directory */
302 if (stat(nextpathptr, &st_dotdot) == -1) {
303 (void)sprintf(pathname,
304 "getwd: Cannot stat directory \"%s\" (%s)",
305 nextpathptr, strerror(errno));
308 if ((dp = opendir(nextpathptr)) == NULL) {
309 (void)sprintf(pathname,
310 "getwd: Cannot open directory \"%s\" (%s)",
311 nextpathptr, strerror(errno));
315 /* look in the parent for the entry with the same inode */
316 if (DEV_DEV_COMPARE(st_dotdot.st_dev, st_cur.st_dev)) {
317 /* Parent has same device. No need to stat every member */
318 for (d = readdir(dp); d != NULL; d = readdir(dp))
319 if (d->d_fileno == st_cur.st_ino)
324 * Parent has a different device. This is a mount point so we
325 * need to stat every member
327 for (d = readdir(dp); d != NULL; d = readdir(dp)) {
328 if (ISDOT(d->d_name) || ISDOTDOT(d->d_name))
330 (void)strcpy(cur_name_add, d->d_name);
331 if (lstat(nextpathptr, &st_next) == -1) {
332 (void)sprintf(pathname,
333 "getwd: Cannot stat \"%s\" (%s)",
334 d->d_name, strerror(errno));
338 /* check if we found it yet */
339 if (st_next.st_ino == st_cur.st_ino &&
340 DEV_DEV_COMPARE(st_next.st_dev, st_cur.st_dev))
345 (void)sprintf(pathname,
346 "getwd: Cannot find \".\" in \"..\"");
351 pathptr = strrcpy(pathptr, d->d_name);
352 pathptr = strrcpy(pathptr, "/");
353 nextpathptr = strrcpy(nextpathptr, "../");
355 *cur_name_add = '\0';
361 #if !defined(HAVE_GETCWD)
371 /* force posix signals */
373 bmake_signal(int s, void (*a)(int)))(int)
375 struct sigaction sa, osa;
378 sigemptyset(&sa.sa_mask);
379 sa.sa_flags = SA_RESTART;
381 if (sigaction(s, &sa, &osa) == -1)
384 return osa.sa_handler;
387 #if !defined(HAVE_VSNPRINTF) || !defined(HAVE_VASPRINTF)
391 #if !defined(HAVE_VSNPRINTF)
392 #if !defined(__osf__)
394 #define STRFLAG (_IOSTRG|_IOWRT) /* no _IOWRT: avoid stdio bug */
397 #define STRFLAG (_IOREAD) /* XXX: Assume svr4 stdio */
403 vsnprintf(char *s, size_t n, const char *fmt, va_list args)
408 fakebuf._flag = STRFLAG;
410 * Some os's are char * _ptr, others are unsigned char *_ptr...
411 * We cast to void * to make everyone happy.
413 fakebuf._ptr = (void *)s;
416 _doprnt(fmt, args, &fakebuf);
418 putc('\0', &fakebuf);
421 return (n-fakebuf._cnt-1);
423 #ifndef _PATH_DEVNULL
424 # define _PATH_DEVNULL "/dev/null"
427 * Rats... we don't want to clobber anything...
428 * do a printf to /dev/null to see how much space we need.
431 int need = 0; /* XXX what's a useful error return? */
434 nullfp = fopen(_PATH_DEVNULL, "w");
436 need = vfprintf(nullfp, fmt, args);
438 (void)vsprintf(s, fmt, args);
445 #if !defined(HAVE_SNPRINTF)
447 snprintf(char *s, size_t n, const char *fmt, ...)
453 rv = vsnprintf(s, n, fmt, ap);
459 #if !defined(HAVE_STRFTIME)
461 strftime(char *buf, size_t len, const char *fmt, const struct tm *tm)
463 static char months[][4] = {
464 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
465 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
483 if (len == 0) return buf - b;
490 s = snprintf(buf, len, "%d", tm->tm_hour);
493 s = snprintf(buf, len, "%02d", tm->tm_min);
496 s = snprintf(buf, len, "%02d", tm->tm_sec);
499 if (tm->tm_mon >= 12)
501 s = snprintf(buf, len, "%s", months[tm->tm_mon]);
504 s = snprintf(buf, len, "%02d", tm->tm_mday);
507 s = snprintf(buf, len, "%d", 1900 + tm->tm_year);
510 s = snprintf(buf, len, "Unsupported format %c",
520 #if !defined(HAVE_KILLPG)
521 #if !defined(__hpux__) && !defined(__hpux)
523 killpg(int pid, int sig)
525 return kill(-pid, sig);