]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/snprintf.c
2 * "$Id: snprintf.c,v 1.4.2.8 2004/02/25 16:58:32 mike Exp $"
4 * snprintf functions for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2003 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-3111 USA
20 * Voice: (301) 373-9603
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 const char *bufformat
; /* Start of format */
58 int width
, /* Width of field */
59 prec
; /* Number of characters of precision */
60 char tformat
[100], /* Temporary format string for sprintf() */
61 temp
[1024]; /* Buffer for formatted numbers */
62 char *s
; /* Pointer to string */
63 int slen
; /* Length of string */
67 * Loop through the format string, formatting as needed...
71 bufend
= buffer
+ bufsize
- 1;
73 while (*format
&& bufptr
< bufend
)
82 *bufptr
++ = *format
++;
85 else if (strchr(" -+#\'", *format
))
91 while (isdigit(*format
& 255))
92 width
= width
* 10 + *format
++ - '0';
99 while (isdigit(*format
& 255))
100 prec
= prec
* 10 + *format
++ - '0';
105 if (*format
== 'l' && format
[1] == 'l')
110 else if (*format
== 'h' || *format
== 'l' || *format
== 'L')
120 case 'E' : /* Floating point formats */
125 if ((format
- bufformat
+ 1) > sizeof(tformat
) ||
126 (width
+ 2) > sizeof(temp
))
129 strncpy(tformat
, bufformat
, format
- bufformat
);
130 tformat
[format
- bufformat
] = '\0';
132 sprintf(temp
, tformat
, va_arg(ap
, double));
134 if ((bufptr
+ strlen(temp
)) > bufend
)
136 strncpy(bufptr
, temp
, bufend
- bufptr
);
142 strcpy(bufptr
, temp
);
143 bufptr
+= strlen(temp
);
147 case 'B' : /* Integer formats */
155 if ((format
- bufformat
+ 1) > sizeof(tformat
) ||
156 (width
+ 2) > sizeof(temp
))
159 strncpy(tformat
, bufformat
, format
- bufformat
);
160 tformat
[format
- bufformat
] = '\0';
162 sprintf(temp
, tformat
, va_arg(ap
, int));
164 if ((bufptr
+ strlen(temp
)) > bufend
)
166 strncpy(bufptr
, temp
, bufend
- bufptr
);
172 strcpy(bufptr
, temp
);
173 bufptr
+= strlen(temp
);
177 case 'p' : /* Pointer value */
178 if ((format
- bufformat
+ 1) > sizeof(tformat
) ||
179 (width
+ 2) > sizeof(temp
))
182 strncpy(tformat
, bufformat
, format
- bufformat
);
183 tformat
[format
- bufformat
] = '\0';
185 sprintf(temp
, tformat
, va_arg(ap
, void *));
187 if ((bufptr
+ strlen(temp
)) > bufend
)
189 strncpy(bufptr
, temp
, bufend
- bufptr
);
195 strcpy(bufptr
, temp
);
196 bufptr
+= strlen(temp
);
200 case 'c' : /* Character or character array */
202 *bufptr
++ = va_arg(ap
, int);
205 if ((bufptr
+ width
) > bufend
)
206 width
= bufend
- bufptr
;
208 memcpy(bufptr
, va_arg(ap
, char *), width
);
213 case 's' : /* String */
214 if ((s
= va_arg(ap
, char *)) == NULL
)
218 if (slen
> width
&& prec
!= width
)
221 if ((bufptr
+ width
) > bufend
)
222 width
= bufend
- bufptr
;
229 strncpy(bufptr
, s
, slen
);
230 memset(bufptr
+ slen
, ' ', width
- slen
);
234 memset(bufptr
, ' ', width
- slen
);
235 strncpy(bufptr
+ width
- slen
, s
, slen
);
241 case 'n' : /* Output number of chars so far */
242 if ((format
- bufformat
+ 1) > sizeof(tformat
) ||
243 (width
+ 2) > sizeof(temp
))
246 strncpy(tformat
, bufformat
, format
- bufformat
);
247 tformat
[format
- bufformat
] = '\0';
249 sprintf(temp
, tformat
, va_arg(ap
, int));
251 if ((bufptr
+ strlen(temp
)) > bufend
)
253 strncpy(bufptr
, temp
, bufend
- bufptr
);
259 strcpy(bufptr
, temp
);
260 bufptr
+= strlen(temp
);
266 *bufptr
++ = *format
++;
270 * Nul-terminate the string and return the number of characters in it.
274 return (bufptr
- buffer
);
276 #endif /* !HAVE_VSNPRINT */
279 #ifndef HAVE_SNPRINTF
281 * 'cups_snprintf()' - Format a string into a fixed size buffer.
284 int /* O - Number of bytes formatted */
285 cups_snprintf(char *buffer
, /* O - Output buffer */
286 size_t bufsize
, /* O - Size of output buffer */
287 const char *format
, /* I - printf-style format string */
288 ...) /* I - Additional arguments as needed */
290 int bytes
; /* Number of bytes formatted */
291 va_list ap
; /* Pointer to additional arguments */
294 va_start(ap
, format
);
295 bytes
= vsnprintf(buffer
, bufsize
, format
, ap
);
300 #endif /* !HAVE_SNPRINTF */
304 * End of "$Id: snprintf.c,v 1.4.2.8 2004/02/25 16:58:32 mike Exp $".