added gentoo ebuilds
[libmixp.git] / libmixp / util.c
blobf0f18103d3d3014494a0d456cc6ec73d4716b538
1 /* Written by Kris Maglione <fbsdaemon at gmail dot com> */
2 /* Public domain */
3 #include <errno.h>
4 #include <stdarg.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <malloc.h>
10 #include "mixp_local.h"
12 void
13 ixp_eprint(const char *fmt, ...) {
14 va_list ap;
15 int err;
17 err = errno;
18 fprintf(stderr, "libixp: fatal: ");
20 va_start(ap, fmt);
21 vfprintf(stderr, fmt, ap);
22 va_end(ap);
24 if(fmt[strlen(fmt)-1] == ':')
25 fprintf(stderr, " %s\n", strerror(err));
26 else
27 fprintf(stderr, "\n");
29 exit(1);
32 /* Can't malloc */
33 static void
34 ixp_mfatal(char *name, unsigned int size) {
35 const char
36 couldnot[] = "libixp: fatal: Could not ",
37 paren[] = "() ",
38 bytes[] = " bytes\n";
39 char sizestr[8];
40 int i;
42 i = sizeof(sizestr);
43 do {
44 sizestr[--i] = '0' + (size%10);
45 size /= 10;
46 } while(size > 0);
48 write(1, couldnot, sizeof(couldnot)-1);
49 write(1, name, strlen(name));
50 write(1, paren, sizeof(paren)-1);
51 write(1, sizestr+i, sizeof(sizestr)-i);
52 write(1, bytes, sizeof(bytes)-1);
54 exit(1);
57 void *
58 ixp_emalloc(unsigned int size) {
59 void *ret = malloc(size);
60 if(!ret)
61 ixp_mfatal("malloc", size);
62 return ret;
65 void *
66 ixp_erealloc(void *ptr, unsigned int size) {
67 void *ret = realloc(ptr, size);
68 if(!ret)
69 ixp_mfatal("realloc", size);
70 return ret;
73 unsigned int ixp_tokenize(char *res[], unsigned int reslen, char *str, char delim) {
74 char *s;
75 unsigned int i;
77 i = 0;
78 s = str;
79 while(i < reslen && *s) {
80 while(*s == delim)
81 *(s++) = '\0';
82 if(*s)
83 res[i++] = s;
84 while(*s && *s != delim)
85 s++;
87 return i;