shred: increase I/O block size for periodic pattern case
[coreutils.git] / src / libstdbuf.c
blobac7a1339d00a06d5ac59e0a1d306711563d10d12
1 /* libstdbuf -- a shared lib to preload to setup stdio buffering for a command
2 Copyright (C) 2009-2013 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. */
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "system.h"
23 #include "verify.h"
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.)
50 --------------------
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. */
61 static const char *
62 fileno_to_name (const int fd)
64 const char *ret = NULL;
66 switch (fd)
68 case 0:
69 ret = "stdin";
70 break;
71 case 1:
72 ret = "stdout";
73 break;
74 case 2:
75 ret = "stderr";
76 break;
77 default:
78 ret = "unknown";
79 break;
82 return ret;
85 static void
86 apply_mode (FILE *stream, const char *mode)
88 char *buf = NULL;
89 int setvbuf_mode;
90 size_t size = 0;
92 if (*mode == '0')
93 setvbuf_mode = _IONBF;
94 else if (*mode == 'L')
95 setvbuf_mode = _IOLBF; /* FIXME: should we allow 1ML */
96 else
98 setvbuf_mode = _IOFBF;
99 verify (SIZE_MAX <= ULONG_MAX);
100 size = strtoul (mode, NULL, 10);
101 if (size > 0)
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. */
108 fprintf (stderr,
109 _("failed to allocate a %" PRIuMAX
110 " byte stdio buffer\n"), (uintmax_t) size);
111 return;
114 else
116 fprintf (stderr, _("invalid buffering mode %s for %s\n"),
117 mode, fileno_to_name (fileno (stream)));
118 return;
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);
126 free (buf);
130 __attribute__ ((constructor)) static void
131 stdbuf (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);
138 if (i_mode)
139 apply_mode (stdin, i_mode);
140 if (o_mode)
141 apply_mode (stdout, o_mode);