Mercurial > hg > octave-lojdl > gnulib-hg
annotate lib/xvasprintf.c @ 17426:90f3d53e01f5
sig2str: port to C++
* lib/sig2str.h (sig2str, str2sig): Declare as extern "C".
Reported by Daniel J Sebald in
<http://lists.gnu.org/archive/html/bug-gnulib/2013-06/msg00000.html>.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Sun, 02 Jun 2013 11:52:41 -0700 |
parents | e542fd46ad6f |
children |
rev | line source |
---|---|
5218 | 1 /* vasprintf and asprintf with out-of-memory checking. |
17249
e542fd46ad6f
maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents:
17191
diff
changeset
|
2 Copyright (C) 1999, 2002-2004, 2006-2013 Free Software Foundation, Inc. |
5218 | 3 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8570
diff
changeset
|
4 This program is free software: you can redistribute it and/or modify |
5218 | 5 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8570
diff
changeset
|
6 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8570
diff
changeset
|
7 (at your option) any later version. |
5218 | 8 |
9 This program is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU General Public License for more details. | |
13 | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8570
diff
changeset
|
14 You should have received a copy of the GNU General Public License |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8570
diff
changeset
|
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
5218 | 16 |
7510
6c0cb059c9ea
* lib/xvasprintf.c (includes): Assume config.h.
Eric Blake <ebb9@byu.net>
parents:
6779
diff
changeset
|
17 #include <config.h> |
5218 | 18 |
19 /* Specification. */ | |
20 #include "xvasprintf.h" | |
21 | |
22 #include <errno.h> | |
6779
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
23 #include <limits.h> |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
24 #include <string.h> |
8570
4175c39ba7cc
Move vasprintf prototypes to stdio.
Simon Josefsson <simon@josefsson.org>
parents:
7603
diff
changeset
|
25 #include <stdio.h> |
5218 | 26 |
5272
533332af517b
Fixing a stupid typo in xvasprintf.c... from Oskar Liljeblad.
Bruno Haible <bruno@clisp.org>
parents:
5218
diff
changeset
|
27 #include "xalloc.h" |
5218 | 28 |
6779
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
29 /* Checked size_t computations. */ |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
30 #include "xsize.h" |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
31 |
17191
d27d22e71282
xvasprintf: no 'static inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
32 static char * |
6779
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
33 xstrcat (size_t argcount, va_list args) |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
34 { |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
35 char *result; |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
36 va_list ap; |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
37 size_t totalsize; |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
38 size_t i; |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
39 char *p; |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
40 |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
41 /* Determine the total size. */ |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
42 totalsize = 0; |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
43 va_copy (ap, args); |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
44 for (i = argcount; i > 0; i--) |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
45 { |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
46 const char *next = va_arg (ap, const char *); |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
47 totalsize = xsum (totalsize, strlen (next)); |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
48 } |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
49 va_end (ap); |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
50 |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
51 /* Test for overflow in the summing pass above or in (totalsize + 1) below. |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
52 Also, don't return a string longer than INT_MAX, for consistency with |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
53 vasprintf(). */ |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
54 if (totalsize == SIZE_MAX || totalsize > INT_MAX) |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
55 { |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
56 errno = EOVERFLOW; |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
57 return NULL; |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
58 } |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
59 |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
60 /* Allocate and fill the result string. */ |
7603
23f14c284219
Simplify xmalloc expressions. Add overflow check in xmalloc arguments.
Bruno Haible <bruno@clisp.org>
parents:
7510
diff
changeset
|
61 result = XNMALLOC (totalsize + 1, char); |
6779
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
62 p = result; |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
63 for (i = argcount; i > 0; i--) |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
64 { |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
65 const char *next = va_arg (args, const char *); |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
66 size_t len = strlen (next); |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
67 memcpy (p, next, len); |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
68 p += len; |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
69 } |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
70 *p = '\0'; |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
71 |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
72 return result; |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
73 } |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
74 |
5218 | 75 char * |
76 xvasprintf (const char *format, va_list args) | |
77 { | |
78 char *result; | |
79 | |
6779
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
80 /* Recognize the special case format = "%s...%s". It is a frequently used |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
81 idiom for string concatenation and needs to be fast. We don't want to |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
82 have a separate function xstrcat() for this purpose. */ |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
83 { |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
84 size_t argcount = 0; |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
85 const char *f; |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
86 |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
87 for (f = format;;) |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
88 { |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9831
diff
changeset
|
89 if (*f == '\0') |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9831
diff
changeset
|
90 /* Recognized the special case of string concatenation. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9831
diff
changeset
|
91 return xstrcat (argcount, args); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9831
diff
changeset
|
92 if (*f != '%') |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9831
diff
changeset
|
93 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9831
diff
changeset
|
94 f++; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9831
diff
changeset
|
95 if (*f != 's') |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9831
diff
changeset
|
96 break; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9831
diff
changeset
|
97 f++; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9831
diff
changeset
|
98 argcount++; |
6779
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
99 } |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
100 } |
1807d27bf1ec
Recognize the special case of a string concatenation in xvasprintf.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
101 |
5218 | 102 if (vasprintf (&result, format, args) < 0) |
103 { | |
104 if (errno == ENOMEM) | |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9831
diff
changeset
|
105 xalloc_die (); |
5218 | 106 return NULL; |
107 } | |
108 | |
109 return result; | |
110 } |