Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 1 | /* setlinebuf.c - line-buffer a stdio stream. */ |
| 2 | |
| 3 | /* Copyright (C) 1997 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GNU Bash, the Bourne Again SHell. |
| 6 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 7 | Bash is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 11 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 12 | Bash is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 16 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 17 | You should have received a copy of the GNU General Public License |
| 18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 20 | |
| 21 | #include <config.h> |
| 22 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 25 | #include <xmalloc.h> |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 26 | |
| 27 | #if defined (USING_BASH_MALLOC) |
| 28 | # define LBUF_BUFSIZE 1008 |
| 29 | #else |
| 30 | # define LBUF_BUFSIZE BUFSIZ |
| 31 | #endif |
| 32 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 33 | /* Cause STREAM to buffer lines as opposed to characters or blocks. */ |
| 34 | int |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 35 | sh_setlinebuf (stream) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 36 | FILE *stream; |
| 37 | { |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 38 | char *local_linebuf; |
| 39 | |
| 40 | #if !defined (HAVE_SETLINEBUF) && !defined (HAVE_SETVBUF) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 41 | return (0); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 42 | #endif |
| 43 | |
| 44 | #if defined (USING_BASH_MALLOC) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 45 | local_linebuf = (char *)xmalloc (LBUF_BUFSIZE); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 46 | #else |
| 47 | local_linebuf = (char *)NULL; |
| 48 | #endif |
| 49 | |
| 50 | #if defined (HAVE_SETVBUF) |
| 51 | |
| 52 | # if defined (SETVBUF_REVERSED) |
| 53 | return (setvbuf (stream, _IOLBF, local_linebuf, LBUF_BUFSIZE)); |
| 54 | # else /* !SETVBUF_REVERSED */ |
| 55 | return (setvbuf (stream, local_linebuf, _IOLBF, LBUF_BUFSIZE)); |
| 56 | # endif /* !SETVBUF_REVERSED */ |
| 57 | # else /* !HAVE_SETVBUF */ |
| 58 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 59 | setlinebuf (stream); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 60 | return (0); |
| 61 | |
| 62 | #endif /* !HAVE_SETVBUF */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 63 | } |