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