]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/snprintf.c
2 * "$Id: snprintf.c 6187 2007-01-10 16:20:42Z mike $"
4 * snprintf functions for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2007 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * _cups_vsnprintf() - Format a string into a fixed size buffer.
29 * _cups_snprintf() - Format a string into a fixed size buffer.
33 * Include necessary headers...
41 #ifndef HAVE_VSNPRINTF
43 * '_cups_vsnprintf()' - Format a string into a fixed size buffer.
46 int /* O - Number of bytes formatted */
47 _cups_vsnprintf(char *buffer
, /* O - Output buffer */
48 size_t bufsize
, /* O - Size of output buffer */
49 const char *format
, /* I - printf-style format string */
50 va_list ap
) /* I - Pointer to additional arguments */
52 char *bufptr
, /* Pointer to position in buffer */
53 *bufend
, /* Pointer to end of buffer */
54 sign
, /* Sign of format width */
55 size
, /* Size character (h, l, L) */
56 type
; /* Format type character */
57 int width
, /* Width of field */
58 prec
; /* Number of characters of precision */
59 char tformat
[100], /* Temporary format string for sprintf() */
60 *tptr
, /* Pointer into temporary format */
61 temp
[1024]; /* Buffer for formatted numbers */
62 char *s
; /* Pointer to string */
63 int slen
; /* Length of string */
64 int bytes
; /* Total number of bytes needed */
68 * Loop through the format string, formatting as needed...
72 bufend
= buffer
+ bufsize
- 1;
84 if (bufptr
&& bufptr
< bufend
) *bufptr
++ = *format
;
89 else if (strchr(" -+#\'", *format
))
100 * Get width from argument...
104 width
= va_arg(ap
, int);
106 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", width
);
107 tptr
+= strlen(tptr
);
113 while (isdigit(*format
& 255))
115 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
118 width
= width
* 10 + *format
++ - '0';
124 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
132 * Get precision from argument...
136 prec
= va_arg(ap
, int);
138 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", prec
);
139 tptr
+= strlen(tptr
);
145 while (isdigit(*format
& 255))
147 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
150 prec
= prec
* 10 + *format
++ - '0';
157 if (*format
== 'l' && format
[1] == 'l')
161 if (tptr
< (tformat
+ sizeof(tformat
) - 2))
169 else if (*format
== 'h' || *format
== 'l' || *format
== 'L')
171 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
180 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
188 case 'E' : /* Floating point formats */
193 if ((width
+ 2) > sizeof(temp
))
196 sprintf(temp
, tformat
, va_arg(ap
, double));
198 bytes
+= (int)strlen(temp
);
202 if ((bufptr
+ strlen(temp
)) > bufend
)
204 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
209 strcpy(bufptr
, temp
);
210 bufptr
+= strlen(temp
);
215 case 'B' : /* Integer formats */
223 if ((width
+ 2) > sizeof(temp
))
226 sprintf(temp
, tformat
, va_arg(ap
, int));
228 bytes
+= (int)strlen(temp
);
232 if ((bufptr
+ strlen(temp
)) > bufend
)
234 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
239 strcpy(bufptr
, temp
);
240 bufptr
+= strlen(temp
);
245 case 'p' : /* Pointer value */
246 if ((width
+ 2) > sizeof(temp
))
249 sprintf(temp
, tformat
, va_arg(ap
, void *));
251 bytes
+= (int)strlen(temp
);
255 if ((bufptr
+ strlen(temp
)) > bufend
)
257 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
262 strcpy(bufptr
, temp
);
263 bufptr
+= strlen(temp
);
268 case 'c' : /* Character or character array */
274 *bufptr
++ = va_arg(ap
, int);
277 if ((bufptr
+ width
) > bufend
)
278 width
= (int)(bufend
- bufptr
);
280 memcpy(bufptr
, va_arg(ap
, char *), (size_t)width
);
286 case 's' : /* String */
287 if ((s
= va_arg(ap
, char *)) == NULL
)
290 slen
= (int)strlen(s
);
291 if (slen
> width
&& prec
!= width
)
298 if ((bufptr
+ width
) > bufend
)
299 width
= (int)(bufend
- bufptr
);
306 strncpy(bufptr
, s
, (size_t)slen
);
307 memset(bufptr
+ slen
, ' ', (size_t)(width
- slen
));
311 memset(bufptr
, ' ', (size_t)(width
- slen
));
312 strncpy(bufptr
+ width
- slen
, s
, (size_t)slen
);
319 case 'n' : /* Output number of chars so far */
320 *(va_arg(ap
, int *)) = bytes
;
328 if (bufptr
&& bufptr
< bufend
)
336 * Nul-terminate the string and return the number of characters needed.
343 #endif /* !HAVE_VSNPRINT */
346 #ifndef HAVE_SNPRINTF
348 * '_cups_snprintf()' - Format a string into a fixed size buffer.
351 int /* O - Number of bytes formatted */
352 _cups_snprintf(char *buffer
, /* O - Output buffer */
353 size_t bufsize
, /* O - Size of output buffer */
354 const char *format
, /* I - printf-style format string */
355 ...) /* I - Additional arguments as needed */
357 int bytes
; /* Number of bytes formatted */
358 va_list ap
; /* Pointer to additional arguments */
361 va_start(ap
, format
);
362 bytes
= vsnprintf(buffer
, bufsize
, format
, ap
);
367 #endif /* !HAVE_SNPRINTF */
371 * End of "$Id: snprintf.c 6187 2007-01-10 16:20:42Z mike $".