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