~ubuntu-branches/ubuntu/dapper/pmake/dapper

« back to all changes in this revision

Viewing changes to util.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve McIntyre
  • Date: 2002-03-12 21:59:55 UTC
  • Revision ID: james.westby@ubuntu.com-20020312215955-i183oxj3rpm464yh
Tags: 1.45-10
* Fixed broken '.Nm ""' directives in the man page. Closes: #137768
* Changed name from "make" to "pmake" in manpage.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*      $NetBSD: util.c,v 1.20 1999/03/19 16:11:02 christos Exp $       */
 
2
 
 
3
/*
 
4
 * Missing stuff from OS's
 
5
 */
 
6
 
 
7
#ifdef MAKE_BOOTSTRAP
 
8
static char rcsid[] = "$NetBSD: util.c,v 1.20 1999/03/19 16:11:02 christos Exp $";
 
9
#else
 
10
#include <sys/cdefs.h>
 
11
#ifndef lint
 
12
__RCSID("$NetBSD: util.c,v 1.20 1999/03/19 16:11:02 christos Exp $");
 
13
#endif
 
14
#endif
 
15
 
 
16
#include <stdio.h>
 
17
#include "make.h"
 
18
#include <sys/param.h>
 
19
 
 
20
#ifndef __STDC__
 
21
# ifndef const
 
22
#  define const
 
23
# endif
 
24
#endif
 
25
 
 
26
#ifdef sun
 
27
 
 
28
 
 
29
 
 
30
extern int errno, sys_nerr;
 
31
extern char *sys_errlist[];
 
32
 
 
33
char *
 
34
strerror(e)
 
35
    int e;
 
36
{
 
37
    static char buf[100];
 
38
    if (e < 0 || e >= sys_nerr) {
 
39
        sprintf(buf, "Unknown error %d", e);
 
40
        return buf;
 
41
    }
 
42
    else
 
43
        return sys_errlist[e];
 
44
}
 
45
#endif
 
46
 
 
47
#ifdef ultrix
 
48
#include <string.h>
 
49
 
 
50
/* strdup
 
51
 *
 
52
 * Make a duplicate of a string.
 
53
 * For systems which lack this function.
 
54
 */
 
55
char *
 
56
strdup(str)
 
57
    const char *str;
 
58
{
 
59
    size_t len;
 
60
    char *p;
 
61
 
 
62
    if (str == NULL)
 
63
        return NULL;
 
64
    len = strlen(str) + 1;
 
65
    p = emalloc(len);
 
66
 
 
67
    return memcpy(p, str, len);
 
68
}
 
69
 
 
70
#endif
 
71
 
 
72
#if defined(sun) || defined(__hpux) || defined(__sgi)
 
73
 
 
74
int
 
75
setenv(name, value, dum)
 
76
    const char *name;
 
77
    const char *value;
 
78
    int dum;
 
79
{
 
80
    register char *p;
 
81
    int len = strlen(name) + strlen(value) + 2; /* = \0 */
 
82
    char *ptr = (char*) emalloc(len);
 
83
 
 
84
    (void) dum;
 
85
 
 
86
    if (ptr == NULL)
 
87
        return -1;
 
88
 
 
89
    p = ptr;
 
90
 
 
91
    while (*name)
 
92
        *p++ = *name++;
 
93
 
 
94
    *p++ = '=';
 
95
 
 
96
    while (*value)
 
97
        *p++ = *value++;
 
98
 
 
99
    *p = '\0';
 
100
 
 
101
    len = putenv(ptr);
 
102
/*    free(ptr); */
 
103
    return len;
 
104
}
 
105
#endif
 
106
 
 
107
#ifdef __hpux
 
108
#include <sys/types.h>
 
109
#include <sys/param.h>
 
110
#include <sys/syscall.h>
 
111
#include <sys/signal.h>
 
112
#include <sys/stat.h>
 
113
#include <stdio.h>
 
114
#include <dirent.h>
 
115
#include <sys/time.h>
 
116
#include <time.h>
 
117
#include <unistd.h>
 
118
 
 
119
 
 
120
int
 
121
killpg(pid, sig)
 
122
    int pid, sig;
 
123
{
 
124
    return kill(-pid, sig);
 
125
}
 
126
 
 
127
void
 
128
srandom(seed)
 
129
    long seed;
 
130
{
 
131
    srand48(seed);
 
132
}
 
133
 
 
134
long
 
135
random()
 
136
{
 
137
    return lrand48();
 
138
}
 
139
 
 
140
/* turn into bsd signals */
 
141
void (*
 
142
signal(s, a)) __P((int))
 
143
    int     s;
 
144
    void (*a) __P((int));
 
145
{
 
146
    struct sigvec osv, sv;
 
147
 
 
148
    (void) sigvector(s, (struct sigvec *) 0, &osv);
 
149
    sv = osv;
 
150
    sv.sv_handler = a;
 
151
#ifdef SV_BSDSIG
 
152
    sv.sv_flags = SV_BSDSIG;
 
153
#endif
 
154
 
 
155
    if (sigvector(s, &sv, (struct sigvec *) 0) == -1)
 
156
        return (BADSIG);
 
157
    return (osv.sv_handler);
 
158
}
 
159
 
 
160
#if !defined(BSD) && !defined(d_fileno)
 
161
# define d_fileno d_ino
 
162
#endif
 
163
 
 
164
#ifndef DEV_DEV_COMPARE
 
165
# define DEV_DEV_COMPARE(a, b) ((a) == (b))
 
166
#endif
 
167
#define ISDOT(c) ((c)[0] == '.' && (((c)[1] == '\0') || ((c)[1] == '/')))
 
168
#define ISDOTDOT(c) ((c)[0] == '.' && ISDOT(&((c)[1])))
 
169
 
 
170
 
 
171
/* strrcpy():
 
172
 *      Like strcpy, going backwards and returning the new pointer
 
173
 */
 
174
static char *
 
175
strrcpy(ptr, str)
 
176
    register char *ptr, *str;
 
177
{
 
178
    register int len = strlen(str);
 
179
 
 
180
    while (len)
 
181
        *--ptr = str[--len];
 
182
 
 
183
    return (ptr);
 
184
} /* end strrcpy */
 
185
 
 
186
/* `getwd()' was used nowhere in the code, was removed */
 
187
 
 
188
char    *sys_siglist[] = {
 
189
        "Signal 0",
 
190
        "Hangup",                       /* SIGHUP    */
 
191
        "Interrupt",                    /* SIGINT    */
 
192
        "Quit",                         /* SIGQUIT   */
 
193
        "Illegal instruction",          /* SIGILL    */
 
194
        "Trace/BPT trap",               /* SIGTRAP   */
 
195
        "IOT trap",                     /* SIGIOT    */
 
196
        "EMT trap",                     /* SIGEMT    */
 
197
        "Floating point exception",     /* SIGFPE    */
 
198
        "Killed",                       /* SIGKILL   */
 
199
        "Bus error",                    /* SIGBUS    */
 
200
        "Segmentation fault",           /* SIGSEGV   */
 
201
        "Bad system call",              /* SIGSYS    */
 
202
        "Broken pipe",                  /* SIGPIPE   */
 
203
        "Alarm clock",                  /* SIGALRM   */
 
204
        "Terminated",                   /* SIGTERM   */
 
205
        "User defined signal 1",        /* SIGUSR1   */
 
206
        "User defined signal 2",        /* SIGUSR2   */
 
207
        "Child exited",                 /* SIGCLD    */
 
208
        "Power-fail restart",           /* SIGPWR    */
 
209
        "Virtual timer expired",        /* SIGVTALRM */
 
210
        "Profiling timer expired",      /* SIGPROF   */
 
211
        "I/O possible",                 /* SIGIO     */
 
212
        "Window size changes",          /* SIGWINDOW */
 
213
        "Stopped (signal)",             /* SIGSTOP   */
 
214
        "Stopped",                      /* SIGTSTP   */
 
215
        "Continued",                    /* SIGCONT   */
 
216
        "Stopped (tty input)",          /* SIGTTIN   */
 
217
        "Stopped (tty output)",         /* SIGTTOU   */
 
218
        "Urgent I/O condition",         /* SIGURG    */
 
219
        "Remote lock lost (NFS)",       /* SIGLOST   */
 
220
        "Signal 31",                    /* reserved  */
 
221
        "DIL signal"                    /* SIGDIL    */
 
222
};
 
223
 
 
224
int
 
225
utimes(file, tvp)
 
226
    char *file;
 
227
    struct timeval tvp[2];
 
228
{
 
229
    struct utimbuf t;
 
230
 
 
231
    t.actime  = tvp[0].tv_sec;
 
232
    t.modtime = tvp[1].tv_sec;
 
233
    return(utime(file, &t));
 
234
}
 
235
 
 
236
 
 
237
#endif /* __hpux */
 
238
 
 
239
#if defined(sun) && defined(__svr4__)
 
240
#include <signal.h>
 
241
 
 
242
/* turn into bsd signals */
 
243
void (*
 
244
signal(s, a)) __P((int))
 
245
    int     s;
 
246
    void (*a) __P((int));
 
247
{
 
248
    struct sigaction sa, osa;
 
249
 
 
250
    sa.sa_handler = a;
 
251
    sigemptyset(&sa.sa_mask);
 
252
    sa.sa_flags = SA_RESTART;
 
253
 
 
254
    if (sigaction(s, &sa, &osa) == -1)
 
255
        return SIG_ERR;
 
256
    else
 
257
        return osa.sa_handler;
 
258
}
 
259
 
 
260
#endif
 
261
 
 
262
#if !defined(BSD4_4) && !defined(__linux__)
 
263
#ifdef __STDC__
 
264
#include <stdarg.h>
 
265
#else
 
266
#include <varargs.h>
 
267
#endif
 
268
 
 
269
#ifdef _IOSTRG
 
270
#define STRFLAG (_IOSTRG|_IOWRT)        /* no _IOWRT: avoid stdio bug */
 
271
#else
 
272
#if 0
 
273
#define STRFLAG (_IOREAD)               /* XXX: Assume svr4 stdio */
 
274
#endif
 
275
#endif
 
276
 
 
277
int
 
278
vsnprintf(s, n, fmt, args)
 
279
        char *s;
 
280
        size_t n;
 
281
        const char *fmt;
 
282
        va_list args;
 
283
{
 
284
#ifdef STRFLAG
 
285
        FILE fakebuf;
 
286
 
 
287
        fakebuf._flag = STRFLAG;
 
288
        /*
 
289
         * Some os's are char * _ptr, others are unsigned char *_ptr...
 
290
         * We cast to void * to make everyone happy.
 
291
         */
 
292
        fakebuf._ptr = (void *) s;
 
293
        fakebuf._cnt = n-1;
 
294
        fakebuf._file = -1;
 
295
        _doprnt(fmt, args, &fakebuf);
 
296
        fakebuf._cnt++;
 
297
        putc('\0', &fakebuf);
 
298
        if (fakebuf._cnt<0)
 
299
            fakebuf._cnt = 0;
 
300
        return (n-fakebuf._cnt-1);
 
301
#else
 
302
        (void) vsprintf(s, fmt, args);
 
303
        return strlen(s);
 
304
#endif
 
305
}
 
306
 
 
307
int
 
308
#ifdef __STDC__
 
309
snprintf(char *s, size_t n, const char *fmt, ...)
 
310
#else
 
311
snprintf(va_alist)
 
312
        va_dcl
 
313
#endif
 
314
{
 
315
        va_list ap;
 
316
        int rv;
 
317
#ifdef __STDC__
 
318
        va_start(ap, fmt);
 
319
#else
 
320
        char *s;
 
321
        size_t n;
 
322
        const char *fmt;
 
323
 
 
324
        va_start(ap);
 
325
 
 
326
        s = va_arg(ap, char *);
 
327
        n = va_arg(ap, size_t);
 
328
        fmt = va_arg(ap, const char *);
 
329
#endif
 
330
        rv = vsnprintf(s, n, fmt, ap);
 
331
        va_end(ap);
 
332
        return rv;
 
333
}
 
334
 
 
335
#ifndef __SVR4
 
336
int
 
337
strftime(buf, len, fmt, tm)
 
338
        char *buf;
 
339
        size_t len;
 
340
        const char *fmt;
 
341
        const struct tm *tm;
 
342
{
 
343
        static char months[][4] = {
 
344
                "Jan", "Feb", "Mar", "Apr", "May", "Jun", 
 
345
                "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
 
346
        };
 
347
 
 
348
        size_t s;
 
349
        char *b = buf;
 
350
 
 
351
        while (*fmt) {
 
352
                if (len == 0)
 
353
                        return buf - b;
 
354
                if (*fmt != '%') {
 
355
                        *buf++ = *fmt++;
 
356
                        len--;
 
357
                        continue;
 
358
                }
 
359
                switch (*fmt++) {
 
360
                case '%':
 
361
                        *buf++ = '%';
 
362
                        len--;
 
363
                        if (len == 0) return buf - b;
 
364
                        /*FALLTHROUGH*/
 
365
                case '\0':
 
366
                        *buf = '%';
 
367
                        s = 1;
 
368
                        break;
 
369
                case 'k':
 
370
                        s = snprintf(buf, len, "%d", tm->tm_hour);
 
371
                        break;
 
372
                case 'M':
 
373
                        s = snprintf(buf, len, "%02d", tm->tm_min);
 
374
                        break;
 
375
                case 'S':
 
376
                        s = snprintf(buf, len, "%02d", tm->tm_sec);
 
377
                        break;
 
378
                case 'b':
 
379
                        if (tm->tm_mon >= 12)
 
380
                                return buf - b;
 
381
                        s = snprintf(buf, len, "%s", months[tm->tm_mon]);
 
382
                        break;
 
383
                case 'd':
 
384
                        s = snprintf(buf, len, "%s", tm->tm_mday);
 
385
                        break;
 
386
                case 'Y':
 
387
                        s = snprintf(buf, len, "%s", 1900 + tm->tm_year);
 
388
                        break;
 
389
                default:
 
390
                        s = snprintf(buf, len, "Unsupported format %c",
 
391
                            fmt[-1]);
 
392
                        break;
 
393
                }
 
394
                buf += s;
 
395
                len -= s;
 
396
        }
 
397
}
 
398
#endif
 
399
#endif