annotate lib/snprintf.c @ 7419:0c560af115cb

.
author Karl Berry <karl@freefriends.org>
date Sat, 07 Oct 2006 07:42:41 +0000
parents 8a1a9361108c
children c31f5d04554e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5282
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
1 /* Formatted output to strings.
7109
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
2 Copyright (C) 2004, 2006 Free Software Foundation, Inc.
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
3 Written by Simon Josefsson and Paul Eggert.
5282
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
4
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
5 This program is free software; you can redistribute it and/or modify
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
7 the Free Software Foundation; either version 2, or (at your option)
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
8 any later version.
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
9
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
13 GNU General Public License for more details.
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
14
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License along
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
16 with this program; if not, write to the Free Software Foundation,
5848
a48fb0e98c8c *** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents: 5333
diff changeset
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
5282
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
18
7302
8a1a9361108c * _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents: 7122
diff changeset
19 #include <config.h>
5282
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
20
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
21 #include "snprintf.h"
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
22
7109
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
23 #include <errno.h>
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
24 #include <limits.h>
5290
a32d2a4a925f Fixes, mostly from Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 5288
diff changeset
25 #include <stdarg.h>
a32d2a4a925f Fixes, mostly from Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 5288
diff changeset
26 #include <stdlib.h>
5288
553713ba9dac Ensure memcpy is declared.
Bruno Haible <bruno@clisp.org>
parents: 5282
diff changeset
27 #include <string.h>
553713ba9dac Ensure memcpy is declared.
Bruno Haible <bruno@clisp.org>
parents: 5282
diff changeset
28
5282
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
29 #include "vasnprintf.h"
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
30
7109
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
31 /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
32 #ifndef EOVERFLOW
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
33 # define EOVERFLOW E2BIG
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
34 #endif
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
35
5282
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
36 /* Print formatted output to string STR. Similar to sprintf, but
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
37 additional length SIZE limit how much is written into STR. Returns
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
38 string length of formatted string (which may be larger than SIZE).
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
39 STR may be NULL, in which case nothing will be written. On error,
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
40 return a negative value. */
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
41 int
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
42 snprintf (char *str, size_t size, const char *format, ...)
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
43 {
5290
a32d2a4a925f Fixes, mostly from Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 5288
diff changeset
44 char *output;
5282
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
45 size_t len;
7121
2b481e5376f7 * snprintf.c (snprintf): memcpy LEN bytes, not SIZE - 1, when
Paul Eggert <eggert@cs.ucla.edu>
parents: 7109
diff changeset
46 size_t lenbuf = size;
5290
a32d2a4a925f Fixes, mostly from Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 5288
diff changeset
47 va_list args;
5282
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
48
5290
a32d2a4a925f Fixes, mostly from Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 5288
diff changeset
49 va_start (args, format);
7122
334ac0e6023c Fix typo in previous checkin: len -> lenbuf.
Paul Eggert <eggert@cs.ucla.edu>
parents: 7121
diff changeset
50 output = vasnprintf (str, &lenbuf, format, args);
7121
2b481e5376f7 * snprintf.c (snprintf): memcpy LEN bytes, not SIZE - 1, when
Paul Eggert <eggert@cs.ucla.edu>
parents: 7109
diff changeset
51 len = lenbuf;
5290
a32d2a4a925f Fixes, mostly from Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 5288
diff changeset
52 va_end (args);
a32d2a4a925f Fixes, mostly from Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 5288
diff changeset
53
a32d2a4a925f Fixes, mostly from Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 5288
diff changeset
54 if (!output)
5282
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
55 return -1;
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
56
7109
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
57 if (output != str)
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
58 {
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
59 if (size)
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
60 {
7121
2b481e5376f7 * snprintf.c (snprintf): memcpy LEN bytes, not SIZE - 1, when
Paul Eggert <eggert@cs.ucla.edu>
parents: 7109
diff changeset
61 size_t pruned_len = (len < size ? len : size - 1);
2b481e5376f7 * snprintf.c (snprintf): memcpy LEN bytes, not SIZE - 1, when
Paul Eggert <eggert@cs.ucla.edu>
parents: 7109
diff changeset
62 memcpy (str, output, pruned_len);
2b481e5376f7 * snprintf.c (snprintf): memcpy LEN bytes, not SIZE - 1, when
Paul Eggert <eggert@cs.ucla.edu>
parents: 7109
diff changeset
63 str[pruned_len] = '\0';
7109
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
64 }
5282
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
65
7109
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
66 free (output);
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
67 }
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
68
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
69 if (INT_MAX < len)
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
70 {
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
71 errno = EOVERFLOW;
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
72 return -1;
8f07495555ba * modules/snprintf (Depends-on): Remove minmax.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5848
diff changeset
73 }
5282
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
74
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
75 return len;
149d795beeb2 New snprintf module from Simon Josefsson.
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
76 }