annotate lib/vasnprintf.h @ 17363:5a51fb7777a9

sys_select, sys_time: port 2013-01-30 Solaris 2.6 fix to Cygwin Problem reported by Marco Atzeri in <http://lists.gnu.org/archive/html/bug-gnulib/2013-03/msg00000.html>. * lib/sys_select.in.h [HAVE_SYS_SELECT_H && _CYGWIN_SYS_TIME_H]: Simply delegate to the system <sys/select.h> in this case too. Also, pay attention to _GL_SYS_SELECT_H_REDIRECT_FROM_SYS_TIME_H only if OSF/1, since otherwise Cygwin breaks, and it doesn't seem to be needed on Solaris either. * lib/sys_time.in.h [_CYGWIN_SYS_TIME_H]: Simply delgate to the system <sys/time.h> in this case.
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 19 Mar 2013 09:08:47 -0700
parents e542fd46ad6f
children 344018b6e5d7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* vsprintf with automatic memory allocation.
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 16366
diff changeset
2 Copyright (C) 2002-2004, 2007-2013 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: 4224
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: 4224
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: 4224
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: 4224
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: 4224
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: 4224
diff changeset
14 You should have received a copy of the GNU General Public License along
16366
bb182ee4a09d maint: replace FSF snail-mail addresses with URLs
Paul Eggert <eggert@cs.ucla.edu>
parents: 16201
diff changeset
15 with this program; if not, see <http://www.gnu.org/licenses/>. */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 #ifndef _VASNPRINTF_H
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 #define _VASNPRINTF_H
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 /* Get va_list. */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include <stdarg.h>
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 /* Get size_t. */
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <stddef.h>
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
12762
410eaa89fb57 Avoid a link error due to the __printf__ symbol.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
26 /* The __attribute__ feature is available in gcc versions 2.5 and later.
410eaa89fb57 Avoid a link error due to the __printf__ symbol.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
27 The __-protected variants of the attributes 'format' and 'printf' are
410eaa89fb57 Avoid a link error due to the __printf__ symbol.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
28 accepted by gcc versions 2.6.4 (effectively 2.7) and later.
14350
4cf3b58aaf12 Don't interfere with a program's definition of __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
29 We enable _GL_ATTRIBUTE_FORMAT only if these are supported too, because
12762
410eaa89fb57 Avoid a link error due to the __printf__ symbol.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
30 gnulib and libintl do '#define printf __printf__' when they override
410eaa89fb57 Avoid a link error due to the __printf__ symbol.
Bruno Haible <bruno@clisp.org>
parents: 12559
diff changeset
31 the 'printf' function. */
14350
4cf3b58aaf12 Don't interfere with a program's definition of __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
32 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
4cf3b58aaf12 Don't interfere with a program's definition of __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
33 # define _GL_ATTRIBUTE_FORMAT(spec) __attribute__ ((__format__ spec))
4cf3b58aaf12 Don't interfere with a program's definition of __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
34 #else
4cf3b58aaf12 Don't interfere with a program's definition of __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
35 # define _GL_ATTRIBUTE_FORMAT(spec) /* empty */
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37
8386
4ec84f742938 Convert tabs in the middle of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 8338
diff changeset
38 #ifdef __cplusplus
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 extern "C" {
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 /* Write formatted output to a string dynamically allocated with malloc().
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 You can pass a preallocated buffer for the result in RESULTBUF and its
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 size in *LENGTHP; otherwise you pass RESULTBUF = NULL.
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 If successful, return the address of the string (this may be = RESULTBUF
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 if no dynamic memory allocation was necessary) and set *LENGTHP to the
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 number of resulting bytes, excluding the trailing NUL. Upon error, set
5297
c0fb011695d9 Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 4440
diff changeset
48 errno and return NULL.
c0fb011695d9 Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 4440
diff changeset
49
c0fb011695d9 Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 4440
diff changeset
50 When dynamic memory allocation occurs, the preallocated buffer is left
c0fb011695d9 Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 4440
diff changeset
51 alone (with possibly modified contents). This makes it possible to use
c0fb011695d9 Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 4440
diff changeset
52 a statically allocated or stack-allocated buffer, like this:
c0fb011695d9 Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 4440
diff changeset
53
c0fb011695d9 Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 4440
diff changeset
54 char buf[100];
c0fb011695d9 Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 4440
diff changeset
55 size_t len = sizeof (buf);
c0fb011695d9 Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 4440
diff changeset
56 char *output = vasnprintf (buf, &len, format, args);
c0fb011695d9 Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 4440
diff changeset
57 if (output == NULL)
c0fb011695d9 Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 4440
diff changeset
58 ... error handling ...;
c0fb011695d9 Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 4440
diff changeset
59 else
c0fb011695d9 Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 4440
diff changeset
60 {
c0fb011695d9 Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 4440
diff changeset
61 ... use the output string ...;
c0fb011695d9 Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 4440
diff changeset
62 if (output != buf)
c0fb011695d9 Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 4440
diff changeset
63 free (output);
c0fb011695d9 Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 4440
diff changeset
64 }
c0fb011695d9 Add comment about preallocated buffer. Suggested by Simon Josefsson.
Bruno Haible <bruno@clisp.org>
parents: 4440
diff changeset
65 */
8338
557787fcc6cd Use function name rpl_vasnprintf if vasnprintf already exists.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
66 #if REPLACE_VASNPRINTF
557787fcc6cd Use function name rpl_vasnprintf if vasnprintf already exists.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
67 # define asnprintf rpl_asnprintf
557787fcc6cd Use function name rpl_vasnprintf if vasnprintf already exists.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
68 # define vasnprintf rpl_vasnprintf
557787fcc6cd Use function name rpl_vasnprintf if vasnprintf already exists.
Bruno Haible <bruno@clisp.org>
parents: 5848
diff changeset
69 #endif
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 extern char * asnprintf (char *resultbuf, size_t *lengthp, const char *format, ...)
14350
4cf3b58aaf12 Don't interfere with a program's definition of __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
71 _GL_ATTRIBUTE_FORMAT ((__printf__, 3, 4));
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 extern char * vasnprintf (char *resultbuf, size_t *lengthp, const char *format, va_list args)
14350
4cf3b58aaf12 Don't interfere with a program's definition of __attribute__.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
73 _GL_ATTRIBUTE_FORMAT ((__printf__, 3, 0));
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74
8386
4ec84f742938 Convert tabs in the middle of preprocessor directives.
Bruno Haible <bruno@clisp.org>
parents: 8338
diff changeset
75 #ifdef __cplusplus
4224
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 }
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 #endif
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78
83eabea25586 New modules vasnprintf and vasprintf.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 #endif /* _VASNPRINTF_H */