annotate lib/vdprintf.c @ 17907:0a1c2535cad9

euidaccess: Fix Android build * modules/euidaccess (Depends-on): Add fcntl-h to ensure that AT_EACCESS gets declared.
author Kevin Cernekee <cernekee@google.com>
date Wed, 11 Feb 2015 15:22:54 -0800
parents ab58d4870664
children
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.
17848
ab58d4870664 version-etc: new year
Paul Eggert <eggert@cs.ucla.edu>
parents: 17587
diff changeset
2 Copyright (C) 2009-2015 Free Software Foundation, Inc.
11033
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)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11033
diff changeset
49 {
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11033
diff changeset
50 int saved_errno = errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11033
diff changeset
51 free (output);
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11033
diff changeset
52 errno = saved_errno;
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 11033
diff changeset
53 }
11033
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
12449
eccd5d7c7a69 *printf: Fix memory leak.
Joel E. Denny <jdenny@clemson.edu>
parents: 12421
diff changeset
57 if (output != buf)
eccd5d7c7a69 *printf: Fix memory leak.
Joel E. Denny <jdenny@clemson.edu>
parents: 12421
diff changeset
58 free (output);
eccd5d7c7a69 *printf: Fix memory leak.
Joel E. Denny <jdenny@clemson.edu>
parents: 12421
diff changeset
59
11033
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 if (len > INT_MAX)
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 errno = EOVERFLOW;
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 return -1;
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 }
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 return len;
049c445485ee New modules 'vdprintf', 'vdprintf-posix'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 }