annotate lib/vdprintf.c @ 11033:049c445485ee

New modules 'vdprintf', 'vdprintf-posix'.
author Bruno Haible <bruno@clisp.org>
date Sun, 18 Jan 2009 02:48:41 +0100
parents
children e8d2c6fc33ad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11033
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Formatted output to a file descriptor.
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 2009 Free Software Foundation, Inc.
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 (at your option) any later version.
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 #ifdef HAVE_CONFIG_H
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 # include <config.h>
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #endif
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 /* Specification. */
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include <stdio.h>
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <errno.h>
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include <limits.h>
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include <stdarg.h>
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #include <stdlib.h>
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 #include "full-write.h"
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #include "vasnprintf.h"
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 int
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33 vdprintf (int fd, const char *format, va_list args)
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 {
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 char buf[2000];
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 char *output;
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 size_t len;
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 size_t lenbuf = sizeof (buf);
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 output = vasnprintf (buf, &lenbuf, format, args);
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 len = lenbuf;
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 if (!output)
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 return -1;
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 if (full_write (fd, output, len) < len)
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 {
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 if (output != buf)
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 {
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 int saved_errno = errno;
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 free (output);
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 errno = saved_errno;
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 }
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 return -1;
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 }
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 if (len > INT_MAX)
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 {
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 errno = EOVERFLOW;
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 return -1;
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 }
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 return len;
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 }