]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/snprintf.c
e185fe38e4c2208be62254017468a3f1d2dc1d6d
2 * "$Id: snprintf.c,v 1.6 2002/03/01 19:53:30 mike Exp $"
4 * snprintf functions for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2002 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 * vsnprintf() - Format a string into a fixed size buffer.
29 * snprintf() - Format a string into a fixed size buffer.
33 * Include necessary headers...
41 #ifndef HAVE_VSNPRINTF
43 * 'vsnprintf()' - Format a string into a fixed size buffer.
46 int /* O - Number of bytes formatted */
47 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 int *chars
; /* Pointer to integer for %p */
63 char *s
; /* Pointer to string */
64 int slen
; /* Length of string */
68 * Loop through the format string, formatting as needed...
72 bufend
= buffer
+ bufsize
- 1;
74 while (*format
&& bufptr
< bufend
)
83 *bufptr
++ = *format
++;
86 else if (strchr(" -+#\'", *format
))
92 while (isdigit(*format
))
93 width
= width
* 10 + *format
++ - '0';
100 while (isdigit(*format
))
101 prec
= prec
* 10 + *format
++ - '0';
106 if (*format
== 'l' && format
[1] == 'l')
111 else if (*format
== 'h' || *format
== 'l' || *format
== 'L')
121 case 'E' : /* Floating point formats */
126 if ((format
- bufformat
+ 1) > sizeof(tformat
) ||
127 (width
+ 2) > sizeof(temp
))
130 strncpy(tformat
, bufformat
, format
- bufformat
);
131 tformat
[format
- bufformat
] = '\0';
133 sprintf(temp
, tformat
, va_arg(ap
, double));
135 if ((bufptr
+ strlen(temp
)) > bufend
)
137 strncpy(bufptr
, temp
, bufend
- bufptr
);
143 strcpy(bufptr
, temp
);
144 bufptr
+= strlen(temp
);
148 case 'B' : /* Integer formats */
156 if ((format
- bufformat
+ 1) > sizeof(tformat
) ||
157 (width
+ 2) > sizeof(temp
))
160 strncpy(tformat
, bufformat
, format
- bufformat
);
161 tformat
[format
- bufformat
] = '\0';
163 sprintf(temp
, tformat
, va_arg(ap
, int));
165 if ((bufptr
+ strlen(temp
)) > bufend
)
167 strncpy(bufptr
, temp
, bufend
- bufptr
);
173 strcpy(bufptr
, temp
);
174 bufptr
+= strlen(temp
);
178 case 'p' : /* Pointer value */
179 if ((chars
= va_arg(ap
, int *)) != NULL
)
180 *chars
= bufptr
- buffer
;
183 case 'c' : /* Character or character array */
185 *bufptr
++ = va_arg(ap
, int);
188 if ((bufptr
+ width
) > bufend
)
189 width
= bufend
- bufptr
;
191 memcpy(bufptr
, va_arg(ap
, char *), width
);
196 case 's' : /* String */
197 if ((s
= va_arg(ap
, char *)) == NULL
)
201 if (slen
> width
&& prec
!= width
)
204 if ((bufptr
+ width
) > bufend
)
205 width
= bufend
- bufptr
;
212 strncpy(bufptr
, s
, slen
);
213 memset(bufptr
+ slen
, ' ', width
- slen
);
217 memset(bufptr
, ' ', width
- slen
);
218 strncpy(bufptr
+ width
- slen
, s
, slen
);
224 case 'n' : /* Output number of chars so far */
225 if ((format
- bufformat
+ 1) > sizeof(tformat
) ||
226 (width
+ 2) > sizeof(temp
))
229 strncpy(tformat
, bufformat
, format
- bufformat
);
230 tformat
[format
- bufformat
] = '\0';
232 sprintf(temp
, tformat
, va_arg(ap
, int));
234 if ((bufptr
+ strlen(temp
)) > bufend
)
236 strncpy(bufptr
, temp
, bufend
- bufptr
);
242 strcpy(bufptr
, temp
);
243 bufptr
+= strlen(temp
);
249 *bufptr
++ = *format
++;
253 * Nul-terminate the string and return the number of characters in it.
257 return (bufptr
- buffer
);
259 #endif /* !HAVE_VSNPRINT */
262 #ifndef HAVE_SNPRINTF
264 * 'snprintf()' - Format a string into a fixed size buffer.
267 int /* O - Number of bytes formatted */
268 snprintf(char *buffer
, /* O - Output buffer */
269 size_t bufsize
, /* O - Size of output buffer */
270 const char *format
, /* I - printf-style format string */
271 ...) /* I - Additional arguments as needed */
273 int bytes
; /* Number of bytes formatted */
274 va_list ap
; /* Pointer to additional arguments */
277 va_start(ap
, format
);
278 bytes
= vsnprintf(buffer
, bufsize
, format
, ap
);
283 #endif /* !HAVE_SNPRINTF */
287 * End of "$Id: snprintf.c,v 1.6 2002/03/01 19:53:30 mike Exp $".