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