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