2007-02-15 07:20:47 +03:00
|
|
|
/* Written by Kris Maglione <fbsdaemon at gmail dot com> */
|
|
|
|
/* Public domain */
|
2007-03-22 23:04:47 +03:00
|
|
|
#include <errno.h>
|
2008-01-14 23:42:21 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <signal.h>
|
2007-02-15 07:20:47 +03:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
2007-03-26 08:48:00 +04:00
|
|
|
#include <stdio.h>
|
2007-02-15 07:20:47 +03:00
|
|
|
#include <string.h>
|
2007-03-23 00:00:27 +03:00
|
|
|
#include <unistd.h>
|
2007-03-26 08:48:00 +04:00
|
|
|
#include <util.h>
|
2007-07-03 13:13:01 +04:00
|
|
|
#include <fmt.h>
|
2007-02-15 07:20:47 +03:00
|
|
|
|
2007-07-17 06:16:58 +04:00
|
|
|
typedef struct VFmt VFmt;
|
|
|
|
struct VFmt {
|
|
|
|
const char *fmt;
|
|
|
|
va_list args;
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef VARARGCK
|
|
|
|
# pragma varargck type "V" VFmt*
|
|
|
|
#endif
|
|
|
|
|
2007-07-01 15:08:30 +04:00
|
|
|
static int
|
|
|
|
Vfmt(Fmt *f) {
|
2007-07-17 06:16:58 +04:00
|
|
|
VFmt *vf;
|
2007-07-02 01:43:27 +04:00
|
|
|
int i;
|
2007-07-01 15:08:30 +04:00
|
|
|
|
2007-07-17 06:16:58 +04:00
|
|
|
vf = va_arg(f->args, VFmt*);
|
|
|
|
i = fmtvprint(f, vf->fmt, vf->args);
|
2007-07-02 01:43:27 +04:00
|
|
|
return i;
|
2007-07-01 15:08:30 +04:00
|
|
|
}
|
|
|
|
|
2007-02-15 07:20:47 +03:00
|
|
|
void
|
|
|
|
fatal(const char *fmt, ...) {
|
2007-07-17 06:16:58 +04:00
|
|
|
VFmt fp;
|
2007-03-22 23:04:47 +03:00
|
|
|
|
2007-07-01 15:08:30 +04:00
|
|
|
fmtinstall('V', Vfmt);
|
2007-07-03 13:13:01 +04:00
|
|
|
fmtinstall('\001', Vfmt);
|
2007-02-15 07:20:47 +03:00
|
|
|
|
2007-07-17 06:16:58 +04:00
|
|
|
fp.fmt = fmt;
|
|
|
|
va_start(fp.args, fmt);
|
|
|
|
fprint(2, "%s: fatal: %V\n", argv0, &fp);
|
|
|
|
va_end(fp.args);
|
2007-02-15 07:20:47 +03:00
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2008-01-20 04:29:58 +03:00
|
|
|
char*
|
|
|
|
vsxprint(const char *fmt, va_list ap) {
|
|
|
|
static char* bufs[16];
|
|
|
|
static long nbuf;
|
|
|
|
int id;
|
|
|
|
|
|
|
|
id = nbuf++ % nelem(bufs);
|
|
|
|
if(bufs[id])
|
|
|
|
free(bufs[id]);
|
|
|
|
bufs[id] = vsmprint(fmt, ap);
|
|
|
|
return bufs[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
char*
|
|
|
|
sxprint(const char *fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
char *ret;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
ret = vsxprint(fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-01-14 23:42:21 +03:00
|
|
|
void
|
|
|
|
_die(char *file, int line, char *msg) {
|
|
|
|
fprint(2, "%s: dieing at %s:%d: %s\n",
|
|
|
|
file, line, msg);
|
|
|
|
kill(getpid(), SIGABRT);
|
|
|
|
abort(); /* Adds too many frames:
|
|
|
|
* _die()
|
|
|
|
* abort()
|
|
|
|
* raise(SIGABRT)
|
|
|
|
* kill(getpid(), SIGABRT)
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2007-03-23 00:00:27 +03:00
|
|
|
/* Can't malloc */
|
2007-05-25 01:42:33 +04:00
|
|
|
static void
|
2007-03-23 19:30:50 +03:00
|
|
|
mfatal(char *name, uint size) {
|
2007-03-23 00:00:27 +03:00
|
|
|
const char
|
2007-03-26 08:48:00 +04:00
|
|
|
couldnot[] = ": fatal: Could not ",
|
2007-03-23 00:00:27 +03:00
|
|
|
paren[] = "() ",
|
|
|
|
bytes[] = " bytes\n";
|
2007-07-01 15:08:30 +04:00
|
|
|
char buf[1024];
|
2007-03-23 00:00:27 +03:00
|
|
|
char sizestr[8];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
i = sizeof(sizestr);
|
|
|
|
do {
|
2007-03-23 19:30:50 +03:00
|
|
|
sizestr[--i] = '0' + (size%10);
|
|
|
|
size /= 10;
|
2007-03-23 00:00:27 +03:00
|
|
|
} while(size > 0);
|
|
|
|
|
2007-07-01 15:08:30 +04:00
|
|
|
strlcat(buf, argv0, sizeof(buf));
|
|
|
|
strlcat(buf, couldnot, sizeof(buf));
|
|
|
|
strlcat(buf, name, sizeof(buf));
|
|
|
|
strlcat(buf, paren, sizeof(buf));
|
|
|
|
strlcat(buf, sizestr+i, sizeof(buf));
|
|
|
|
strlcat(buf, bytes, sizeof(buf));
|
|
|
|
write(2, buf, strlen(buf));
|
2007-03-23 19:30:50 +03:00
|
|
|
|
|
|
|
exit(1);
|
2007-03-23 00:00:27 +03:00
|
|
|
}
|
|
|
|
|
2007-02-15 07:20:47 +03:00
|
|
|
void *
|
|
|
|
emalloc(uint size) {
|
|
|
|
void *ret = malloc(size);
|
|
|
|
if(!ret)
|
2007-03-23 00:00:27 +03:00
|
|
|
mfatal("malloc", size);
|
2007-02-15 07:20:47 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
emallocz(uint size) {
|
|
|
|
void *ret = emalloc(size);
|
|
|
|
memset(ret, 0, size);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
erealloc(void *ptr, uint size) {
|
|
|
|
void *ret = realloc(ptr, size);
|
|
|
|
if(!ret)
|
2007-03-23 00:00:27 +03:00
|
|
|
mfatal("realloc", size);
|
2007-02-15 07:20:47 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
estrdup(const char *str) {
|
|
|
|
void *ret = strdup(str);
|
|
|
|
if(!ret)
|
2007-03-23 00:00:27 +03:00
|
|
|
mfatal("strdup", strlen(str));
|
2007-02-15 07:20:47 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint
|
|
|
|
tokenize(char *res[], uint reslen, char *str, char delim) {
|
|
|
|
char *s;
|
|
|
|
uint i;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
s = str;
|
|
|
|
while(i < reslen && *s) {
|
|
|
|
while(*s == delim)
|
|
|
|
*(s++) = '\0';
|
|
|
|
if(*s)
|
|
|
|
res[i++] = s;
|
|
|
|
while(*s && *s != delim)
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
2007-02-12 06:17:20 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
max(int a, int b) {
|
|
|
|
if(a > b)
|
|
|
|
return a;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2007-03-15 01:31:58 +03:00
|
|
|
int
|
|
|
|
min(int a, int b) {
|
|
|
|
if(a < b)
|
|
|
|
return a;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2007-07-17 00:52:35 +04:00
|
|
|
int
|
|
|
|
utflcpy(char *to, const char *from, int l) {
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = utfecpy(to, to+l, from);
|
|
|
|
return p-to;
|
|
|
|
}
|
|
|
|
|
2007-03-03 20:36:44 +03:00
|
|
|
uint
|
2007-03-25 01:35:05 +03:00
|
|
|
strlcat(char *dst, const char *src, uint size) {
|
|
|
|
const char *s;
|
|
|
|
char *d;
|
|
|
|
int n, len;
|
2007-03-03 20:36:44 +03:00
|
|
|
|
|
|
|
d = dst;
|
2007-03-25 01:35:05 +03:00
|
|
|
s = src;
|
|
|
|
n = size;
|
|
|
|
while(n-- > 0 && *d != '\0')
|
|
|
|
d++;
|
|
|
|
len = n;
|
|
|
|
|
|
|
|
while(*s != '\0') {
|
|
|
|
if(n-- > 0)
|
|
|
|
*d++ = *s;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
if(len > 0)
|
|
|
|
*d = '\0';
|
|
|
|
return size - n - 1;
|
2007-03-03 20:36:44 +03:00
|
|
|
}
|