2 * Missing stuff from OS's
4 * $FreeBSD: src/usr.bin/make/util.c,v 1.5.2.2 2001/02/13 03:13:58 will Exp $
5 * $DragonFly: src/usr.bin/make/util.c,v 1.2 2003/06/17 04:29:29 dillon Exp $
19 extern int errno, sys_nerr;
20 extern char *sys_errlist[];
27 if (e < 0 || e >= sys_nerr) {
28 sprintf(buf, "Unknown error %d", e);
32 return sys_errlist[e];
41 * Make a duplicate of a string.
42 * For systems which lack this function.
52 len = strlen(str) + 1;
53 if ((p = malloc(len)) == NULL)
56 return memcpy(p, str, len);
61 #if defined(sun) || defined(__hpux) || defined(__sgi)
64 setenv(name, value, dum)
70 int len = strlen(name) + strlen(value) + 2; /* = \0 */
71 char *ptr = (char*) malloc(len);
97 #include <sys/types.h>
98 #include <sys/param.h>
99 #include <sys/syscall.h>
100 #include <sys/signal.h>
101 #include <sys/stat.h>
104 #include <sys/time.h>
113 return kill(-pid, sig);
129 /* turn into bsd signals */
135 struct sigvec osv, sv;
137 (void) sigvector(s, (struct sigvec *) 0, &osv);
141 sv.sv_flags = SV_BSDSIG;
144 if (sigvector(s, &sv, (struct sigvec *) 0) == -1)
146 return (osv.sv_handler);
149 #if !defined(BSD) && !defined(d_fileno)
150 # define d_fileno d_ino
153 #ifndef DEV_DEV_COMPARE
154 # define DEV_DEV_COMPARE(a, b) ((a) == (b))
158 * Like strcpy, going backwards and returning the new pointer
162 register char *ptr, *str;
164 register int len = strlen(str);
180 struct stat st_root, st_cur, st_next, st_dotdot;
181 char pathbuf[MAXPATHLEN], nextpathbuf[MAXPATHLEN * 2];
182 char *pathptr, *nextpathptr, *cur_name_add;
184 /* find the inode of root */
185 if (stat("/", &st_root) == -1) {
186 (void) sprintf(pathname,
187 "getwd: Cannot stat \"/\" (%s)", strerror(errno));
190 pathbuf[MAXPATHLEN - 1] = '\0';
191 pathptr = &pathbuf[MAXPATHLEN - 1];
192 nextpathbuf[MAXPATHLEN - 1] = '\0';
193 cur_name_add = nextpathptr = &nextpathbuf[MAXPATHLEN - 1];
195 /* find the inode of the current directory */
196 if (lstat(".", &st_cur) == -1) {
197 (void) sprintf(pathname,
198 "getwd: Cannot stat \".\" (%s)", strerror(errno));
201 nextpathptr = strrcpy(nextpathptr, "../");
203 /* Descend to root */
206 /* look if we found root yet */
207 if (st_cur.st_ino == st_root.st_ino &&
208 DEV_DEV_COMPARE(st_cur.st_dev, st_root.st_dev)) {
209 (void) strcpy(pathname, *pathptr != '/' ? "/" : pathptr);
213 /* open the parent directory */
214 if (stat(nextpathptr, &st_dotdot) == -1) {
215 snprintf(pathname, sizeof(pathname),
216 "getwd: Cannot stat directory \"%s\" (%s)",
217 nextpathptr, strerror(errno));
220 if ((dp = opendir(nextpathptr)) == NULL) {
221 snprintf(pathname, sizeof(pathname),
222 "getwd: Cannot open directory \"%s\" (%s)",
223 nextpathptr, strerror(errno));
227 /* look in the parent for the entry with the same inode */
228 if (DEV_DEV_COMPARE(st_dotdot.st_dev, st_cur.st_dev)) {
229 /* Parent has same device. No need to stat every member */
230 for (d = readdir(dp); d != NULL; d = readdir(dp))
231 if (d->d_fileno == st_cur.st_ino)
236 * Parent has a different device. This is a mount point so we
237 * need to stat every member
239 for (d = readdir(dp); d != NULL; d = readdir(dp)) {
240 if (ISDOT(d->d_name) || ISDOTDOT(d->d_name))
242 (void) strcpy(cur_name_add, d->d_name);
243 if (lstat(nextpathptr, &st_next) == -1) {
244 snprintf(pathname, sizeof(pathname), "getwd: Cannot stat \"%s\" (%s)",
245 d->d_name, strerror(errno));
249 /* check if we found it yet */
250 if (st_next.st_ino == st_cur.st_ino &&
251 DEV_DEV_COMPARE(st_next.st_dev, st_cur.st_dev))
256 (void) sprintf(pathname, "getwd: Cannot find \".\" in \"..\"");
261 pathptr = strrcpy(pathptr, d->d_name);
262 pathptr = strrcpy(pathptr, "/");
263 nextpathptr = strrcpy(nextpathptr, "../");
265 *cur_name_add = '\0';
270 char *sys_siglist[] = {
272 "Hangup", /* SIGHUP */
273 "Interrupt", /* SIGINT */
274 "Quit", /* SIGQUIT */
275 "Illegal instruction", /* SIGILL */
276 "Trace/BPT trap", /* SIGTRAP */
277 "IOT trap", /* SIGIOT */
278 "EMT trap", /* SIGEMT */
279 "Floating point exception", /* SIGFPE */
280 "Killed", /* SIGKILL */
281 "Bus error", /* SIGBUS */
282 "Segmentation fault", /* SIGSEGV */
283 "Bad system call", /* SIGSYS */
284 "Broken pipe", /* SIGPIPE */
285 "Alarm clock", /* SIGALRM */
286 "Terminated", /* SIGTERM */
287 "User defined signal 1", /* SIGUSR1 */
288 "User defined signal 2", /* SIGUSR2 */
289 "Child exited", /* SIGCLD */
290 "Power-fail restart", /* SIGPWR */
291 "Virtual timer expired", /* SIGVTALRM */
292 "Profiling timer expired", /* SIGPROF */
293 "I/O possible", /* SIGIO */
294 "Window size changes", /* SIGWINDOW */
295 "Stopped (signal)", /* SIGSTOP */
296 "Stopped", /* SIGTSTP */
297 "Continued", /* SIGCONT */
298 "Stopped (tty input)", /* SIGTTIN */
299 "Stopped (tty output)", /* SIGTTOU */
300 "Urgent I/O condition", /* SIGURG */
301 "Remote lock lost (NFS)", /* SIGLOST */
302 "Signal 31", /* reserved */
303 "DIL signal" /* SIGDIL */
309 struct timeval tvp[2];
313 t.actime = tvp[0].tv_sec;
314 t.modtime = tvp[1].tv_sec;
315 return(utime(file, &t));
321 #if defined(sun) && defined(__svr4__)
324 /* turn into bsd signals */
330 struct sigaction sa, osa;
333 sigemptyset(&sa.sa_mask);
334 sa.sa_flags = SA_RESTART;
336 if (sigaction(s, &sa, &osa) == -1)
339 return osa.sa_handler;