stty: fix sane setting of susp to ^z on Solaris
[coreutils.git] / src / libstdbuf.c
blob885377a861cc1bac77eafd2a90b8b381a7fc5dc1
1 /* libstdbuf -- a shared lib to preload to setup stdio buffering for a command
2 Copyright (C) 2009-2016 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 "system.h"
23 /* Note currently for glibc (2.3.5) the following call does not change
24 the buffer size, and more problematically does not give any indication
25 that the new size request was ignored:
27 setvbuf (stdout, (char*)NULL, _IOFBF, 8192);
29 The ISO C99 standard section 7.19.5.6 on the setvbuf function says:
31 ... If buf is not a null pointer, the array it points to _may_ be used
32 instead of a buffer allocated by the setvbuf function and the argument
33 size specifies the size of the array; otherwise, size _may_ determine
34 the size of a buffer allocated by the setvbuf function. ...
36 Obviously some interpret the above to mean setvbuf(....,size)
37 is only a hint from the application which I don't agree with.
39 FreeBSD's libc seems more sensible in this regard. From the man page:
41 The size argument may be given as zero to obtain deferred optimal-size
42 buffer allocation as usual. If it is not zero, then except for
43 unbuffered files, the buf argument should point to a buffer at least size
44 bytes long; this buffer will be used instead of the current buffer. (If
45 the size argument is not zero but buf is NULL, a buffer of the given size
46 will be allocated immediately, and released on close. This is an extension
47 to ANSI C; portable code should use a size of 0 with any NULL buffer.)
48 --------------------
49 Another issue is that on glibc-2.7 the following doesn't buffer
50 the first write if it's greater than 1 byte.
52 setvbuf(stdout,buf,_IOFBF,127);
54 Now the POSIX standard says that "allocating a buffer of size bytes does
55 not necessarily imply that all of size bytes are used for the buffer area".
56 However I think it's just a buggy implementation due to the various
57 inconsistencies with write sizes and subsequent writes. */
59 static const char *
60 fileno_to_name (const int fd)
62 const char *ret = NULL;
64 switch (fd)
66 case 0:
67 ret = "stdin";
68 break;
69 case 1:
70 ret = "stdout";
71 break;
72 case 2:
73 ret = "stderr";
74 break;
75 default:
76 ret = "unknown";
77 break;
80 return ret;
83 static void
84 apply_mode (FILE *stream, const char *mode)
86 char *buf = NULL;
87 int setvbuf_mode;
88 size_t size = 0;
90 if (*mode == '0')
91 setvbuf_mode = _IONBF;
92 else if (*mode == 'L')
93 setvbuf_mode = _IOLBF; /* FIXME: should we allow 1ML */
94 else
96 setvbuf_mode = _IOFBF;
97 verify (SIZE_MAX <= ULONG_MAX);
98 size = strtoul (mode, NULL, 10);
99 if (size > 0)
101 if (!(buf = malloc (size))) /* will be freed by fclose() */
103 /* We could defer the allocation to libc, however since
104 glibc currently ignores the combination of NULL buffer
105 with non zero size, we'll fail here. */
106 fprintf (stderr,
107 _("failed to allocate a %" PRIuMAX
108 " byte stdio buffer\n"), (uintmax_t) size);
109 return;
112 else
114 fprintf (stderr, _("invalid buffering mode %s for %s\n"),
115 mode, fileno_to_name (fileno (stream)));
116 return;
120 if (setvbuf (stream, buf, setvbuf_mode, size) != 0)
122 fprintf (stderr, _("could not set buffering of %s to mode %s\n"),
123 fileno_to_name (fileno (stream)), mode);
124 free (buf);
128 /* Use __attribute to avoid elision of __attribute__ on SUNPRO_C etc. */
129 static void __attribute ((constructor))
130 stdbuf (void)
132 char *e_mode = getenv ("_STDBUF_E");
133 char *i_mode = getenv ("_STDBUF_I");
134 char *o_mode = getenv ("_STDBUF_O");
135 if (e_mode) /* Do first so can write errors to stderr */
136 apply_mode (stderr, e_mode);
137 if (i_mode)
138 apply_mode (stdin, i_mode);
139 if (o_mode)
140 apply_mode (stdout, o_mode);