File: snprintf.c

package info (click to toggle)
ssed 3.60-2.1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,092 kB
  • ctags: 1,641
  • sloc: ansic: 18,803; sh: 4,036; sed: 885; makefile: 417; perl: 373; yacc: 318
file content (338 lines) | stat: -rw-r--r-- 5,664 bytes parent folder | download | duplicates (2)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/* A simplified snprintf, without floating point support */

/* AIX requires this to be the first thing in the file.  */
#ifdef _AIX
#pragma alloca
#endif

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifndef __GNUC__
# if HAVE_ALLOCA_H
#  include <alloca.h>
# else
#  ifndef alloca /* predefined by HP cc +Olibcalls */
    char *alloca ();
#  endif
# endif
#endif

#ifndef BOOTSTRAP
#include <stdio.h>
#endif
#ifdef STDC_HEADERS
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <ctype.h>

#include "basicdefs.h"

#define MINUS_FLAG 1
#define PLUS_FLAG 2
#define SPACE_FLAG 4
#define ALTERNATE_FLAG 8
#define ZERO_FLAG 16

static int
compute_number_length (num, base, width, prec, flags, minusp)
     unsigned long num;
     unsigned base;
     int width;
     int prec;
     int flags;
     int minusp;
{
  int len = 0;
  int i;

  /* length of the number itself */
  do
    {
      len++;
      num /= base;
    }
  while (num);

  /* ...padded to prec characters */
  if (prec > len) len = prec;

  /* ...with alternate prefix */
  if (flags & ALTERNATE_FLAG && (base == 16 || base == 8))
    len += base / 8;

  /* ...with the sign before it */
  if (minusp || (flags & SPACE_FLAG) || (flags & PLUS_FLAG))
    len++;

  /* ...and padded with spaces or zeroes (zeroes go
     before the sign, actually.  This routine does
     not consider that but vsprintf will). */

  return (width > len) ? width : len;
}

/* These can't be made into a function... */

#define PARSE_INT_FORMAT(res, args) \
if (long_flag) \
     res = va_arg(args, long); \
else if (short_flag) \
     res = va_arg(args, int); \
else \
     res = va_arg(args, int)

#define PARSE_UINT_FORMAT(res, args) \
if (long_flag) \
     res = va_arg(args, unsigned long); \
else if (short_flag) \
     res = va_arg(args, unsigned int); \
else \
     res = va_arg(args, unsigned int)

static int
compute_length (fmt, args)
     const char *fmt;
     va_list args;
{
  char c;
  const char *format;
  size_t sz;

  /* Go through the string and find the correct length. */
  sz = 0;
  format = fmt;
  while (c = *format++)
    {
      if (c == '%')
	{
	  int flags = 0;
	  int width = 0;
	  int prec = -1;
	  int long_flag = 0;
	  int short_flag = 0;

	  /* flags */
	  while ((c = *format++))
	    {
	      if (c == '-')
		flags |= MINUS_FLAG;
	      else if (c == '+')
		flags |= PLUS_FLAG;
	      else if (c == ' ')
		flags |= SPACE_FLAG;
	      else if (c == '#')
		flags |= ALTERNATE_FLAG;
	      else if (c == '0')
		flags |= ZERO_FLAG;
	      else
		break;
	    }

	  if (flags & PLUS_FLAG)
	    flags &= ~SPACE_FLAG;
	  if (flags & MINUS_FLAG)
	    flags &= ~ZERO_FLAG;

	  /* width */
	  if (isdigit (c))
	    do
	      {
		width = width * 10 + c - '0';
		c = *format++;
	      }
	    while (isdigit (c));
	  else if (c == '*')
	    {
	      width = va_arg (args, int);
	      c = *format++;
	    }

	  /* precision */
	  if (c == '.')
	    {
	      prec = 0;
	      c = *format++;
	      if (isdigit (c))
		do
		  {
		    prec = prec * 10 + c - '0';
		    c = *format++;
		  }
		while (isdigit (c));
	      else if (c == '*')
		{
		  prec = va_arg (args, int);
		  c = *format++;
		}
	    }

	  /* size */

	  if (c == 'h')
	    {
	      short_flag = 1;
	      c = *format++;
	    }
	  else if (c == 'l')
	    {
	      long_flag = 1;
	      c = *format++;
	    }

	  switch (c)
	    {
	    case 'c':
	      sz += width + 1;
	      break;
	    case 's':
	      {
		int len;
		char *arg;

		arg = va_arg (args, char *);
		len = strlen (arg);

		if (prec != -1 || len > prec)
		  len = prec;

		sz += (width > len) ? width : len;
		break;
	      }

	    case 'd':
	    case 'i':
	      {
		long arg;
		unsigned long num;
		int minusp = 0;

		PARSE_INT_FORMAT (arg, args);

		if (arg < 0)
		  {
		    minusp = 1;
		    num = -arg;
		  }
		else
		  num = arg;

		sz += compute_number_length(num, 10, width,
						prec, flags, minusp);
		break;
	      }
	    case 'u':
	      {
		unsigned long arg;

		PARSE_UINT_FORMAT (arg, args);

		sz += compute_number_length(arg, 10, width,
						prec, flags, 0);
		break;
	      }
	    case 'o':
	      {
		unsigned long arg;

		PARSE_UINT_FORMAT (arg, args);

		sz += compute_number_length(arg, 8, width,
						prec, flags, 0);
		break;
	      }

	    case 'p':
	      flags |= ALTERNATE_FLAG;
	      /* fall through */

	    case 'x':
	    case 'X':
	      {
		unsigned long arg;

		PARSE_UINT_FORMAT (arg, args);

		sz += compute_number_length(arg, 16, width,
						prec, flags, 0);
		break;
	      }

	    case 'n':
	      break;

	    case '%':
	      sz++;
	      break;

	    default:
	      sz += 2;
	      break;
	    }
	}
      else
	sz++;
    }

  return sz;
  }

static int
my_vsnprintf (str, sz, fmt, args)
     char *str;
     size_t sz;
     const char *fmt;
     va_list args;
{
  char *buf;
  size_t realsz = compute_length(fmt, args);

  /* Here, we have to count the null character at the end! */
  if (realsz + 1 < sz)
    buf = alloca (realsz + 1);
  else
    buf = str;

  realsz = vsprintf(buf, fmt, args);
  if (buf != str)
    memcpy(str, buf, sz);

  return realsz;
}

int
#ifdef HAVE_STDARG_H
snprintf (char *str, size_t sz, const char *format,...)
#else
snprintf (va_alist)
     va_dcl
#endif
{
  va_list args;
  int ret;
#ifndef HAVE_STDARG_H
  char *str;
  size_t sz;
  char *format;

  va_start (args);
  str = va_arg(args, char *);
  sz = va_arg(args, size_t);
  format = va_arg(args, char *);
#else
  va_start (args, format);
#endif

  ret = my_vsnprintf (str, sz, format, args);
  va_end (args);
  return ret;
}