Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/vasnprintf.c @ 8051:9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Tue, 30 Jan 2007 01:07:22 +0000 |
parents | 99b9b91e548c |
children | bbea39ab5a58 |
rev | line source |
---|---|
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
1 /* vsprintf with automatic memory allocation. |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
2 Copyright (C) 1999, 2002-2007 Free Software Foundation, Inc. |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
3 |
4440
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4428
diff
changeset
|
4 This program is free software; you can redistribute it and/or modify |
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4428
diff
changeset
|
5 it under the terms of the GNU General Public License as published by |
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4428
diff
changeset
|
6 the Free Software Foundation; either version 2, or (at your option) |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
7 any later version. |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
8 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
9 This program is distributed in the hope that it will be useful, |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
4440
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4428
diff
changeset
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4428
diff
changeset
|
12 GNU General Public License for more details. |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
13 |
4440
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4428
diff
changeset
|
14 You should have received a copy of the GNU General Public License along |
e58a1c05a6ba
Update gettext source files from gettext automatically, using srclist-update.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4428
diff
changeset
|
15 with this program; if not, write to the Free Software Foundation, |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
5670
diff
changeset
|
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
17 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
18 /* Tell glibc's <stdio.h> to provide a prototype for snprintf(). |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
19 This must come before <config.h> because <config.h> may include |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
20 <features.h>, and once <features.h> has been included, it's too late. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
21 #ifndef _GNU_SOURCE |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
22 # define _GNU_SOURCE 1 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
23 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
24 |
7304
1c4ed7637c24
Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
7218
diff
changeset
|
25 #include <config.h> |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
26 #ifndef IN_LIBINTL |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
27 # include <alloca.h> |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
28 #endif |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
29 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
30 /* Specification. */ |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
31 #if WIDE_CHAR_VERSION |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
32 # include "vasnwprintf.h" |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
33 #else |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
34 # include "vasnprintf.h" |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
35 #endif |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
36 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
37 #include <stdio.h> /* snprintf(), sprintf() */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
38 #include <stdlib.h> /* abort(), malloc(), realloc(), free() */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
39 #include <string.h> /* memcpy(), strlen() */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
40 #include <errno.h> /* errno */ |
7218
0fd12ba5cfc6
Do the INT_MAX check only where size_t is converted to 'int'.
Bruno Haible <bruno@clisp.org>
parents:
6583
diff
changeset
|
41 #include <limits.h> /* CHAR_BIT */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
42 #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */ |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
43 #if WIDE_CHAR_VERSION |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
44 # include "wprintf-parse.h" |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
45 #else |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
46 # include "printf-parse.h" |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
47 #endif |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
48 |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
49 /* Checked size_t computations. */ |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
50 #include "xsize.h" |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
51 |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
52 #if HAVE_WCHAR_T |
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
53 # if HAVE_WCSLEN |
4523
6cc4874a100b
Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents:
4444
diff
changeset
|
54 # define local_wcslen wcslen |
6cc4874a100b
Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents:
4444
diff
changeset
|
55 # else |
6cc4874a100b
Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents:
4444
diff
changeset
|
56 /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
57 a dependency towards this library, here is a local substitute. |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
58 Define this substitute only once, even if this file is included |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
59 twice in the same compilation unit. */ |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
60 # ifndef local_wcslen_defined |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
61 # define local_wcslen_defined 1 |
4523
6cc4874a100b
Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents:
4444
diff
changeset
|
62 static size_t |
6cc4874a100b
Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents:
4444
diff
changeset
|
63 local_wcslen (const wchar_t *s) |
6cc4874a100b
Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents:
4444
diff
changeset
|
64 { |
6cc4874a100b
Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents:
4444
diff
changeset
|
65 const wchar_t *ptr; |
6cc4874a100b
Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents:
4444
diff
changeset
|
66 |
6cc4874a100b
Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents:
4444
diff
changeset
|
67 for (ptr = s; *ptr != (wchar_t) 0; ptr++) |
6cc4874a100b
Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents:
4444
diff
changeset
|
68 ; |
6cc4874a100b
Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents:
4444
diff
changeset
|
69 return ptr - s; |
6cc4874a100b
Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents:
4444
diff
changeset
|
70 } |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
71 # endif |
4523
6cc4874a100b
Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents:
4444
diff
changeset
|
72 # endif |
6cc4874a100b
Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents:
4444
diff
changeset
|
73 #endif |
6cc4874a100b
Work around the lack of wcslen() on Solaris 2.5.1.
Bruno Haible <bruno@clisp.org>
parents:
4444
diff
changeset
|
74 |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
75 #if WIDE_CHAR_VERSION |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
76 # define VASNPRINTF vasnwprintf |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
77 # define CHAR_T wchar_t |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
78 # define DIRECTIVE wchar_t_directive |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
79 # define DIRECTIVES wchar_t_directives |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
80 # define PRINTF_PARSE wprintf_parse |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
81 # define USE_SNPRINTF 1 |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
82 # if HAVE_DECL__SNWPRINTF |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
83 /* On Windows, the function swprintf() has a different signature than |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
84 on Unix; we use the _snwprintf() function instead. */ |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
85 # define SNPRINTF _snwprintf |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
86 # else |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
87 /* Unix. */ |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
88 # define SNPRINTF swprintf |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
89 # endif |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
90 #else |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
91 # define VASNPRINTF vasnprintf |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
92 # define CHAR_T char |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
93 # define DIRECTIVE char_directive |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
94 # define DIRECTIVES char_directives |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
95 # define PRINTF_PARSE printf_parse |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
96 # define USE_SNPRINTF (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
97 # if HAVE_DECL__SNPRINTF |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
98 /* Windows. */ |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
99 # define SNPRINTF _snprintf |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
100 # else |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
101 /* Unix. */ |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
102 # define SNPRINTF snprintf |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
103 # endif |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
104 #endif |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
105 |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
106 CHAR_T * |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
107 VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args) |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
108 { |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
109 DIRECTIVES d; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
110 arguments a; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
111 |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
112 if (PRINTF_PARSE (format, &d, &a) < 0) |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
113 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
114 errno = EINVAL; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
115 return NULL; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
116 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
117 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
118 #define CLEANUP() \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
119 free (d.dir); \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
120 if (a.arg) \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
121 free (a.arg); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
122 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
123 if (printf_fetchargs (args, &a) < 0) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
124 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
125 CLEANUP (); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
126 errno = EINVAL; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
127 return NULL; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
128 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
129 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
130 { |
4879
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
131 size_t buf_neededlength; |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
132 CHAR_T *buf; |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
133 CHAR_T *buf_malloced; |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
134 const CHAR_T *cp; |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
135 size_t i; |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
136 DIRECTIVE *dp; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
137 /* Output string accumulator. */ |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
138 CHAR_T *result; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
139 size_t allocated; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
140 size_t length; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
141 |
4879
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
142 /* Allocate a small buffer that will hold a directive passed to |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
143 sprintf or snprintf. */ |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
144 buf_neededlength = |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
145 xsum4 (7, d.max_width_length, d.max_precision_length, 6); |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
146 #if HAVE_ALLOCA |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
147 if (buf_neededlength < 4000 / sizeof (CHAR_T)) |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
148 { |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
149 buf = (CHAR_T *) alloca (buf_neededlength * sizeof (CHAR_T)); |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
150 buf_malloced = NULL; |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
151 } |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
152 else |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
153 #endif |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
154 { |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
155 size_t buf_memsize = xtimes (buf_neededlength, sizeof (CHAR_T)); |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
156 if (size_overflow_p (buf_memsize)) |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
157 goto out_of_memory_1; |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
158 buf = (CHAR_T *) malloc (buf_memsize); |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
159 if (buf == NULL) |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
160 goto out_of_memory_1; |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
161 buf_malloced = buf; |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
162 } |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
163 |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
164 if (resultbuf != NULL) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
165 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
166 result = resultbuf; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
167 allocated = *lengthp; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
168 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
169 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
170 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
171 result = NULL; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
172 allocated = 0; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
173 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
174 length = 0; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
175 /* Invariants: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
176 result is either == resultbuf or == NULL or malloc-allocated. |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
177 If length > 0, then result != NULL. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
178 |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
179 /* Ensures that allocated >= needed. Aborts through a jump to |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
180 out_of_memory if needed is SIZE_MAX or otherwise too big. */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
181 #define ENSURE_ALLOCATION(needed) \ |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
182 if ((needed) > allocated) \ |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
183 { \ |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
184 size_t memory_size; \ |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
185 CHAR_T *memory; \ |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
186 \ |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
187 allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \ |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
188 if ((needed) > allocated) \ |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
189 allocated = (needed); \ |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
190 memory_size = xtimes (allocated, sizeof (CHAR_T)); \ |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
191 if (size_overflow_p (memory_size)) \ |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
192 goto out_of_memory; \ |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
193 if (result == resultbuf || result == NULL) \ |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
194 memory = (CHAR_T *) malloc (memory_size); \ |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
195 else \ |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
196 memory = (CHAR_T *) realloc (result, memory_size); \ |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
197 if (memory == NULL) \ |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
198 goto out_of_memory; \ |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
199 if (result == resultbuf && length > 0) \ |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
200 memcpy (memory, result, length * sizeof (CHAR_T)); \ |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
201 result = memory; \ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
202 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
203 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
204 for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
205 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
206 if (cp != dp->dir_start) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
207 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
208 size_t n = dp->dir_start - cp; |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
209 size_t augmented_length = xsum (length, n); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
210 |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
211 ENSURE_ALLOCATION (augmented_length); |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
212 memcpy (result + length, cp, n * sizeof (CHAR_T)); |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
213 length = augmented_length; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
214 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
215 if (i == d.count) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
216 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
217 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
218 /* Execute a single directive. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
219 if (dp->conversion == '%') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
220 { |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
221 size_t augmented_length; |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
222 |
4886
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4879
diff
changeset
|
223 if (!(dp->arg_index == ARG_NONE)) |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
224 abort (); |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
225 augmented_length = xsum (length, 1); |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
226 ENSURE_ALLOCATION (augmented_length); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
227 result[length] = '%'; |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
228 length = augmented_length; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
229 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
230 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
231 { |
4886
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4879
diff
changeset
|
232 if (!(dp->arg_index != ARG_NONE)) |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
233 abort (); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
234 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
235 if (dp->conversion == 'n') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
236 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
237 switch (a.arg[dp->arg_index].type) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
238 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
239 case TYPE_COUNT_SCHAR_POINTER: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
240 *a.arg[dp->arg_index].a.a_count_schar_pointer = length; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
241 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
242 case TYPE_COUNT_SHORT_POINTER: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
243 *a.arg[dp->arg_index].a.a_count_short_pointer = length; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
244 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
245 case TYPE_COUNT_INT_POINTER: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
246 *a.arg[dp->arg_index].a.a_count_int_pointer = length; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
247 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
248 case TYPE_COUNT_LONGINT_POINTER: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
249 *a.arg[dp->arg_index].a.a_count_longint_pointer = length; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
250 break; |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
251 #if HAVE_LONG_LONG_INT |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
252 case TYPE_COUNT_LONGLONGINT_POINTER: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
253 *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
254 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
255 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
256 default: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
257 abort (); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
258 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
259 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
260 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
261 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
262 arg_type type = a.arg[dp->arg_index].type; |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
263 CHAR_T *p; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
264 unsigned int prefix_count; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
265 int prefixes[2]; |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
266 #if !USE_SNPRINTF |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
267 size_t tmp_length; |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
268 CHAR_T tmpbuf[700]; |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
269 CHAR_T *tmp; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
270 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
271 /* Allocate a temporary buffer of sufficient size for calling |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
272 sprintf. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
273 { |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
274 size_t width; |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
275 size_t precision; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
276 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
277 width = 0; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
278 if (dp->width_start != dp->width_end) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
279 { |
4886
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4879
diff
changeset
|
280 if (dp->width_arg_index != ARG_NONE) |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
281 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
282 int arg; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
283 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
284 if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
285 abort (); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
286 arg = a.arg[dp->width_arg_index].a.a_int; |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
287 width = (arg < 0 ? (unsigned int) (-arg) : arg); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
288 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
289 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
290 { |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
291 const CHAR_T *digitp = dp->width_start; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
292 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
293 do |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
294 width = xsum (xtimes (width, 10), *digitp++ - '0'); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
295 while (digitp != dp->width_end); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
296 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
297 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
298 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
299 precision = 6; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
300 if (dp->precision_start != dp->precision_end) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
301 { |
4886
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4879
diff
changeset
|
302 if (dp->precision_arg_index != ARG_NONE) |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
303 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
304 int arg; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
305 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
306 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
307 abort (); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
308 arg = a.arg[dp->precision_arg_index].a.a_int; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
309 precision = (arg < 0 ? 0 : arg); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
310 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
311 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
312 { |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
313 const CHAR_T *digitp = dp->precision_start + 1; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
314 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
315 precision = 0; |
5066
a2cb70e482fc
Fix for format strings like "%2.f".
Bruno Haible <bruno@clisp.org>
parents:
4886
diff
changeset
|
316 while (digitp != dp->precision_end) |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
317 precision = xsum (xtimes (precision, 10), *digitp++ - '0'); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
318 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
319 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
320 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
321 switch (dp->conversion) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
322 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
323 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
324 case 'd': case 'i': case 'u': |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
325 # if HAVE_LONG_LONG_INT |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
326 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
327 tmp_length = |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
328 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
329 * 0.30103 /* binary -> decimal */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
330 ) |
6583
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
331 + 1; /* turn floor into ceil */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
332 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
333 # endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
334 if (type == TYPE_LONGINT || type == TYPE_ULONGINT) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
335 tmp_length = |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
336 (unsigned int) (sizeof (unsigned long) * CHAR_BIT |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
337 * 0.30103 /* binary -> decimal */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
338 ) |
6583
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
339 + 1; /* turn floor into ceil */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
340 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
341 tmp_length = |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
342 (unsigned int) (sizeof (unsigned int) * CHAR_BIT |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
343 * 0.30103 /* binary -> decimal */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
344 ) |
6583
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
345 + 1; /* turn floor into ceil */ |
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
346 if (tmp_length < precision) |
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
347 tmp_length = precision; |
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
348 /* Multiply by 2, as an estimate for FLAG_GROUP. */ |
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
349 tmp_length = xsum (tmp_length, tmp_length); |
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
350 /* Add 1, to account for a leading sign. */ |
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
351 tmp_length = xsum (tmp_length, 1); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
352 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
353 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
354 case 'o': |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
355 # if HAVE_LONG_LONG_INT |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
356 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
357 tmp_length = |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
358 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
359 * 0.333334 /* binary -> octal */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
360 ) |
6583
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
361 + 1; /* turn floor into ceil */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
362 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
363 # endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
364 if (type == TYPE_LONGINT || type == TYPE_ULONGINT) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
365 tmp_length = |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
366 (unsigned int) (sizeof (unsigned long) * CHAR_BIT |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
367 * 0.333334 /* binary -> octal */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
368 ) |
6583
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
369 + 1; /* turn floor into ceil */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
370 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
371 tmp_length = |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
372 (unsigned int) (sizeof (unsigned int) * CHAR_BIT |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
373 * 0.333334 /* binary -> octal */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
374 ) |
6583
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
375 + 1; /* turn floor into ceil */ |
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
376 if (tmp_length < precision) |
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
377 tmp_length = precision; |
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
378 /* Add 1, to account for a leading sign. */ |
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
379 tmp_length = xsum (tmp_length, 1); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
380 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
381 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
382 case 'x': case 'X': |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
383 # if HAVE_LONG_LONG_INT |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
384 if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
385 tmp_length = |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
386 (unsigned int) (sizeof (unsigned long long) * CHAR_BIT |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
387 * 0.25 /* binary -> hexadecimal */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
388 ) |
6583
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
389 + 1; /* turn floor into ceil */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
390 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
391 # endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
392 if (type == TYPE_LONGINT || type == TYPE_ULONGINT) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
393 tmp_length = |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
394 (unsigned int) (sizeof (unsigned long) * CHAR_BIT |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
395 * 0.25 /* binary -> hexadecimal */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
396 ) |
6583
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
397 + 1; /* turn floor into ceil */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
398 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
399 tmp_length = |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
400 (unsigned int) (sizeof (unsigned int) * CHAR_BIT |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
401 * 0.25 /* binary -> hexadecimal */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
402 ) |
6583
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
403 + 1; /* turn floor into ceil */ |
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
404 if (tmp_length < precision) |
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
405 tmp_length = precision; |
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
406 /* Add 2, to account for a leading sign or alternate form. */ |
eb8d54cb67a4
Fix an incorrect estimation of the sprintf result size.
Bruno Haible <bruno@clisp.org>
parents:
5848
diff
changeset
|
407 tmp_length = xsum (tmp_length, 2); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
408 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
409 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
410 case 'f': case 'F': |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
411 # if HAVE_LONG_DOUBLE |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
412 if (type == TYPE_LONGDOUBLE) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
413 tmp_length = |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
414 (unsigned int) (LDBL_MAX_EXP |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
415 * 0.30103 /* binary -> decimal */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
416 * 2 /* estimate for FLAG_GROUP */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
417 ) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
418 + 1 /* turn floor into ceil */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
419 + 10; /* sign, decimal point etc. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
420 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
421 # endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
422 tmp_length = |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
423 (unsigned int) (DBL_MAX_EXP |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
424 * 0.30103 /* binary -> decimal */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
425 * 2 /* estimate for FLAG_GROUP */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
426 ) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
427 + 1 /* turn floor into ceil */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
428 + 10; /* sign, decimal point etc. */ |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
429 tmp_length = xsum (tmp_length, precision); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
430 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
431 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
432 case 'e': case 'E': case 'g': case 'G': |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
433 case 'a': case 'A': |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
434 tmp_length = |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
435 12; /* sign, decimal point, exponent etc. */ |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
436 tmp_length = xsum (tmp_length, precision); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
437 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
438 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
439 case 'c': |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
440 # if HAVE_WINT_T && !WIDE_CHAR_VERSION |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
441 if (type == TYPE_WIDE_CHAR) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
442 tmp_length = MB_CUR_MAX; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
443 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
444 # endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
445 tmp_length = 1; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
446 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
447 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
448 case 's': |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
449 # if HAVE_WCHAR_T |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
450 if (type == TYPE_WIDE_STRING) |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
451 { |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
452 tmp_length = |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
453 local_wcslen (a.arg[dp->arg_index].a.a_wide_string); |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
454 |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
455 # if !WIDE_CHAR_VERSION |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
456 tmp_length = xtimes (tmp_length, MB_CUR_MAX); |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
457 # endif |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
458 } |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
459 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
460 # endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
461 tmp_length = strlen (a.arg[dp->arg_index].a.a_string); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
462 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
463 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
464 case 'p': |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
465 tmp_length = |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
466 (unsigned int) (sizeof (void *) * CHAR_BIT |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
467 * 0.25 /* binary -> hexadecimal */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
468 ) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
469 + 1 /* turn floor into ceil */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
470 + 2; /* account for leading 0x */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
471 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
472 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
473 default: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
474 abort (); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
475 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
476 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
477 if (tmp_length < width) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
478 tmp_length = width; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
479 |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
480 tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
481 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
482 |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
483 if (tmp_length <= sizeof (tmpbuf) / sizeof (CHAR_T)) |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
484 tmp = tmpbuf; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
485 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
486 { |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
487 size_t tmp_memsize = xtimes (tmp_length, sizeof (CHAR_T)); |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
488 |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
489 if (size_overflow_p (tmp_memsize)) |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
490 /* Overflow, would lead to out of memory. */ |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
491 goto out_of_memory; |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
492 tmp = (CHAR_T *) malloc (tmp_memsize); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
493 if (tmp == NULL) |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
494 /* Out of memory. */ |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
495 goto out_of_memory; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
496 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
497 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
498 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
499 /* Construct the format string for calling snprintf or |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
500 sprintf. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
501 p = buf; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
502 *p++ = '%'; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
503 if (dp->flags & FLAG_GROUP) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
504 *p++ = '\''; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
505 if (dp->flags & FLAG_LEFT) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
506 *p++ = '-'; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
507 if (dp->flags & FLAG_SHOWSIGN) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
508 *p++ = '+'; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
509 if (dp->flags & FLAG_SPACE) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
510 *p++ = ' '; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
511 if (dp->flags & FLAG_ALT) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
512 *p++ = '#'; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
513 if (dp->flags & FLAG_ZERO) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
514 *p++ = '0'; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
515 if (dp->width_start != dp->width_end) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
516 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
517 size_t n = dp->width_end - dp->width_start; |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
518 memcpy (p, dp->width_start, n * sizeof (CHAR_T)); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
519 p += n; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
520 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
521 if (dp->precision_start != dp->precision_end) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
522 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
523 size_t n = dp->precision_end - dp->precision_start; |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
524 memcpy (p, dp->precision_start, n * sizeof (CHAR_T)); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
525 p += n; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
526 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
527 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
528 switch (type) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
529 { |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
530 #if HAVE_LONG_LONG_INT |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
531 case TYPE_LONGLONGINT: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
532 case TYPE_ULONGLONGINT: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
533 *p++ = 'l'; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
534 /*FALLTHROUGH*/ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
535 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
536 case TYPE_LONGINT: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
537 case TYPE_ULONGINT: |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
538 #if HAVE_WINT_T |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
539 case TYPE_WIDE_CHAR: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
540 #endif |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
541 #if HAVE_WCHAR_T |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
542 case TYPE_WIDE_STRING: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
543 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
544 *p++ = 'l'; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
545 break; |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
546 #if HAVE_LONG_DOUBLE |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
547 case TYPE_LONGDOUBLE: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
548 *p++ = 'L'; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
549 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
550 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
551 default: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
552 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
553 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
554 *p = dp->conversion; |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
555 #if USE_SNPRINTF |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
556 p[1] = '%'; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
557 p[2] = 'n'; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
558 p[3] = '\0'; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
559 #else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
560 p[1] = '\0'; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
561 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
562 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
563 /* Construct the arguments for calling snprintf or sprintf. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
564 prefix_count = 0; |
4886
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4879
diff
changeset
|
565 if (dp->width_arg_index != ARG_NONE) |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
566 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
567 if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
568 abort (); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
569 prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
570 } |
4886
e4317f1f2e2c
Use size_t instead of ssize_t.
Bruno Haible <bruno@clisp.org>
parents:
4879
diff
changeset
|
571 if (dp->precision_arg_index != ARG_NONE) |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
572 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
573 if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
574 abort (); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
575 prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
576 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
577 |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
578 #if USE_SNPRINTF |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
579 /* Prepare checking whether snprintf returns the count |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
580 via %n. */ |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
581 ENSURE_ALLOCATION (xsum (length, 1)); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
582 result[length] = '\0'; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
583 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
584 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
585 for (;;) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
586 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
587 size_t maxlen; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
588 int count; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
589 int retcount; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
590 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
591 maxlen = allocated - length; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
592 count = -1; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
593 retcount = 0; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
594 |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
595 #if USE_SNPRINTF |
4704 | 596 # define SNPRINTF_BUF(arg) \ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
597 switch (prefix_count) \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
598 { \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
599 case 0: \ |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
600 retcount = SNPRINTF (result + length, maxlen, buf, \ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
601 arg, &count); \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
602 break; \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
603 case 1: \ |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
604 retcount = SNPRINTF (result + length, maxlen, buf, \ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
605 prefixes[0], arg, &count); \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
606 break; \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
607 case 2: \ |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
608 retcount = SNPRINTF (result + length, maxlen, buf, \ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
609 prefixes[0], prefixes[1], arg, \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
610 &count); \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
611 break; \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
612 default: \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
613 abort (); \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
614 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
615 #else |
4704 | 616 # define SNPRINTF_BUF(arg) \ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
617 switch (prefix_count) \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
618 { \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
619 case 0: \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
620 count = sprintf (tmp, buf, arg); \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
621 break; \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
622 case 1: \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
623 count = sprintf (tmp, buf, prefixes[0], arg); \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
624 break; \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
625 case 2: \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
626 count = sprintf (tmp, buf, prefixes[0], prefixes[1],\ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
627 arg); \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
628 break; \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
629 default: \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
630 abort (); \ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
631 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
632 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
633 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
634 switch (type) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
635 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
636 case TYPE_SCHAR: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
637 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
638 int arg = a.arg[dp->arg_index].a.a_schar; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
639 SNPRINTF_BUF (arg); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
640 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
641 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
642 case TYPE_UCHAR: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
643 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
644 unsigned int arg = a.arg[dp->arg_index].a.a_uchar; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
645 SNPRINTF_BUF (arg); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
646 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
647 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
648 case TYPE_SHORT: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
649 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
650 int arg = a.arg[dp->arg_index].a.a_short; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
651 SNPRINTF_BUF (arg); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
652 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
653 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
654 case TYPE_USHORT: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
655 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
656 unsigned int arg = a.arg[dp->arg_index].a.a_ushort; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
657 SNPRINTF_BUF (arg); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
658 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
659 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
660 case TYPE_INT: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
661 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
662 int arg = a.arg[dp->arg_index].a.a_int; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
663 SNPRINTF_BUF (arg); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
664 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
665 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
666 case TYPE_UINT: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
667 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
668 unsigned int arg = a.arg[dp->arg_index].a.a_uint; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
669 SNPRINTF_BUF (arg); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
670 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
671 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
672 case TYPE_LONGINT: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
673 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
674 long int arg = a.arg[dp->arg_index].a.a_longint; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
675 SNPRINTF_BUF (arg); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
676 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
677 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
678 case TYPE_ULONGINT: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
679 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
680 unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
681 SNPRINTF_BUF (arg); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
682 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
683 break; |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
684 #if HAVE_LONG_LONG_INT |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
685 case TYPE_LONGLONGINT: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
686 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
687 long long int arg = a.arg[dp->arg_index].a.a_longlongint; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
688 SNPRINTF_BUF (arg); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
689 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
690 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
691 case TYPE_ULONGLONGINT: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
692 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
693 unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
694 SNPRINTF_BUF (arg); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
695 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
696 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
697 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
698 case TYPE_DOUBLE: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
699 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
700 double arg = a.arg[dp->arg_index].a.a_double; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
701 SNPRINTF_BUF (arg); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
702 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
703 break; |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
704 #if HAVE_LONG_DOUBLE |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
705 case TYPE_LONGDOUBLE: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
706 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
707 long double arg = a.arg[dp->arg_index].a.a_longdouble; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
708 SNPRINTF_BUF (arg); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
709 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
710 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
711 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
712 case TYPE_CHAR: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
713 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
714 int arg = a.arg[dp->arg_index].a.a_char; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
715 SNPRINTF_BUF (arg); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
716 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
717 break; |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
718 #if HAVE_WINT_T |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
719 case TYPE_WIDE_CHAR: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
720 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
721 wint_t arg = a.arg[dp->arg_index].a.a_wide_char; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
722 SNPRINTF_BUF (arg); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
723 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
724 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
725 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
726 case TYPE_STRING: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
727 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
728 const char *arg = a.arg[dp->arg_index].a.a_string; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
729 SNPRINTF_BUF (arg); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
730 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
731 break; |
8051
9c2b0396b27c
Stylistic change: Use '#if HAVE_*' instead of '#ifdef HAVE_*'.
Bruno Haible <bruno@clisp.org>
parents:
7475
diff
changeset
|
732 #if HAVE_WCHAR_T |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
733 case TYPE_WIDE_STRING: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
734 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
735 const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
736 SNPRINTF_BUF (arg); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
737 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
738 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
739 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
740 case TYPE_POINTER: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
741 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
742 void *arg = a.arg[dp->arg_index].a.a_pointer; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
743 SNPRINTF_BUF (arg); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
744 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
745 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
746 default: |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
747 abort (); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
748 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
749 |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
750 #if USE_SNPRINTF |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
751 /* Portability: Not all implementations of snprintf() |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
752 are ISO C 99 compliant. Determine the number of |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
753 bytes that snprintf() has produced or would have |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
754 produced. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
755 if (count >= 0) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
756 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
757 /* Verify that snprintf() has NUL-terminated its |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
758 result. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
759 if (count < maxlen && result[length + count] != '\0') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
760 abort (); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
761 /* Portability hack. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
762 if (retcount > count) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
763 count = retcount; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
764 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
765 else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
766 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
767 /* snprintf() doesn't understand the '%n' |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
768 directive. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
769 if (p[1] != '\0') |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
770 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
771 /* Don't use the '%n' directive; instead, look |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
772 at the snprintf() return value. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
773 p[1] = '\0'; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
774 continue; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
775 } |
4801
83746ec4d74a
Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents:
4704
diff
changeset
|
776 else |
83746ec4d74a
Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents:
4704
diff
changeset
|
777 { |
83746ec4d74a
Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents:
4704
diff
changeset
|
778 /* Look at the snprintf() return value. */ |
83746ec4d74a
Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents:
4704
diff
changeset
|
779 if (retcount < 0) |
83746ec4d74a
Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents:
4704
diff
changeset
|
780 { |
83746ec4d74a
Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents:
4704
diff
changeset
|
781 /* HP-UX 10.20 snprintf() is doubly deficient: |
83746ec4d74a
Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents:
4704
diff
changeset
|
782 It doesn't understand the '%n' directive, |
83746ec4d74a
Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents:
4704
diff
changeset
|
783 *and* it returns -1 (rather than the length |
83746ec4d74a
Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents:
4704
diff
changeset
|
784 that would have been required) when the |
83746ec4d74a
Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents:
4704
diff
changeset
|
785 buffer is too small. */ |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
786 size_t bigger_need = |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
787 xsum (xtimes (allocated, 2), 12); |
4801
83746ec4d74a
Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents:
4704
diff
changeset
|
788 ENSURE_ALLOCATION (bigger_need); |
83746ec4d74a
Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents:
4704
diff
changeset
|
789 continue; |
83746ec4d74a
Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents:
4704
diff
changeset
|
790 } |
83746ec4d74a
Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents:
4704
diff
changeset
|
791 else |
83746ec4d74a
Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents:
4704
diff
changeset
|
792 count = retcount; |
83746ec4d74a
Portability to HP-UX 10, found by Jim Meyering.
Bruno Haible <bruno@clisp.org>
parents:
4704
diff
changeset
|
793 } |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
794 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
795 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
796 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
797 /* Attempt to handle failure. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
798 if (count < 0) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
799 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
800 if (!(result == resultbuf || result == NULL)) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
801 free (result); |
4879
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
802 if (buf_malloced != NULL) |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
803 free (buf_malloced); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
804 CLEANUP (); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
805 errno = EINVAL; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
806 return NULL; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
807 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
808 |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
809 #if !USE_SNPRINTF |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
810 if (count >= tmp_length) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
811 /* tmp_length was incorrectly calculated - fix the |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
812 code above! */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
813 abort (); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
814 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
815 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
816 /* Make room for the result. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
817 if (count >= maxlen) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
818 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
819 /* Need at least count bytes. But allocate |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
820 proportionally, to avoid looping eternally if |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
821 snprintf() reports a too small count. */ |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
822 size_t n = |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
823 xmax (xsum (length, count), xtimes (allocated, 2)); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
824 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
825 ENSURE_ALLOCATION (n); |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
826 #if USE_SNPRINTF |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
827 continue; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
828 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
829 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
830 |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
831 #if USE_SNPRINTF |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
832 /* The snprintf() result did fit. */ |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
833 #else |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
834 /* Append the sprintf() result. */ |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
835 memcpy (result + length, tmp, count * sizeof (CHAR_T)); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
836 if (tmp != tmpbuf) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
837 free (tmp); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
838 #endif |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
839 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
840 length += count; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
841 break; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
842 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
843 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
844 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
845 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
846 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
847 /* Add the final NUL. */ |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
848 ENSURE_ALLOCATION (xsum (length, 1)); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
849 result[length] = '\0'; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
850 |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
851 if (result != resultbuf && length + 1 < allocated) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
852 { |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
853 /* Shrink the allocated memory if possible. */ |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
854 CHAR_T *memory; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
855 |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
856 memory = (CHAR_T *) realloc (result, (length + 1) * sizeof (CHAR_T)); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
857 if (memory != NULL) |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
858 result = memory; |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
859 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
860 |
4879
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
861 if (buf_malloced != NULL) |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
862 free (buf_malloced); |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
863 CLEANUP (); |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
864 *lengthp = length; |
7218
0fd12ba5cfc6
Do the INT_MAX check only where size_t is converted to 'int'.
Bruno Haible <bruno@clisp.org>
parents:
6583
diff
changeset
|
865 /* Note that we can produce a big string of a length > INT_MAX. POSIX |
0fd12ba5cfc6
Do the INT_MAX check only where size_t is converted to 'int'.
Bruno Haible <bruno@clisp.org>
parents:
6583
diff
changeset
|
866 says that snprintf() fails with errno = EOVERFLOW in this case, but |
0fd12ba5cfc6
Do the INT_MAX check only where size_t is converted to 'int'.
Bruno Haible <bruno@clisp.org>
parents:
6583
diff
changeset
|
867 that's only because snprintf() returns an 'int'. This function does |
0fd12ba5cfc6
Do the INT_MAX check only where size_t is converted to 'int'.
Bruno Haible <bruno@clisp.org>
parents:
6583
diff
changeset
|
868 not have this limitation. */ |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
869 return result; |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
870 |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
871 out_of_memory: |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
872 if (!(result == resultbuf || result == NULL)) |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
873 free (result); |
4879
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
874 if (buf_malloced != NULL) |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
875 free (buf_malloced); |
9b96adbed643
Avoid alloca with too large size.
Bruno Haible <bruno@clisp.org>
parents:
4872
diff
changeset
|
876 out_of_memory_1: |
4872
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
877 CLEANUP (); |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
878 errno = ENOMEM; |
c5afc99b8ce5
Use xsize.h to protect against memory size overflows.
Bruno Haible <bruno@clisp.org>
parents:
4871
diff
changeset
|
879 return NULL; |
4224
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
880 } |
83eabea25586
New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
881 } |
4871
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
882 |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
883 #undef SNPRINTF |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
884 #undef USE_SNPRINTF |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
885 #undef PRINTF_PARSE |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
886 #undef DIRECTIVES |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
887 #undef DIRECTIVE |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
888 #undef CHAR_T |
87df35000dab
Merge support for wide characters, from GNU gettext.
Bruno Haible <bruno@clisp.org>
parents:
4801
diff
changeset
|
889 #undef VASNPRINTF |