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