util.c revision 1.13 1 1.13 christos /* $NetBSD: util.c,v 1.13 1997/07/11 20:16:01 christos Exp $ */
2 1.4 christos
3 1.1 cgd /*
4 1.1 cgd * Missing stuff from OS's
5 1.1 cgd */
6 1.4 christos
7 1.12 christos #include <sys/cdefs.h>
8 1.4 christos #ifndef lint
9 1.13 christos __RCSID("$NetBSD: util.c,v 1.13 1997/07/11 20:16:01 christos Exp $");
10 1.4 christos #endif
11 1.4 christos
12 1.1 cgd #include <stdio.h>
13 1.2 glass #include "make.h"
14 1.10 christos #include <sys/param.h>
15 1.1 cgd
16 1.1 cgd #if !__STDC__
17 1.1 cgd # ifndef const
18 1.1 cgd # define const
19 1.1 cgd # endif
20 1.1 cgd #endif
21 1.1 cgd
22 1.1 cgd #ifdef sun
23 1.1 cgd
24 1.1 cgd
25 1.1 cgd
26 1.1 cgd extern int errno, sys_nerr;
27 1.1 cgd extern char *sys_errlist[];
28 1.1 cgd
29 1.1 cgd char *
30 1.8 christos strerror(e)
31 1.8 christos int e;
32 1.1 cgd {
33 1.1 cgd static char buf[100];
34 1.1 cgd if (e < 0 || e >= sys_nerr) {
35 1.1 cgd sprintf(buf, "Unknown error %d", e);
36 1.1 cgd return buf;
37 1.1 cgd }
38 1.1 cgd else
39 1.1 cgd return sys_errlist[e];
40 1.1 cgd }
41 1.1 cgd #endif
42 1.1 cgd
43 1.6 christos #ifdef ultrix
44 1.6 christos #include <string.h>
45 1.6 christos
46 1.6 christos /* strdup
47 1.6 christos *
48 1.6 christos * Make a duplicate of a string.
49 1.6 christos * For systems which lack this function.
50 1.6 christos */
51 1.6 christos char *
52 1.6 christos strdup(str)
53 1.6 christos const char *str;
54 1.6 christos {
55 1.6 christos size_t len;
56 1.9 christos char *p;
57 1.6 christos
58 1.6 christos if (str == NULL)
59 1.6 christos return NULL;
60 1.6 christos len = strlen(str) + 1;
61 1.6 christos if ((p = malloc(len)) == NULL)
62 1.6 christos return NULL;
63 1.6 christos
64 1.6 christos return memcpy(p, str, len);
65 1.6 christos }
66 1.6 christos
67 1.6 christos #endif
68 1.6 christos
69 1.6 christos #if defined(sun) || defined(__hpux) || defined(__sgi)
70 1.1 cgd
71 1.1 cgd int
72 1.1 cgd setenv(name, value, dum)
73 1.8 christos const char *name;
74 1.1 cgd const char *value;
75 1.1 cgd int dum;
76 1.1 cgd {
77 1.1 cgd register char *p;
78 1.1 cgd int len = strlen(name) + strlen(value) + 2; /* = \0 */
79 1.1 cgd char *ptr = (char*) malloc(len);
80 1.1 cgd
81 1.1 cgd (void) dum;
82 1.1 cgd
83 1.1 cgd if (ptr == NULL)
84 1.1 cgd return -1;
85 1.8 christos
86 1.1 cgd p = ptr;
87 1.1 cgd
88 1.8 christos while (*name)
89 1.1 cgd *p++ = *name++;
90 1.1 cgd
91 1.1 cgd *p++ = '=';
92 1.1 cgd
93 1.8 christos while (*value)
94 1.1 cgd *p++ = *value++;
95 1.1 cgd
96 1.1 cgd *p = '\0';
97 1.1 cgd
98 1.1 cgd len = putenv(ptr);
99 1.1 cgd /* free(ptr); */
100 1.1 cgd return len;
101 1.1 cgd }
102 1.1 cgd #endif
103 1.1 cgd
104 1.1 cgd #ifdef __hpux
105 1.1 cgd #include <sys/types.h>
106 1.1 cgd #include <sys/param.h>
107 1.1 cgd #include <sys/syscall.h>
108 1.1 cgd #include <sys/signal.h>
109 1.1 cgd #include <sys/stat.h>
110 1.1 cgd #include <stdio.h>
111 1.1 cgd #include <dirent.h>
112 1.1 cgd #include <sys/time.h>
113 1.1 cgd #include <time.h>
114 1.1 cgd #include <unistd.h>
115 1.1 cgd
116 1.1 cgd
117 1.1 cgd int
118 1.1 cgd killpg(pid, sig)
119 1.1 cgd int pid, sig;
120 1.1 cgd {
121 1.1 cgd return kill(-pid, sig);
122 1.1 cgd }
123 1.1 cgd
124 1.1 cgd void
125 1.1 cgd srandom(seed)
126 1.1 cgd long seed;
127 1.1 cgd {
128 1.1 cgd srand48(seed);
129 1.1 cgd }
130 1.1 cgd
131 1.1 cgd long
132 1.1 cgd random()
133 1.1 cgd {
134 1.1 cgd return lrand48();
135 1.1 cgd }
136 1.1 cgd
137 1.1 cgd /* turn into bsd signals */
138 1.1 cgd void (*
139 1.1 cgd signal(s, a)) ()
140 1.1 cgd int s;
141 1.1 cgd void (*a)();
142 1.1 cgd {
143 1.1 cgd struct sigvec osv, sv;
144 1.1 cgd
145 1.1 cgd (void) sigvector(s, (struct sigvec *) 0, &osv);
146 1.1 cgd sv = osv;
147 1.1 cgd sv.sv_handler = a;
148 1.1 cgd #ifdef SV_BSDSIG
149 1.1 cgd sv.sv_flags = SV_BSDSIG;
150 1.1 cgd #endif
151 1.1 cgd
152 1.1 cgd if (sigvector(s, &sv, (struct sigvec *) 0) == -1)
153 1.1 cgd return (BADSIG);
154 1.1 cgd return (osv.sv_handler);
155 1.1 cgd }
156 1.1 cgd
157 1.1 cgd #if !defined(BSD) && !defined(d_fileno)
158 1.1 cgd # define d_fileno d_ino
159 1.1 cgd #endif
160 1.1 cgd
161 1.1 cgd #ifndef DEV_DEV_COMPARE
162 1.1 cgd # define DEV_DEV_COMPARE(a, b) ((a) == (b))
163 1.1 cgd #endif
164 1.1 cgd #define ISDOT(c) ((c)[0] == '.' && (((c)[1] == '\0') || ((c)[1] == '/')))
165 1.1 cgd #define ISDOTDOT(c) ((c)[0] == '.' && ISDOT(&((c)[1])))
166 1.1 cgd
167 1.1 cgd
168 1.1 cgd /* strrcpy():
169 1.1 cgd * Like strcpy, going backwards and returning the new pointer
170 1.1 cgd */
171 1.1 cgd static char *
172 1.1 cgd strrcpy(ptr, str)
173 1.1 cgd register char *ptr, *str;
174 1.1 cgd {
175 1.1 cgd register int len = strlen(str);
176 1.1 cgd
177 1.1 cgd while (len)
178 1.1 cgd *--ptr = str[--len];
179 1.1 cgd
180 1.1 cgd return (ptr);
181 1.1 cgd } /* end strrcpy */
182 1.1 cgd
183 1.1 cgd
184 1.1 cgd char *
185 1.1 cgd getwd(pathname)
186 1.1 cgd char *pathname;
187 1.1 cgd {
188 1.1 cgd DIR *dp;
189 1.1 cgd struct dirent *d;
190 1.1 cgd extern int errno;
191 1.1 cgd
192 1.1 cgd struct stat st_root, st_cur, st_next, st_dotdot;
193 1.1 cgd char pathbuf[MAXPATHLEN], nextpathbuf[MAXPATHLEN * 2];
194 1.1 cgd char *pathptr, *nextpathptr, *cur_name_add;
195 1.1 cgd
196 1.1 cgd /* find the inode of root */
197 1.1 cgd if (stat("/", &st_root) == -1) {
198 1.1 cgd (void) sprintf(pathname,
199 1.1 cgd "getwd: Cannot stat \"/\" (%s)", strerror(errno));
200 1.1 cgd return (NULL);
201 1.1 cgd }
202 1.1 cgd pathbuf[MAXPATHLEN - 1] = '\0';
203 1.1 cgd pathptr = &pathbuf[MAXPATHLEN - 1];
204 1.1 cgd nextpathbuf[MAXPATHLEN - 1] = '\0';
205 1.1 cgd cur_name_add = nextpathptr = &nextpathbuf[MAXPATHLEN - 1];
206 1.1 cgd
207 1.1 cgd /* find the inode of the current directory */
208 1.1 cgd if (lstat(".", &st_cur) == -1) {
209 1.1 cgd (void) sprintf(pathname,
210 1.1 cgd "getwd: Cannot stat \".\" (%s)", strerror(errno));
211 1.1 cgd return (NULL);
212 1.1 cgd }
213 1.1 cgd nextpathptr = strrcpy(nextpathptr, "../");
214 1.1 cgd
215 1.1 cgd /* Descend to root */
216 1.1 cgd for (;;) {
217 1.1 cgd
218 1.1 cgd /* look if we found root yet */
219 1.1 cgd if (st_cur.st_ino == st_root.st_ino &&
220 1.1 cgd DEV_DEV_COMPARE(st_cur.st_dev, st_root.st_dev)) {
221 1.1 cgd (void) strcpy(pathname, *pathptr != '/' ? "/" : pathptr);
222 1.1 cgd return (pathname);
223 1.1 cgd }
224 1.1 cgd
225 1.1 cgd /* open the parent directory */
226 1.1 cgd if (stat(nextpathptr, &st_dotdot) == -1) {
227 1.1 cgd (void) sprintf(pathname,
228 1.1 cgd "getwd: Cannot stat directory \"%s\" (%s)",
229 1.1 cgd nextpathptr, strerror(errno));
230 1.1 cgd return (NULL);
231 1.1 cgd }
232 1.1 cgd if ((dp = opendir(nextpathptr)) == NULL) {
233 1.1 cgd (void) sprintf(pathname,
234 1.1 cgd "getwd: Cannot open directory \"%s\" (%s)",
235 1.1 cgd nextpathptr, strerror(errno));
236 1.1 cgd return (NULL);
237 1.1 cgd }
238 1.1 cgd
239 1.1 cgd /* look in the parent for the entry with the same inode */
240 1.1 cgd if (DEV_DEV_COMPARE(st_dotdot.st_dev, st_cur.st_dev)) {
241 1.1 cgd /* Parent has same device. No need to stat every member */
242 1.8 christos for (d = readdir(dp); d != NULL; d = readdir(dp))
243 1.1 cgd if (d->d_fileno == st_cur.st_ino)
244 1.1 cgd break;
245 1.1 cgd }
246 1.1 cgd else {
247 1.8 christos /*
248 1.8 christos * Parent has a different device. This is a mount point so we
249 1.8 christos * need to stat every member
250 1.1 cgd */
251 1.1 cgd for (d = readdir(dp); d != NULL; d = readdir(dp)) {
252 1.1 cgd if (ISDOT(d->d_name) || ISDOTDOT(d->d_name))
253 1.1 cgd continue;
254 1.1 cgd (void) strcpy(cur_name_add, d->d_name);
255 1.1 cgd if (lstat(nextpathptr, &st_next) == -1) {
256 1.1 cgd (void) sprintf(pathname, "getwd: Cannot stat \"%s\" (%s)",
257 1.1 cgd d->d_name, strerror(errno));
258 1.1 cgd (void) closedir(dp);
259 1.1 cgd return (NULL);
260 1.1 cgd }
261 1.1 cgd /* check if we found it yet */
262 1.1 cgd if (st_next.st_ino == st_cur.st_ino &&
263 1.8 christos DEV_DEV_COMPARE(st_next.st_dev, st_cur.st_dev))
264 1.1 cgd break;
265 1.1 cgd }
266 1.1 cgd }
267 1.1 cgd if (d == NULL) {
268 1.1 cgd (void) sprintf(pathname, "getwd: Cannot find \".\" in \"..\"");
269 1.1 cgd (void) closedir(dp);
270 1.1 cgd return (NULL);
271 1.1 cgd }
272 1.1 cgd st_cur = st_dotdot;
273 1.1 cgd pathptr = strrcpy(pathptr, d->d_name);
274 1.1 cgd pathptr = strrcpy(pathptr, "/");
275 1.1 cgd nextpathptr = strrcpy(nextpathptr, "../");
276 1.1 cgd (void) closedir(dp);
277 1.1 cgd *cur_name_add = '\0';
278 1.1 cgd }
279 1.1 cgd } /* end getwd */
280 1.1 cgd
281 1.1 cgd
282 1.1 cgd char *sys_siglist[] = {
283 1.1 cgd "Signal 0",
284 1.1 cgd "Hangup", /* SIGHUP */
285 1.1 cgd "Interrupt", /* SIGINT */
286 1.1 cgd "Quit", /* SIGQUIT */
287 1.1 cgd "Illegal instruction", /* SIGILL */
288 1.1 cgd "Trace/BPT trap", /* SIGTRAP */
289 1.1 cgd "IOT trap", /* SIGIOT */
290 1.1 cgd "EMT trap", /* SIGEMT */
291 1.1 cgd "Floating point exception", /* SIGFPE */
292 1.1 cgd "Killed", /* SIGKILL */
293 1.1 cgd "Bus error", /* SIGBUS */
294 1.1 cgd "Segmentation fault", /* SIGSEGV */
295 1.1 cgd "Bad system call", /* SIGSYS */
296 1.1 cgd "Broken pipe", /* SIGPIPE */
297 1.1 cgd "Alarm clock", /* SIGALRM */
298 1.1 cgd "Terminated", /* SIGTERM */
299 1.1 cgd "User defined signal 1", /* SIGUSR1 */
300 1.1 cgd "User defined signal 2", /* SIGUSR2 */
301 1.1 cgd "Child exited", /* SIGCLD */
302 1.1 cgd "Power-fail restart", /* SIGPWR */
303 1.1 cgd "Virtual timer expired", /* SIGVTALRM */
304 1.1 cgd "Profiling timer expired", /* SIGPROF */
305 1.1 cgd "I/O possible", /* SIGIO */
306 1.1 cgd "Window size changes", /* SIGWINDOW */
307 1.1 cgd "Stopped (signal)", /* SIGSTOP */
308 1.1 cgd "Stopped", /* SIGTSTP */
309 1.1 cgd "Continued", /* SIGCONT */
310 1.1 cgd "Stopped (tty input)", /* SIGTTIN */
311 1.1 cgd "Stopped (tty output)", /* SIGTTOU */
312 1.1 cgd "Urgent I/O condition", /* SIGURG */
313 1.1 cgd "Remote lock lost (NFS)", /* SIGLOST */
314 1.1 cgd "Signal 31", /* reserved */
315 1.1 cgd "DIL signal" /* SIGDIL */
316 1.1 cgd };
317 1.1 cgd
318 1.1 cgd int
319 1.1 cgd utimes(file, tvp)
320 1.1 cgd char *file;
321 1.1 cgd struct timeval tvp[2];
322 1.1 cgd {
323 1.1 cgd struct utimbuf t;
324 1.1 cgd
325 1.1 cgd t.actime = tvp[0].tv_sec;
326 1.1 cgd t.modtime = tvp[1].tv_sec;
327 1.1 cgd return(utime(file, &t));
328 1.1 cgd }
329 1.1 cgd
330 1.1 cgd
331 1.1 cgd #endif /* __hpux */
332 1.5 christos
333 1.5 christos #if defined(sun) && defined(__svr4__)
334 1.5 christos #include <signal.h>
335 1.5 christos
336 1.5 christos /* turn into bsd signals */
337 1.5 christos void (*
338 1.5 christos signal(s, a)) ()
339 1.5 christos int s;
340 1.5 christos void (*a)();
341 1.5 christos {
342 1.5 christos struct sigaction sa, osa;
343 1.5 christos
344 1.5 christos sa.sa_handler = a;
345 1.5 christos sigemptyset(&sa.sa_mask);
346 1.5 christos sa.sa_flags = SA_RESTART;
347 1.5 christos
348 1.5 christos if (sigaction(s, &sa, &osa) == -1)
349 1.5 christos return SIG_ERR;
350 1.5 christos else
351 1.5 christos return osa.sa_handler;
352 1.5 christos }
353 1.5 christos
354 1.10 christos #endif
355 1.10 christos
356 1.10 christos #ifndef BSD4_4
357 1.10 christos #ifdef __STDC__
358 1.10 christos #include <stdarg.h>
359 1.10 christos #else
360 1.10 christos #include <varargs.h>
361 1.10 christos #endif
362 1.10 christos
363 1.10 christos #ifdef _IOSTRG
364 1.10 christos #define STRFLAG (_IOSTRG|_IOWRT) /* no _IOWRT: avoid stdio bug */
365 1.10 christos #else
366 1.11 christos #if 0
367 1.10 christos #define STRFLAG (_IOREAD) /* XXX: Assume svr4 stdio */
368 1.10 christos #endif
369 1.11 christos #endif
370 1.10 christos
371 1.10 christos int
372 1.10 christos vsnprintf(s, n, fmt, args)
373 1.10 christos char *s;
374 1.10 christos size_t n;
375 1.10 christos const char *fmt;
376 1.10 christos va_list args;
377 1.10 christos {
378 1.10 christos FILE fakebuf;
379 1.10 christos
380 1.11 christos #ifdef STRFLAG
381 1.10 christos fakebuf._flag = STRFLAG;
382 1.10 christos /*
383 1.10 christos * Some os's are char * _ptr, others are unsigned char *_ptr...
384 1.10 christos * We cast to void * to make everyone happy.
385 1.10 christos */
386 1.10 christos fakebuf._ptr = (void *) s;
387 1.10 christos fakebuf._cnt = n-1;
388 1.10 christos fakebuf._file = -1;
389 1.10 christos _doprnt(fmt, args, &fakebuf);
390 1.10 christos fakebuf._cnt++;
391 1.10 christos putc('\0', &fakebuf);
392 1.10 christos if (fakebuf._cnt<0)
393 1.10 christos fakebuf._cnt = 0;
394 1.10 christos return (n-fakebuf._cnt-1);
395 1.11 christos #else
396 1.13 christos (void) vsprintf(s, fmt, args);
397 1.11 christos return strlen(s);
398 1.11 christos #endif
399 1.10 christos }
400 1.10 christos
401 1.10 christos int
402 1.10 christos #ifdef __STDC__
403 1.10 christos snprintf(char *s, size_t n, const char *fmt, ...)
404 1.10 christos #else
405 1.10 christos snprintf(va_alist)
406 1.10 christos va_dcl
407 1.10 christos #endif
408 1.10 christos {
409 1.10 christos va_list ap;
410 1.10 christos int rv;
411 1.10 christos #ifdef __STDC__
412 1.10 christos va_start(ap, fmt);
413 1.10 christos #else
414 1.10 christos char *s;
415 1.10 christos size_t n;
416 1.10 christos const char *fmt;
417 1.10 christos
418 1.10 christos va_start(ap);
419 1.10 christos
420 1.10 christos s = va_arg(ap, char *);
421 1.10 christos n = va_arg(ap, size_t);
422 1.10 christos fmt = va_arg(ap, const char *);
423 1.10 christos #endif
424 1.10 christos rv = vsnprintf(s, n, fmt, ap);
425 1.10 christos va_end(ap);
426 1.10 christos return rv;
427 1.10 christos }
428 1.5 christos #endif
429