1 /* $NetBSD: util.c,v 1.54 2013/11/26 13:44:41 joerg Exp $ */
4 * Missing stuff from OS's
6 * $Id: util.c,v 1.33 2014/01/02 02:29:49 sjg Exp $
8 #if defined(__MINT__) || defined(__linux__)
15 static char rcsid
[] = "$NetBSD: util.c,v 1.54 2013/11/26 13:44:41 joerg Exp $";
18 __RCSID("$NetBSD: util.c,v 1.54 2013/11/26 13:44:41 joerg 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(__hpux__) && !defined(__hpux)
246 #if !defined(__hpux__) && !defined(__hpux)
248 utimes(char *file
, struct timeval tvp
[2])
252 t
.actime
= tvp
[0].tv_sec
;
253 t
.modtime
= tvp
[1].tv_sec
;
254 return(utime(file
, &t
));
258 #if !defined(BSD) && !defined(d_fileno)
259 # define d_fileno d_ino
262 #ifndef DEV_DEV_COMPARE
263 # define DEV_DEV_COMPARE(a, b) ((a) == (b))
265 #define ISDOT(c) ((c)[0] == '.' && (((c)[1] == '\0') || ((c)[1] == '/')))
266 #define ISDOTDOT(c) ((c)[0] == '.' && ISDOT(&((c)[1])))
269 getwd(char *pathname
)
275 struct stat st_root
, st_cur
, st_next
, st_dotdot
;
276 char pathbuf
[MAXPATHLEN
], nextpathbuf
[MAXPATHLEN
* 2];
277 char *pathptr
, *nextpathptr
, *cur_name_add
;
279 /* find the inode of root */
280 if (stat("/", &st_root
) == -1) {
281 (void)sprintf(pathname
,
282 "getwd: Cannot stat \"/\" (%s)", strerror(errno
));
285 pathbuf
[MAXPATHLEN
- 1] = '\0';
286 pathptr
= &pathbuf
[MAXPATHLEN
- 1];
287 nextpathbuf
[MAXPATHLEN
- 1] = '\0';
288 cur_name_add
= nextpathptr
= &nextpathbuf
[MAXPATHLEN
- 1];
290 /* find the inode of the current directory */
291 if (lstat(".", &st_cur
) == -1) {
292 (void)sprintf(pathname
,
293 "getwd: Cannot stat \".\" (%s)", strerror(errno
));
296 nextpathptr
= strrcpy(nextpathptr
, "../");
298 /* Descend to root */
301 /* look if we found root yet */
302 if (st_cur
.st_ino
== st_root
.st_ino
&&
303 DEV_DEV_COMPARE(st_cur
.st_dev
, st_root
.st_dev
)) {
304 (void)strcpy(pathname
, *pathptr
!= '/' ? "/" : pathptr
);
308 /* open the parent directory */
309 if (stat(nextpathptr
, &st_dotdot
) == -1) {
310 (void)sprintf(pathname
,
311 "getwd: Cannot stat directory \"%s\" (%s)",
312 nextpathptr
, strerror(errno
));
315 if ((dp
= opendir(nextpathptr
)) == NULL
) {
316 (void)sprintf(pathname
,
317 "getwd: Cannot open directory \"%s\" (%s)",
318 nextpathptr
, strerror(errno
));
322 /* look in the parent for the entry with the same inode */
323 if (DEV_DEV_COMPARE(st_dotdot
.st_dev
, st_cur
.st_dev
)) {
324 /* Parent has same device. No need to stat every member */
325 for (d
= readdir(dp
); d
!= NULL
; d
= readdir(dp
))
326 if (d
->d_fileno
== st_cur
.st_ino
)
331 * Parent has a different device. This is a mount point so we
332 * need to stat every member
334 for (d
= readdir(dp
); d
!= NULL
; d
= readdir(dp
)) {
335 if (ISDOT(d
->d_name
) || ISDOTDOT(d
->d_name
))
337 (void)strcpy(cur_name_add
, d
->d_name
);
338 if (lstat(nextpathptr
, &st_next
) == -1) {
339 (void)sprintf(pathname
,
340 "getwd: Cannot stat \"%s\" (%s)",
341 d
->d_name
, strerror(errno
));
345 /* check if we found it yet */
346 if (st_next
.st_ino
== st_cur
.st_ino
&&
347 DEV_DEV_COMPARE(st_next
.st_dev
, st_cur
.st_dev
))
352 (void)sprintf(pathname
,
353 "getwd: Cannot find \".\" in \"..\"");
358 pathptr
= strrcpy(pathptr
, d
->d_name
);
359 pathptr
= strrcpy(pathptr
, "/");
360 nextpathptr
= strrcpy(nextpathptr
, "../");
362 *cur_name_add
= '\0';
368 #if !defined(HAVE_GETCWD)
378 /* force posix signals */
380 bmake_signal(int s
, void (*a
)(int)))(int)
382 struct sigaction sa
, osa
;
385 sigemptyset(&sa
.sa_mask
);
386 sa
.sa_flags
= SA_RESTART
;
388 if (sigaction(s
, &sa
, &osa
) == -1)
391 return osa
.sa_handler
;
394 #if !defined(HAVE_VSNPRINTF) || !defined(HAVE_VASPRINTF)
398 #if !defined(HAVE_VSNPRINTF)
399 #if !defined(__osf__)
401 #define STRFLAG (_IOSTRG|_IOWRT) /* no _IOWRT: avoid stdio bug */
404 #define STRFLAG (_IOREAD) /* XXX: Assume svr4 stdio */
410 vsnprintf(char *s
, size_t n
, const char *fmt
, va_list args
)
415 fakebuf
._flag
= STRFLAG
;
417 * Some os's are char * _ptr, others are unsigned char *_ptr...
418 * We cast to void * to make everyone happy.
420 fakebuf
._ptr
= (void *)s
;
423 _doprnt(fmt
, args
, &fakebuf
);
425 putc('\0', &fakebuf
);
428 return (n
-fakebuf
._cnt
-1);
430 #ifndef _PATH_DEVNULL
431 # define _PATH_DEVNULL "/dev/null"
434 * Rats... we don't want to clobber anything...
435 * do a printf to /dev/null to see how much space we need.
438 int need
= 0; /* XXX what's a useful error return? */
441 nullfp
= fopen(_PATH_DEVNULL
, "w");
443 need
= vfprintf(nullfp
, fmt
, args
);
445 (void)vsprintf(s
, fmt
, args
);
452 #if !defined(HAVE_SNPRINTF)
454 snprintf(char *s
, size_t n
, const char *fmt
, ...)
460 rv
= vsnprintf(s
, n
, fmt
, ap
);
466 #if !defined(HAVE_STRFTIME)
468 strftime(char *buf
, size_t len
, const char *fmt
, const struct tm
*tm
)
470 static char months
[][4] = {
471 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
472 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
490 if (len
== 0) return buf
- b
;
497 s
= snprintf(buf
, len
, "%d", tm
->tm_hour
);
500 s
= snprintf(buf
, len
, "%02d", tm
->tm_min
);
503 s
= snprintf(buf
, len
, "%02d", tm
->tm_sec
);
506 if (tm
->tm_mon
>= 12)
508 s
= snprintf(buf
, len
, "%s", months
[tm
->tm_mon
]);
511 s
= snprintf(buf
, len
, "%02d", tm
->tm_mday
);
514 s
= snprintf(buf
, len
, "%d", 1900 + tm
->tm_year
);
517 s
= snprintf(buf
, len
, "Unsupported format %c",
527 #if !defined(HAVE_KILLPG)
528 #if !defined(__hpux__) && !defined(__hpux)
530 killpg(int pid
, int sig
)
532 return kill(-pid
, sig
);
537 #if !defined(HAVE_WARNX)
539 vwarnx(const char *fmt
, va_list args
)
541 fprintf(stderr
, "%s: ", progname
);
543 vfprintf(stderr
, fmt
, args
);
544 fprintf(stderr
, ": ");
549 #if !defined(HAVE_WARN)
551 vwarn(const char *fmt
, va_list args
)
554 fprintf(stderr
, "%s\n", strerror(errno
));
558 #if !defined(HAVE_ERR)
560 verr(int eval
, const char *fmt
, va_list args
)
567 #if !defined(HAVE_ERRX)
569 verrx(int eval
, const char *fmt
, va_list args
)
576 #if !defined(HAVE_ERR)
578 err(int eval
, const char *fmt
, ...)
588 #if !defined(HAVE_ERRX)
590 errx(int eval
, const char *fmt
, ...)
595 verrx(eval
, fmt
, ap
);
600 #if !defined(HAVE_WARN)
602 warn(const char *fmt
, ...)
612 #if !defined(HAVE_WARNX)
614 warnx(const char *fmt
, ...)