]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/snprintf.c
2 * "$Id: snprintf.c 4683 2005-09-21 22:17:44Z mike $"
4 * snprintf functions for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2005 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
))
99 // Get width from argument...
101 width
= va_arg(ap
, int);
103 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", width
);
104 tptr
+= strlen(tptr
);
110 while (isdigit(*format
& 255))
112 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
115 width
= width
* 10 + *format
++ - '0';
121 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
128 // Get precision from argument...
130 prec
= va_arg(ap
, int);
132 snprintf(tptr
, sizeof(tformat
) - (tptr
- tformat
), "%d", prec
);
133 tptr
+= strlen(tptr
);
139 while (isdigit(*format
& 255))
141 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
144 prec
= prec
* 10 + *format
++ - '0';
151 if (*format
== 'l' && format
[1] == 'l')
155 if (tptr
< (tformat
+ sizeof(tformat
) - 2))
163 else if (*format
== 'h' || *format
== 'l' || *format
== 'L')
165 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
174 if (tptr
< (tformat
+ sizeof(tformat
) - 1))
182 case 'E' : /* Floating point formats */
187 if ((width
+ 2) > sizeof(temp
))
190 sprintf(temp
, tformat
, va_arg(ap
, double));
192 bytes
+= strlen(temp
);
196 if ((bufptr
+ strlen(temp
)) > bufend
)
198 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
203 strcpy(bufptr
, temp
);
204 bufptr
+= strlen(temp
);
209 case 'B' : /* Integer formats */
217 if ((width
+ 2) > sizeof(temp
))
220 sprintf(temp
, tformat
, va_arg(ap
, int));
222 bytes
+= strlen(temp
);
226 if ((bufptr
+ strlen(temp
)) > bufend
)
228 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
233 strcpy(bufptr
, temp
);
234 bufptr
+= strlen(temp
);
239 case 'p' : /* Pointer value */
240 if ((width
+ 2) > sizeof(temp
))
243 sprintf(temp
, tformat
, va_arg(ap
, void *));
245 bytes
+= strlen(temp
);
249 if ((bufptr
+ strlen(temp
)) > bufend
)
251 strncpy(bufptr
, temp
, (size_t)(bufend
- bufptr
));
256 strcpy(bufptr
, temp
);
257 bufptr
+= strlen(temp
);
262 case 'c' : /* Character or character array */
268 *bufptr
++ = va_arg(ap
, int);
271 if ((bufptr
+ width
) > bufend
)
272 width
= bufend
- bufptr
;
274 memcpy(bufptr
, va_arg(ap
, char *), (size_t)width
);
280 case 's' : /* String */
281 if ((s
= va_arg(ap
, char *)) == NULL
)
285 if (slen
> width
&& prec
!= width
)
292 if ((bufptr
+ width
) > bufend
)
293 width
= bufend
- bufptr
;
300 strncpy(bufptr
, s
, (size_t)slen
);
301 memset(bufptr
+ slen
, ' ', (size_t)(width
- slen
));
305 memset(bufptr
, ' ', (size_t)(width
- slen
));
306 strncpy(bufptr
+ width
- slen
, s
, (size_t)slen
);
313 case 'n' : /* Output number of chars so far */
314 *(va_arg(ap
, int *)) = bytes
;
322 if (bufptr
&& bufptr
< bufend
)
330 * Nul-terminate the string and return the number of characters needed.
337 #endif /* !HAVE_VSNPRINT */
340 #ifndef HAVE_SNPRINTF
342 * '_cups_snprintf()' - Format a string into a fixed size buffer.
345 int /* O - Number of bytes formatted */
346 _cups_snprintf(char *buffer
, /* O - Output buffer */
347 size_t bufsize
, /* O - Size of output buffer */
348 const char *format
, /* I - printf-style format string */
349 ...) /* I - Additional arguments as needed */
351 int bytes
; /* Number of bytes formatted */
352 va_list ap
; /* Pointer to additional arguments */
355 va_start(ap
, format
);
356 bytes
= vsnprintf(buffer
, bufsize
, format
, ap
);
361 #endif /* !HAVE_SNPRINTF */
365 * End of "$Id: snprintf.c 4683 2005-09-21 22:17:44Z mike $".