]>
Commit | Line | Data |
---|---|---|
bb70624e JA |
1 | /* Copyright (C) 1999 Free Software Foundation, Inc. |
2 | ||
3 | This file is part of GNU Bash, the Bourne Again SHell. | |
4 | ||
5 | Bash is free software; you can redistribute it and/or modify it under | |
6 | the terms of the GNU General Public License as published by the Free | |
7 | Software Foundation; either version 2, or (at your option) any later | |
8 | version. | |
9 | ||
10 | Bash is distributed in the hope that it will be useful, but WITHOUT ANY | |
11 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
13 | for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License along | |
16 | with Bash; see the file COPYING. If not, write to the Free Software | |
17 | Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ | |
18 | ||
19 | #include <config.h> | |
20 | ||
21 | #include <sys/types.h> | |
22 | ||
23 | #if defined (HAVE_UNISTD_H) | |
24 | # include <unistd.h> | |
25 | #endif | |
26 | ||
27 | #include <errno.h> | |
28 | ||
29 | #if !defined (errno) | |
30 | extern int errno; | |
31 | #endif | |
32 | ||
33 | #ifndef SEEK_CUR | |
34 | # define SEEK_CUR 1 | |
35 | #endif | |
36 | ||
37 | /* Read LEN bytes from FD into BUF. Retry the read on EINTR. Any other | |
38 | error causes the loop to break. */ | |
f73dda09 | 39 | ssize_t |
bb70624e JA |
40 | zread (fd, buf, len) |
41 | int fd; | |
42 | char *buf; | |
43 | size_t len; | |
44 | { | |
f73dda09 | 45 | ssize_t r; |
bb70624e JA |
46 | |
47 | while ((r = read (fd, buf, len)) < 0 && errno == EINTR) | |
48 | ; | |
49 | return r; | |
50 | } | |
51 | ||
52 | /* Read LEN bytes from FD into BUF. Retry the read on EINTR, up to three | |
53 | interrupts. Any other error causes the loop to break. */ | |
54 | ||
55 | #ifdef NUM_INTR | |
56 | # undef NUM_INTR | |
57 | #endif | |
58 | #define NUM_INTR 3 | |
59 | ||
f73dda09 | 60 | ssize_t |
bb70624e JA |
61 | zread1 (fd, buf, len) |
62 | int fd; | |
63 | char *buf; | |
64 | size_t len; | |
65 | { | |
f73dda09 JA |
66 | ssize_t r; |
67 | int nintr; | |
bb70624e JA |
68 | |
69 | for (nintr = 0; ; ) | |
70 | { | |
71 | r = read (fd, buf, len); | |
72 | if (r >= 0) | |
28ef6c31 | 73 | return r; |
bb70624e JA |
74 | if (r == -1 && errno == EINTR) |
75 | { | |
76 | if (++nintr > NUM_INTR) | |
77 | return -1; | |
78 | continue; | |
79 | } | |
80 | return r; | |
81 | } | |
82 | } | |
83 | ||
84 | /* Read one character from FD and return it in CP. Return values are as | |
85 | in read(2). This does some local buffering to avoid many one-character | |
86 | calls to read(2), like those the `read' builtin performs. */ | |
87 | ||
f73dda09 JA |
88 | static char lbuf[128]; |
89 | static size_t lind, lused; | |
bb70624e | 90 | |
f73dda09 | 91 | ssize_t |
bb70624e JA |
92 | zreadc (fd, cp) |
93 | int fd; | |
94 | char *cp; | |
95 | { | |
f73dda09 | 96 | ssize_t nr; |
bb70624e JA |
97 | |
98 | if (lind == lused || lused == 0) | |
99 | { | |
f73dda09 | 100 | nr = zread (fd, lbuf, sizeof (lbuf)); |
bb70624e | 101 | lind = 0; |
f73dda09 JA |
102 | if (nr <= 0) |
103 | { | |
104 | lused = 0; | |
105 | return nr; | |
106 | } | |
107 | lused = nr; | |
bb70624e JA |
108 | } |
109 | if (cp) | |
f73dda09 | 110 | *cp = lbuf[lind++]; |
bb70624e JA |
111 | return 1; |
112 | } | |
113 | ||
114 | void | |
115 | zreset () | |
116 | { | |
117 | lind = lused = 0; | |
118 | } | |
119 | ||
120 | /* Sync the seek pointer for FD so that the kernel's idea of the last char | |
121 | read is the last char returned by zreadc. */ | |
122 | void | |
123 | zsyncfd (fd) | |
124 | int fd; | |
125 | { | |
f73dda09 | 126 | off_t off; |
bb70624e JA |
127 | |
128 | off = lused - lind; | |
129 | if (off > 0) | |
130 | lseek (fd, -off, SEEK_CUR); | |
131 | lused = lind = 0; | |
132 | } |