annotate lib/obstack_printf.c @ 14379:2330aac2ae54

maint: adjust cpp indentation to reflect nesting depth I.e., in a block of code that begins with an unnested "#if", put one space between the "#" in column 1 and following token. For example, -#include <sys/vfs.h> +# include <sys/vfs.h> Do this only in .c files that are part of a module I maintain. * lib/linkat.c: Filter through cppi. * lib/nanosleep.c: Likewise. * lib/openat.c: Likewise. * lib/openat-die.c: Likewise. * lib/dup3.c: Likewise. * lib/fchownat.c: Likewise. * lib/flock.c: Likewise. * lib/fsync.c: Likewise. * lib/fts.c: Likewise. * lib/getpass.c: Likewise. * lib/gettimeofday.c: Likewise. * lib/userspec.c: Likewise. * Makefile (sc_cpp_indent_check): New rule, to check this.
author Jim Meyering <meyering@redhat.com>
date Sun, 20 Feb 2011 23:02:43 +0100
parents 97fc9a21a8fb
children 8250f2777afc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10205
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
1 /* Formatted output to obstacks.
14079
97fc9a21a8fb maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents: 12559
diff changeset
2 Copyright (C) 2008-2011 Free Software Foundation, Inc.
10205
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
3
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
4 This program is free software; you can redistribute it and/or modify
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
6 the Free Software Foundation; either version 2, or (at your option)
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
7 any later version.
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
8
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
12 GNU General Public License for more details.
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
13
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License along
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
15 with this program; if not, write to the Free Software Foundation,
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
17
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
18 #include <config.h>
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
19
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
20 /* Specification. */
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
21 #include <stdio.h>
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
22
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
23 #include "obstack.h"
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
24 #include "vasnprintf.h"
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
25
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
26 #include <errno.h>
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
27 #include <stdarg.h>
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
28 #include <stdlib.h>
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
29
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
30 /* Grow an obstack with formatted output. Return the number of bytes
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
31 added to OBS. No trailing nul byte is added, and the object should
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
32 be closed with obstack_finish before use.
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
33
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
34 Upon memory allocation error, call obstack_alloc_failed_handler.
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
35 Upon other error, return -1. */
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
36 int
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
37 obstack_printf (struct obstack *obs, const char *format, ...)
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
38 {
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
39 va_list args;
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
40 int result;
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
41
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
42 va_start (args, format);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
43 result = obstack_vprintf (obs, format, args);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
44 va_end (args);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
45 return result;
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
46 }
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
47
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
48 /* Grow an obstack with formatted output. Return the number of bytes
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
49 added to OBS. No trailing nul byte is added, and the object should
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
50 be closed with obstack_finish before use.
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
51
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
52 Upon memory allocation error, call obstack_alloc_failed_handler.
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
53 Upon other error, return -1. */
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
54 int
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
55 obstack_vprintf (struct obstack *obs, const char *format, va_list args)
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
56 {
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
57 /* If we are close to the end of the current obstack chunk, use a
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
58 stack-allocated buffer and copy, to reduce the likelihood of a
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
59 small-size malloc. Otherwise, print directly into the
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
60 obstack. */
10206
927afe014a7d Fix various problems in 'obstack-printf' module.
Bruno Haible <bruno@clisp.org>
parents: 10205
diff changeset
61 enum { CUTOFF = 1024 };
927afe014a7d Fix various problems in 'obstack-printf' module.
Bruno Haible <bruno@clisp.org>
parents: 10205
diff changeset
62 char buf[CUTOFF];
10205
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
63 char *base = obstack_next_free (obs);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
64 size_t len = obstack_room (obs);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
65 char *str;
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
66
10206
927afe014a7d Fix various problems in 'obstack-printf' module.
Bruno Haible <bruno@clisp.org>
parents: 10205
diff changeset
67 if (len < CUTOFF)
10205
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
68 {
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
69 base = buf;
10206
927afe014a7d Fix various problems in 'obstack-printf' module.
Bruno Haible <bruno@clisp.org>
parents: 10205
diff changeset
70 len = CUTOFF;
10205
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
71 }
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
72 str = vasnprintf (base, &len, format, args);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
73 if (!str)
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
74 {
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
75 if (errno == ENOMEM)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10206
diff changeset
76 obstack_alloc_failed_handler ();
10205
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
77 return -1;
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
78 }
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
79 if (str == base && str != buf)
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
80 /* The output was already computed in place, but we need to
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
81 account for its size. */
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
82 obstack_blank_fast (obs, len);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
83 else
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
84 {
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
85 /* The output exceeded available obstack space or we used buf;
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10206
diff changeset
86 copy the resulting string. */
10205
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
87 obstack_grow (obs, str, len);
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
88 if (str != buf)
12421
e8d2c6fc33ad Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents: 10206
diff changeset
89 free (str);
10205
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
90 }
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
91 return len;
3384541effec Add obstack-printf and obstack-printf-posix modules.
Eric Blake <ebb9@byu.net>
parents:
diff changeset
92 }