1 /* $NetBSD: util.c,v 1.51 2011/04/02 07:58:30 mbalmer Exp $ */
4 * Missing stuff from OS's
8 static char rcsid
[] = "$NetBSD: util.c,v 1.51 2011/04/02 07:58:30 mbalmer Exp $";
10 #include <sys/cdefs.h>
12 __RCSID("$NetBSD: util.c,v 1.51 2011/04/02 07:58:30 mbalmer Exp $");
16 #include <sys/param.h>
25 #if !defined(MAKE_NATIVE) && !defined(HAVE_STRERROR)
26 extern int errno
, sys_nerr
;
27 extern char *sys_errlist
[];
33 if (e
< 0 || e
>= sys_nerr
) {
34 snprintf(buf
, sizeof(buf
), "Unknown error %d", e
);
38 return sys_errlist
[e
];
42 #if !defined(MAKE_NATIVE) && !defined(HAVE_SETENV)
43 extern char **environ
;
46 findenv(const char *name
, int *offset
)
51 for (i
= 0; (q
= environ
[i
]); i
++) {
55 if (strncmp(name
, q
, len
= p
- q
) == 0) {
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 setenv(const char *name
, const char *value
, int rewrite
)
86 static char **saveenv
; /* copy of previously allocated space */
92 if (name
== NULL
|| value
== NULL
) {
97 if (*value
== '=') /* no `=' in value */
99 l_value
= strlen(value
);
101 /* find if already exists */
102 if ((c
= findenv(name
, &offset
))) {
105 if (strlen(c
) >= l_value
) /* old larger; copy over */
107 } else { /* create new slot */
108 size
= sizeof(char *) * (offset
+ 2);
109 if (saveenv
== environ
) { /* just increase size */
110 if ((newenv
= realloc(saveenv
, size
)) == NULL
)
113 } else { /* get new space */
115 * We don't free here because we don't know if
116 * the first allocation is valid on all OS's
118 if ((saveenv
= malloc(size
)) == NULL
)
120 (void)memcpy(saveenv
, environ
, size
- sizeof(char *));
123 environ
[offset
+ 1] = NULL
;
125 for (cc
= name
; *cc
&& *cc
!= '='; ++cc
) /* no `=' in name */
128 /* name + `=' + value */
129 if ((environ
[offset
] = malloc(size
+ l_value
+ 2)) == NULL
)
132 (void)memcpy(c
, name
, size
);
136 (void)memcpy(c
, value
, l_value
+ 1);
142 main(int argc
, char *argv
[])
144 setenv(argv
[1], argv
[2], 0);
145 printf("%s\n", getenv(argv
[1]));
147 printf("%s\n", getenv(argv
[1]));
154 #if defined(__hpux__) || defined(__hpux)
156 * Like strcpy, going backwards and returning the new pointer
159 strrcpy(char *ptr
, char *str
)
161 int len
= strlen(str
);
169 char *sys_siglist
[] = {
171 "Hangup", /* SIGHUP */
172 "Interrupt", /* SIGINT */
173 "Quit", /* SIGQUIT */
174 "Illegal instruction", /* SIGILL */
175 "Trace/BPT trap", /* SIGTRAP */
176 "IOT trap", /* SIGIOT */
177 "EMT trap", /* SIGEMT */
178 "Floating point exception", /* SIGFPE */
179 "Killed", /* SIGKILL */
180 "Bus error", /* SIGBUS */
181 "Segmentation fault", /* SIGSEGV */
182 "Bad system call", /* SIGSYS */
183 "Broken pipe", /* SIGPIPE */
184 "Alarm clock", /* SIGALRM */
185 "Terminated", /* SIGTERM */
186 "User defined signal 1", /* SIGUSR1 */
187 "User defined signal 2", /* SIGUSR2 */
188 "Child exited", /* SIGCLD */
189 "Power-fail restart", /* SIGPWR */
190 "Virtual timer expired", /* SIGVTALRM */
191 "Profiling timer expired", /* SIGPROF */
192 "I/O possible", /* SIGIO */
193 "Window size changes", /* SIGWINDOW */
194 "Stopped (signal)", /* SIGSTOP */
195 "Stopped", /* SIGTSTP */
196 "Continued", /* SIGCONT */
197 "Stopped (tty input)", /* SIGTTIN */
198 "Stopped (tty output)", /* SIGTTOU */
199 "Urgent I/O condition", /* SIGURG */
200 "Remote lock lost (NFS)", /* SIGLOST */
201 "Signal 31", /* reserved */
202 "DIL signal" /* SIGDIL */
204 #endif /* __hpux__ || __hpux */
206 #if defined(__hpux__) || defined(__hpux)
207 #include <sys/types.h>
208 #include <sys/syscall.h>
209 #include <sys/signal.h>
210 #include <sys/stat.h>
212 #include <sys/time.h>
216 killpg(int pid
, int sig
)
218 return kill(-pid
, sig
);
221 #if !defined(__hpux__) && !defined(__hpux)
235 #if !defined(__hpux__) && !defined(__hpux)
237 utimes(char *file
, struct timeval tvp
[2])
241 t
.actime
= tvp
[0].tv_sec
;
242 t
.modtime
= tvp
[1].tv_sec
;
243 return(utime(file
, &t
));
247 #if !defined(BSD) && !defined(d_fileno)
248 # define d_fileno d_ino
251 #ifndef DEV_DEV_COMPARE
252 # define DEV_DEV_COMPARE(a, b) ((a) == (b))
254 #define ISDOT(c) ((c)[0] == '.' && (((c)[1] == '\0') || ((c)[1] == '/')))
255 #define ISDOTDOT(c) ((c)[0] == '.' && ISDOT(&((c)[1])))
258 getwd(char *pathname
)
264 struct stat st_root
, st_cur
, st_next
, st_dotdot
;
265 char pathbuf
[MAXPATHLEN
], nextpathbuf
[MAXPATHLEN
* 2];
266 char *pathptr
, *nextpathptr
, *cur_name_add
;
268 /* find the inode of root */
269 if (stat("/", &st_root
) == -1) {
270 (void)sprintf(pathname
,
271 "getwd: Cannot stat \"/\" (%s)", strerror(errno
));
274 pathbuf
[MAXPATHLEN
- 1] = '\0';
275 pathptr
= &pathbuf
[MAXPATHLEN
- 1];
276 nextpathbuf
[MAXPATHLEN
- 1] = '\0';
277 cur_name_add
= nextpathptr
= &nextpathbuf
[MAXPATHLEN
- 1];
279 /* find the inode of the current directory */
280 if (lstat(".", &st_cur
) == -1) {
281 (void)sprintf(pathname
,
282 "getwd: Cannot stat \".\" (%s)", strerror(errno
));
285 nextpathptr
= strrcpy(nextpathptr
, "../");
287 /* Descend to root */
290 /* look if we found root yet */
291 if (st_cur
.st_ino
== st_root
.st_ino
&&
292 DEV_DEV_COMPARE(st_cur
.st_dev
, st_root
.st_dev
)) {
293 (void)strcpy(pathname
, *pathptr
!= '/' ? "/" : pathptr
);
297 /* open the parent directory */
298 if (stat(nextpathptr
, &st_dotdot
) == -1) {
299 (void)sprintf(pathname
,
300 "getwd: Cannot stat directory \"%s\" (%s)",
301 nextpathptr
, strerror(errno
));
304 if ((dp
= opendir(nextpathptr
)) == NULL
) {
305 (void)sprintf(pathname
,
306 "getwd: Cannot open directory \"%s\" (%s)",
307 nextpathptr
, strerror(errno
));
311 /* look in the parent for the entry with the same inode */
312 if (DEV_DEV_COMPARE(st_dotdot
.st_dev
, st_cur
.st_dev
)) {
313 /* Parent has same device. No need to stat every member */
314 for (d
= readdir(dp
); d
!= NULL
; d
= readdir(dp
))
315 if (d
->d_fileno
== st_cur
.st_ino
)
320 * Parent has a different device. This is a mount point so we
321 * need to stat every member
323 for (d
= readdir(dp
); d
!= NULL
; d
= readdir(dp
)) {
324 if (ISDOT(d
->d_name
) || ISDOTDOT(d
->d_name
))
326 (void)strcpy(cur_name_add
, d
->d_name
);
327 if (lstat(nextpathptr
, &st_next
) == -1) {
328 (void)sprintf(pathname
,
329 "getwd: Cannot stat \"%s\" (%s)",
330 d
->d_name
, strerror(errno
));
334 /* check if we found it yet */
335 if (st_next
.st_ino
== st_cur
.st_ino
&&
336 DEV_DEV_COMPARE(st_next
.st_dev
, st_cur
.st_dev
))
341 (void)sprintf(pathname
,
342 "getwd: Cannot find \".\" in \"..\"");
347 pathptr
= strrcpy(pathptr
, d
->d_name
);
348 pathptr
= strrcpy(pathptr
, "/");
349 nextpathptr
= strrcpy(nextpathptr
, "../");
351 *cur_name_add
= '\0';
356 /* force posix signals */
358 bmake_signal(int s
, void (*a
)(int)))(int)
360 struct sigaction sa
, osa
;
363 sigemptyset(&sa
.sa_mask
);
364 sa
.sa_flags
= SA_RESTART
;
366 if (sigaction(s
, &sa
, &osa
) == -1)
369 return osa
.sa_handler
;
372 #if !defined(MAKE_NATIVE) && !defined(HAVE_VSNPRINTF)
375 #if !defined(__osf__)
377 #define STRFLAG (_IOSTRG|_IOWRT) /* no _IOWRT: avoid stdio bug */
380 #define STRFLAG (_IOREAD) /* XXX: Assume svr4 stdio */
386 vsnprintf(char *s
, size_t n
, const char *fmt
, va_list 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
);
412 snprintf(char *s
, size_t n
, const char *fmt
, ...)
418 rv
= vsnprintf(s
, n
, fmt
, ap
);
423 #if !defined(MAKE_NATIVE) && !defined(HAVE_STRFTIME)
425 strftime(char *buf
, size_t len
, const char *fmt
, const struct tm
*tm
)
427 static char months
[][4] = {
428 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
429 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
447 if (len
== 0) return buf
- b
;
454 s
= snprintf(buf
, len
, "%d", tm
->tm_hour
);
457 s
= snprintf(buf
, len
, "%02d", tm
->tm_min
);
460 s
= snprintf(buf
, len
, "%02d", tm
->tm_sec
);
463 if (tm
->tm_mon
>= 12)
465 s
= snprintf(buf
, len
, "%s", months
[tm
->tm_mon
]);
468 s
= snprintf(buf
, len
, "%02d", tm
->tm_mday
);
471 s
= snprintf(buf
, len
, "%d", 1900 + tm
->tm_year
);
474 s
= snprintf(buf
, len
, "Unsupported format %c",