1
/* $NetBSD: util.c,v 1.20 1999/03/19 16:11:02 christos Exp $ */
4
* Missing stuff from OS's
8
static char rcsid[] = "$NetBSD: util.c,v 1.20 1999/03/19 16:11:02 christos Exp $";
10
#include <sys/cdefs.h>
12
__RCSID("$NetBSD: util.c,v 1.20 1999/03/19 16:11:02 christos Exp $");
18
#include <sys/param.h>
30
extern int errno, sys_nerr;
31
extern char *sys_errlist[];
38
if (e < 0 || e >= sys_nerr) {
39
sprintf(buf, "Unknown error %d", e);
43
return sys_errlist[e];
52
* Make a duplicate of a string.
53
* For systems which lack this function.
64
len = strlen(str) + 1;
67
return memcpy(p, str, len);
72
#if defined(sun) || defined(__hpux) || defined(__sgi)
75
setenv(name, value, dum)
81
int len = strlen(name) + strlen(value) + 2; /* = \0 */
82
char *ptr = (char*) emalloc(len);
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>
115
#include <sys/time.h>
124
return kill(-pid, sig);
140
/* turn into bsd signals */
142
signal(s, a)) __P((int))
144
void (*a) __P((int));
146
struct sigvec osv, sv;
148
(void) sigvector(s, (struct sigvec *) 0, &osv);
152
sv.sv_flags = SV_BSDSIG;
155
if (sigvector(s, &sv, (struct sigvec *) 0) == -1)
157
return (osv.sv_handler);
160
#if !defined(BSD) && !defined(d_fileno)
161
# define d_fileno d_ino
164
#ifndef DEV_DEV_COMPARE
165
# define DEV_DEV_COMPARE(a, b) ((a) == (b))
167
#define ISDOT(c) ((c)[0] == '.' && (((c)[1] == '\0') || ((c)[1] == '/')))
168
#define ISDOTDOT(c) ((c)[0] == '.' && ISDOT(&((c)[1])))
172
* Like strcpy, going backwards and returning the new pointer
176
register char *ptr, *str;
178
register int len = strlen(str);
186
/* `getwd()' was used nowhere in the code, was removed */
188
char *sys_siglist[] = {
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 */
227
struct timeval tvp[2];
231
t.actime = tvp[0].tv_sec;
232
t.modtime = tvp[1].tv_sec;
233
return(utime(file, &t));
239
#if defined(sun) && defined(__svr4__)
242
/* turn into bsd signals */
244
signal(s, a)) __P((int))
246
void (*a) __P((int));
248
struct sigaction sa, osa;
251
sigemptyset(&sa.sa_mask);
252
sa.sa_flags = SA_RESTART;
254
if (sigaction(s, &sa, &osa) == -1)
257
return osa.sa_handler;
262
#if !defined(BSD4_4) && !defined(__linux__)
270
#define STRFLAG (_IOSTRG|_IOWRT) /* no _IOWRT: avoid stdio bug */
273
#define STRFLAG (_IOREAD) /* XXX: Assume svr4 stdio */
278
vsnprintf(s, n, fmt, args)
287
fakebuf._flag = STRFLAG;
289
* Some os's are char * _ptr, others are unsigned char *_ptr...
290
* We cast to void * to make everyone happy.
292
fakebuf._ptr = (void *) s;
295
_doprnt(fmt, args, &fakebuf);
297
putc('\0', &fakebuf);
300
return (n-fakebuf._cnt-1);
302
(void) vsprintf(s, fmt, args);
309
snprintf(char *s, size_t n, const char *fmt, ...)
326
s = va_arg(ap, char *);
327
n = va_arg(ap, size_t);
328
fmt = va_arg(ap, const char *);
330
rv = vsnprintf(s, n, fmt, ap);
337
strftime(buf, len, fmt, tm)
343
static char months[][4] = {
344
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
345
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
363
if (len == 0) return buf - b;
370
s = snprintf(buf, len, "%d", tm->tm_hour);
373
s = snprintf(buf, len, "%02d", tm->tm_min);
376
s = snprintf(buf, len, "%02d", tm->tm_sec);
379
if (tm->tm_mon >= 12)
381
s = snprintf(buf, len, "%s", months[tm->tm_mon]);
384
s = snprintf(buf, len, "%s", tm->tm_mday);
387
s = snprintf(buf, len, "%s", 1900 + tm->tm_year);
390
s = snprintf(buf, len, "Unsupported format %c",