]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/snprintf.c
2 * snprintf functions for CUPS.
4 * Copyright © 2007-2019 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
15 #include "string-private.h"
18 #ifndef HAVE_VSNPRINTF
20 * '_cups_vsnprintf()' - Format a string into a fixed size buffer.
23 int /* O - Number of bytes formatted */
24 _cups_vsnprintf(char *buffer
, /* O - Output buffer */
25 size_t bufsize
, /* O - Size of output buffer */
26 const char *format
, /* I - printf-style format string */
27 va_list ap
) /* I - Pointer to additional arguments */
29 char *bufptr
, /* Pointer to position in buffer */
30 *bufend
, /* Pointer to end of buffer */
31 sign
, /* Sign of format width */
32 size
, /* Size character (h, l, L) */
33 type
; /* Format type character */
34 int width
, /* Width of field */
35 prec
; /* Number of characters of precision */
36 char tformat
[100], /* Temporary format string for sprintf() */
37 *tptr
, /* Pointer into temporary format */
38 temp
[1024]; /* Buffer for formatted numbers */
39 size_t templen
; /* Length of "temp" */
40 char *s
; /* Pointer to string */
41 int slen
; /* Length of string */
42 int bytes
; /* Total number of bytes needed */
46 * Loop through the format string, formatting as needed...
50 bufend
= buffer
+ bufsize
- 1;
62 if (bufptr
&& bufptr
< bufend
) *bufptr
++ = *format
;
67 else if (strchr(" -+#\'", *format
))
78 * Get width from argument...
82 width
= va_arg(ap
, int);
84 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", width
);
91 while (isdigit(*format
& 255))
93 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
96 width
= width
* 10 + *format
++ - '0';
102 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
110 * Get precision from argument...
114 prec
= va_arg(ap
, int);
116 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", prec
);
117 tptr
+= strlen(tptr
);
123 while (isdigit(*format
& 255))
125 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
128 prec
= prec
* 10 + *format
++ - '0';
135 if (*format
== 'l' && format
[1] == 'l')
139 if (tptr
< (tformat
+ sizeof(tformat
) - 2))
147 else if (*format
== 'h' || *format
== 'l' || *format
== 'L')
149 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
158 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
166 case 'E' : /* Floating point formats */
171 if ((width
+ 2) > sizeof(temp
))
174 sprintf(temp
, tformat
, va_arg(ap
, double));
175 templen
= strlen(temp
);
177 bytes
+= (int)templen
;
181 if ((bufptr
+ templen
) > bufend
)
183 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
188 memcpy(bufptr
, temp
, templen
+ 1);
194 case 'B' : /* Integer formats */
202 if ((width
+ 2) > sizeof(temp
))
205 sprintf(temp
, tformat
, va_arg(ap
, int));
206 templen
= strlen(temp
);
208 bytes
+= (int)templen
;
212 if ((bufptr
+ templen
) > bufend
)
214 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
219 memcpy(bufptr
, temp
, templen
+ 1);
225 case 'p' : /* Pointer value */
226 if ((width
+ 2) > sizeof(temp
))
229 sprintf(temp
, tformat
, va_arg(ap
, void *));
230 templen
= strlen(temp
);
232 bytes
+= (int)templen
;
236 if ((bufptr
+ templen
) > bufend
)
238 strlcpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
243 memcpy(bufptr
, temp
, templen
+ 1);
249 case 'c' : /* Character or character array */
255 *bufptr
++ = va_arg(ap
, int);
258 if ((bufptr
+ width
) > bufend
)
259 width
= (int)(bufend
- bufptr
);
261 memcpy(bufptr
, va_arg(ap
, char *), (size_t)width
);
267 case 's' : /* String */
268 if ((s
= va_arg(ap
, char *)) == NULL
)
271 slen
= (int)strlen(s
);
272 if (slen
> width
&& prec
!= width
)
279 if ((bufptr
+ width
) > bufend
)
280 width
= (int)(bufend
- bufptr
);
287 memcpy(bufptr
, s
, (size_t)slen
);
288 memset(bufptr
+ slen
, ' ', (size_t)(width
- slen
));
292 memset(bufptr
, ' ', (size_t)(width
- slen
));
293 memcpy(bufptr
+ width
- slen
, s
, (size_t)slen
);
300 case 'n' : /* Output number of chars so far */
301 *(va_arg(ap
, int *)) = bytes
;
309 if (bufptr
&& bufptr
< bufend
)
317 * Nul-terminate the string and return the number of characters needed.
324 #endif /* !HAVE_VSNPRINT */
327 #ifndef HAVE_SNPRINTF
329 * '_cups_snprintf()' - Format a string into a fixed size buffer.
332 int /* O - Number of bytes formatted */
333 _cups_snprintf(char *buffer
, /* O - Output buffer */
334 size_t bufsize
, /* O - Size of output buffer */
335 const char *format
, /* I - printf-style format string */
336 ...) /* I - Additional arguments as needed */
338 int bytes
; /* Number of bytes formatted */
339 va_list ap
; /* Pointer to additional arguments */
342 va_start(ap
, format
);
343 bytes
= vsnprintf(buffer
, bufsize
, format
, ap
);
348 #endif /* !HAVE_SNPRINTF */