File: snprintf.c

package info (click to toggle)
leafnode 1.11.11-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 2,468 kB
  • sloc: ansic: 10,914; sh: 1,748; xml: 628; makefile: 291; perl: 84; sed: 4
file content (232 lines) | stat: -rw-r--r-- 5,343 bytes parent folder | download | duplicates (8)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * poor man's snprintf() - for systems which don't have their own
 *
 * This version of snprintf() currently supports only %s, %c, %d, %u,
 * %ld, %lu, %li, %i. It supports the 0, + and width modifiers only for decimal
 * output (%[l]{d|u|i}).
 *
 * Copyright (c) Cornelius Krasel 2000.
 * Modified by Matthias Andree <matthias.andree@gmx.de>.
 * Modifications (C)opyright 2002 - 2003.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include "leafnode.h"

#ifndef HAVE_WORKING_SNPRINTF

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

/** format unsigned value u into p */
static void fmtu(char *p, unsigned long u) {
    unsigned int len = 0;
    unsigned long i = u, ni;
    unsigned char b[22];
    do {
	ni = i / 10;
	b[len++] = i - 10*ni;
	i = ni;
    } while (i);

    while (len) {
	*p++  = "0123456789"[b[--len]];
    }
    *p = '\0';
}

/** format signed value s into p, prefix asign if its nonzero and the
 * value is positive. asign is usually '+' or ' '. */
static inline void fmts(char *p, long s, char asign) {
    if (s < 0) {
	*p++ = '-';
	fmtu(p, -s);
    } else {
	if (asign)
	    *p++ = asign;
	fmtu(p, s);
    }
}

int
ln_vsnprintf(char *str, size_t n, const char *format, va_list ap)
{
    const char *p;
    char *q;
    int flag = 0;
    size_t width = 0;
    char fill = ' ';
    int lflag = 0;		/* checking for longs */
    int asign = 0;		/* always show sign */
    size_t i = 1;		/* because the terminating \0 also counts */
    size_t len, olen;
    char buf[30];		/* buffer for converting longs and ints */

    char *s;
    char c;

    p = format;
    q = str;
    while (p && *p) {
	if ((*p == '%') && !flag) {
	    /* swallow the %, switch to % mode and initialize */
	    lflag = 0;
	    flag = 1;
	    asign = 0; /* can be 0, '+' or later ' ' (' ' is unimplemented) */
	    fill = ' ';
	    width = 0;
	    p++;
	} else if (flag) {
	    /* % mode */
	    switch (*p) {
	    case 's': case 'm': {
		    if (*p == 's')
			s = va_arg(ap, char *);
		    else
			s = strerror(errno);
		    olen = len = strlen(s);
		    if (len > (n - i))
			len = n - i;
		    *q = '\0';
		    strncat(q, s, len);
		    p++;
		    q += len;
		    i += olen;
		    flag = 0;
		    break;
		}
	    case 'u':{
		    unsigned long u;
		    if (lflag) {
			u = va_arg(ap, unsigned long);
		    } else {
			u = (unsigned long)va_arg(ap, unsigned int);
		    }
		    fmtu(buf, u);
		    goto printdec;
		}
	    case 'd': case 'i': {
		    long l;
		    if (lflag) {
			l = va_arg(ap, long);
		    } else {
			l = (long)va_arg(ap, int);
		    }
		    fmts(buf, l, asign);
printdec:
		    olen = len = strlen(buf);
		    if (width) {
			int off = !!asign;
			if (len < width) {
			    switch (fill) {
				case ' ':
				    memmove(buf + width - len, buf, len + 1);
				    memset(buf, fill, width - len);
				    break;
				case '0':
				    memmove(buf + off + width - len, buf + off, len + 1 - off);
				    memset(buf + off, fill, width - len);
				    break;
				default:
				    abort();
			    }
		       }
		       olen = len = strlen(buf);
		    }
		    if (len > (n - i))
			len = n - i;
		    *q = '\0';
		    strncat(q, buf, len);
		    q += len;
		    i += olen;
		    flag = 0;
		    p++;
		    break;
		}
	    case 'l':{
		    /* next argument will be long */
		    lflag = 1;
		    p++;
		    break;
		}
	    case 'c':{
		    c = va_arg(ap, int);
		    flag = 0;
		    if (i < n)
			*q++ = c;
		    i++;
		    p++;
		    break;
		}
	    case '%':{
		    flag = 0;
		    if (i < n)
			*q++ = *p++;
		    i++;
		    break;
		}
	    case '0':
		     if (fill == ' ' && width == 0)
			 fill='0';
		     else
			 width *= 10;
		     p++;
		     break;
	    case '1': case '2': case '3': case '4': case '5':
	    case '6': case '7': case '8': case '9':
		     width = width * 10 + *p - '0';
		     p++;
		     break;
	    case '+':
		     asign = '+';
		     p++;
		     break;
	    default:
		     abort();
		     break;
	    }
	} else {
	    if (i < n) {
		*q++ = *p;
	    }
	    p++;
	    i++;
	}
    }
    va_end(ap);
    *q = '\0';
    return (i - 1);
}

int
ln_snprintf(char *str, size_t n, const char *format, ...)
{
    int r;
    va_list ap;

    va_start(ap, format);
    r = ln_vsnprintf(str, n, format, ap);
    va_end(ap);
    return r;
}

#endif