aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/rc/util.c
blob: b0be788311481269289d29598d58bec53a0a48b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "rc.h"

void
fatal(char *msg, ...)
{
    va_list args;
    vfprintf(stderr, msg, args);
    va_end(args);

    abort();
}

void*
emalloc(uintptr n)
{
    void *p;
    if(!(p = malloc(n)))
        fatal("out of memory: can't allocate %d bytes", n);

    memset(p, 0, n);
    return p;
}

void*
erealloc(void *p, uintptr n)
{
    void *r;
    if(!(r = realloc(p,n)))
        fatal("out of memory: can't reallocate %d bytes", n);

    return r;
}

void
efree(void *p)
{
    if(p)
        free(p);
    // TODO: log the double free
}


char *bp;

static
void
iacvt(int n)
{
    if(n<0){
        *bp++='-';
        n=-n;	/* doesn't work for n==-inf */
    }
    if(n/10)
        iacvt(n/10);

    *bp++=n%10+'0';
}

void
itoa(char *s, long n)
{
    bp = s;
    iacvt(n);
    *bp='\0';
}