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