1 /* libstdbuf -- a shared lib to preload to setup stdio buffering for a command
2 Copyright (C) 2009-2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Pádraig Brady. LD_PRELOAD idea from Brian Dessent. */
25 /* Note currently for glibc (2.3.5) the following call does not change
26 the buffer size, and more problematically does not give any indication
27 that the new size request was ignored:
29 setvbuf (stdout, (char*)NULL, _IOFBF, 8192);
31 The ISO C99 standard section 7.19.5.6 on the setvbuf function says:
33 ... If buf is not a null pointer, the array it points to _may_ be used
34 instead of a buffer allocated by the setvbuf function and the argument
35 size specifies the size of the array; otherwise, size _may_ determine
36 the size of a buffer allocated by the setvbuf function. ...
38 Obviously some interpret the above to mean setvbuf(....,size)
39 is only a hint from the application which I don't agree with.
41 FreeBSD's libc seems more sensible in this regard. From the man page:
43 The size argument may be given as zero to obtain deferred optimal-size
44 buffer allocation as usual. If it is not zero, then except for
45 unbuffered files, the buf argument should point to a buffer at least size
46 bytes long; this buffer will be used instead of the current buffer. (If
47 the size argument is not zero but buf is NULL, a buffer of the given size
48 will be allocated immediately, and released on close. This is an extension
49 to ANSI C; portable code should use a size of 0 with any NULL buffer.)
51 Another issue is that on glibc-2.7 the following doesn't buffer
52 the first write if it's greater than 1 byte.
54 setvbuf(stdout,buf,_IOFBF,127);
56 Now the POSIX standard says that "allocating a buffer of size bytes does
57 not necessarily imply that all of size bytes are used for the buffer area".
58 However I think it's just a buggy implementation due to the various
59 inconsistencies with write sizes and subsequent writes. */
62 fileno_to_name (const int fd
)
64 const char *ret
= NULL
;
86 apply_mode (FILE *stream
, const char *mode
)
93 setvbuf_mode
= _IONBF
;
94 else if (*mode
== 'L')
95 setvbuf_mode
= _IOLBF
; /* FIXME: should we allow 1ML */
98 setvbuf_mode
= _IOFBF
;
99 verify (SIZE_MAX
<= ULONG_MAX
);
100 size
= strtoul (mode
, NULL
, 10);
103 if (!(buf
= malloc (size
))) /* will be freed by fclose() */
105 /* We could defer the allocation to libc, however since
106 glibc currently ignores the combination of NULL buffer
107 with non zero size, we'll fail here. */
109 _("failed to allocate a %" PRIuMAX
110 " byte stdio buffer\n"), (uintmax_t) size
);
116 fprintf (stderr
, _("invalid buffering mode %s for %s\n"),
117 mode
, fileno_to_name (fileno (stream
)));
122 if (setvbuf (stream
, buf
, setvbuf_mode
, size
) != 0)
124 fprintf (stderr
, _("could not set buffering of %s to mode %s\n"),
125 fileno_to_name (fileno (stream
)), mode
);
130 __attribute__ ((constructor
)) static void
133 char *e_mode
= getenv ("_STDBUF_E");
134 char *i_mode
= getenv ("_STDBUF_I");
135 char *o_mode
= getenv ("_STDBUF_O");
136 if (e_mode
) /* Do first so can write errors to stderr */
137 apply_mode (stderr
, e_mode
);
139 apply_mode (stdin
, i_mode
);
141 apply_mode (stdout
, o_mode
);