annotate lib/sprintf.c @ 8373:b975f31a2a92

New module 'sprintf-posix'.
author Bruno Haible <bruno@clisp.org>
date Wed, 07 Mar 2007 03:47:50 +0000
parents
children 8c96722f8bd0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8373
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Formatted output to strings.
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 2004, 2006-2007 Free Software Foundation, Inc.
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software; you can redistribute it and/or modify
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 2, or (at your option)
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 any later version.
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License along
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 with this program; if not, write to the Free Software Foundation,
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 #ifdef HAVE_CONFIG_H
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 # include <config.h>
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 #endif
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 /* Specification. */
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <stdio.h>
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include <errno.h>
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <limits.h>
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #include <stdarg.h>
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #include <stdlib.h>
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #include "vasnprintf.h"
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 #ifndef EOVERFLOW
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 # define EOVERFLOW E2BIG
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 #endif
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 #ifndef SIZE_MAX
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 # define SIZE_MAX ((size_t) -1)
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 #endif
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 /* Print formatted output to string STR.
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 Return string length of formatted string. On error, return a negative
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 value. */
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 int
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 sprintf (char *str, const char *format, ...)
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 {
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 char *output;
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 size_t len;
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 size_t lenbuf = SIZE_MAX;
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 va_list args;
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 va_start (args, format);
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 output = vasnprintf (str, &lenbuf, format, args);
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 len = lenbuf;
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 va_end (args);
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 if (!output)
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 return -1;
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 if (output != str)
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 {
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 /* len is near SIZE_MAX. */
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 free (output);
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 errno = EOVERFLOW;
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 return -1;
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 }
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 if (len > INT_MAX)
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 {
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 errno = EOVERFLOW;
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 return -1;
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 }
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 return len;
b975f31a2a92 New module 'sprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 }