File: memutil.c

package info (click to toggle)
tcng 10b-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,632 kB
  • ctags: 2,515
  • sloc: ansic: 19,038; pascal: 4,640; yacc: 2,619; sh: 1,908; perl: 1,546; lex: 772; makefile: 755
file content (152 lines) | stat: -rw-r--r-- 2,537 bytes parent folder | download | duplicates (5)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * memutil.c - Utility functions for dynamic memory allocation
 *
 * Written 2001,2002 by Werner Almesberger
 * Copyright 2001 EPFL-ICA
 * Copyright 2001,2002 Bivio Networks, Network Robots
 */


#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>

#include "memutil.h"


/* ----- Malloc wrappers --------------------------------------------------- */


void *alloc(size_t size)
{
    void *new;

    new = malloc(size);
    if (new) return new;
    perror("malloc");
    exit(1);
}


char *stralloc(const char *s)
{
    char *new;

    new = strdup(s);
    if (new) return new;
    perror("strdup");
    exit(1);
}


/* ----- "Safe" sprintf ---------------------------------------------------- */


int log_8(unsigned long value)
{
    int len;

    if (!value) return 1;
    for (len = 0; value; value /= 8) len++;
    return len;
}


int log_10(unsigned long value)
{
    int len;

    if (!value) return 1;
    for (len = 0; value; value /= 10) len++;
    return len;
}


int log_16(unsigned long value)
{
    int len;

    if (!value) return 1;
    for (len = 0; value; value /= 16) len++;
    return len;
}


char *alloc_sprintf(const char *fmt,...)
{
    va_list ap;
    const char *s;
    char *buf;
    int total,real_len;

    total = 0;
    va_start(ap,fmt);
    for (s = fmt; *s; s++)
	if (*s != '%') total++;
	else {
	    int len = 0,min = 0;

	    s++;
	    if (isdigit(*s)) {
		char *end;

		min = strtoul(s,&end,10);
		s = end;
	    }
	    if (*s == '*') {
		min = va_arg(ap,int);
		s++;
	    }
	    if (*s == '.') {
		if (s[1] != '*' || s[2] != 's') abort();
		len = va_arg(ap,int);
		real_len = strlen(va_arg(ap,char *));
		if (real_len < len) len = real_len;
		s += 2;
	    }
	    else {
		int tmp;

		switch (*s) {
		    case 'c':
			(void) va_arg(ap,int); /* converted from char */
			len = 1;
			break;
		    case 's':
			len = strlen(va_arg(ap,char *));
			break;
		    case 'd':
			tmp = va_arg(ap,int);
			if (tmp < 0) {
			    tmp = -tmp;
			    len++;
			}
			len += log_10(tmp);
		        break;
		    case 'u':
			len = log_10(va_arg(ap,unsigned));
		        break;
		    case 'x':
			len = log_16(va_arg(ap,unsigned));
		        break;
		    case '%':
			len = 1;
			break;
		    default:
			abort();
		}
	    }
	    total += len < min ? min : len;
	}
    va_end(ap);
    buf = alloc(total+1);
    va_start(ap,fmt);
    real_len = vsprintf(buf,fmt,ap);
    va_end(ap);
    assert(real_len == total);
    return buf;
}