]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/snprintf.c
2 * snprintf functions for CUPS.
4 * Copyright 2007-2013 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include "string-private.h"
23 #ifndef HAVE_VSNPRINTF
25 * '_cups_vsnprintf()' - Format a string into a fixed size buffer.
28 int /* O - Number of bytes formatted */
29 _cups_vsnprintf(char *buffer
, /* O - Output buffer */
30 size_t bufsize
, /* O - Size of output buffer */
31 const char *format
, /* I - printf-style format string */
32 va_list ap
) /* I - Pointer to additional arguments */
34 char *bufptr
, /* Pointer to position in buffer */
35 *bufend
, /* Pointer to end of buffer */
36 sign
, /* Sign of format width */
37 size
, /* Size character (h, l, L) */
38 type
; /* Format type character */
39 int width
, /* Width of field */
40 prec
; /* Number of characters of precision */
41 char tformat
[100], /* Temporary format string for sprintf() */
42 *tptr
, /* Pointer into temporary format */
43 temp
[1024]; /* Buffer for formatted numbers */
44 size_t templen
; /* Length of "temp" */
45 char *s
; /* Pointer to string */
46 int slen
; /* Length of string */
47 int bytes
; /* Total number of bytes needed */
51 * Loop through the format string, formatting as needed...
55 bufend
= buffer
+ bufsize
- 1;
67 if (bufptr
&& bufptr
< bufend
) *bufptr
++ = *format
;
72 else if (strchr(" -+#\'", *format
))
83 * Get width from argument...
87 width
= va_arg(ap
, int);
89 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", width
);
96 while (isdigit(*format
& 255))
98 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
101 width
= width
* 10 + *format
++ - '0';
107 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
115 * Get precision from argument...
119 prec
= va_arg(ap
, int);
121 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", prec
);
122 tptr
+= strlen(tptr
);
128 while (isdigit(*format
& 255))
130 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
133 prec
= prec
* 10 + *format
++ - '0';
140 if (*format
== 'l' && format
[1] == 'l')
144 if (tptr
< (tformat
+ sizeof(tformat
) - 2))
152 else if (*format
== 'h' || *format
== 'l' || *format
== 'L')
154 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
163 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
171 case 'E' : /* Floating point formats */
176 if ((width
+ 2) > sizeof(temp
))
179 sprintf(temp
, tformat
, va_arg(ap
, double));
180 templen
= strlen(temp
):
182 bytes
+= (int)templen
;
186 if ((bufptr
+ templen
) > bufend
)
188 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
193 memcpy(bufptr
, temp
, templen
+ 1);
199 case 'B' : /* Integer formats */
207 if ((width
+ 2) > sizeof(temp
))
210 sprintf(temp
, tformat
, va_arg(ap
, int));
211 templen
= strlen(temp
):
213 bytes
+= (int)templen
;
217 if ((bufptr
+ templen
) > bufend
)
219 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
224 memcpy(bufptr
, temp
, templen
+ 1);
230 case 'p' : /* Pointer value */
231 if ((width
+ 2) > sizeof(temp
))
234 sprintf(temp
, tformat
, va_arg(ap
, void *));
235 templen
= strlen(temp
):
237 bytes
+= (int)templen
;
241 if ((bufptr
+ templen
) > bufend
)
243 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
248 memcpy(bufptr
, temp
, templen
+ 1);
254 case 'c' : /* Character or character array */
260 *bufptr
++ = va_arg(ap
, int);
263 if ((bufptr
+ width
) > bufend
)
264 width
= (int)(bufend
- bufptr
);
266 memcpy(bufptr
, va_arg(ap
, char *), (size_t)width
);
272 case 's' : /* String */
273 if ((s
= va_arg(ap
, char *)) == NULL
)
276 slen
= (int)strlen(s
);
277 if (slen
> width
&& prec
!= width
)
284 if ((bufptr
+ width
) > bufend
)
285 width
= (int)(bufend
- bufptr
);
292 memcpy(bufptr
, s
, (size_t)slen
);
293 memset(bufptr
+ slen
, ' ', (size_t)(width
- slen
));
297 memset(bufptr
, ' ', (size_t)(width
- slen
));
298 memcpy(bufptr
+ width
- slen
, s
, (size_t)slen
);
305 case 'n' : /* Output number of chars so far */
306 *(va_arg(ap
, int *)) = bytes
;
314 if (bufptr
&& bufptr
< bufend
)
322 * Nul-terminate the string and return the number of characters needed.
329 #endif /* !HAVE_VSNPRINT */
332 #ifndef HAVE_SNPRINTF
334 * '_cups_snprintf()' - Format a string into a fixed size buffer.
337 int /* O - Number of bytes formatted */
338 _cups_snprintf(char *buffer
, /* O - Output buffer */
339 size_t bufsize
, /* O - Size of output buffer */
340 const char *format
, /* I - printf-style format string */
341 ...) /* I - Additional arguments as needed */
343 int bytes
; /* Number of bytes formatted */
344 va_list ap
; /* Pointer to additional arguments */
347 va_start(ap
, format
);
348 bytes
= vsnprintf(buffer
, bufsize
, format
, ap
);
353 #endif /* !HAVE_SNPRINTF */