| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/bash/src/lib/sh/zread.c |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /* zread - read data from file descriptor into buffer with retries */ | - | ||||||||||||
| 2 | - | |||||||||||||
| 3 | /* Copyright (C) 1999-2017 Free Software Foundation, Inc. | - | ||||||||||||
| 4 | - | |||||||||||||
| 5 | This file is part of GNU Bash, the Bourne Again SHell. | - | ||||||||||||
| 6 | - | |||||||||||||
| 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. | - | ||||||||||||
| 11 | - | |||||||||||||
| 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. | - | ||||||||||||
| 16 | - | |||||||||||||
| 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 | */ | - | ||||||||||||
| 20 | - | |||||||||||||
| 21 | #include <config.h> | - | ||||||||||||
| 22 | - | |||||||||||||
| 23 | #include <sys/types.h> | - | ||||||||||||
| 24 | - | |||||||||||||
| 25 | #if defined (HAVE_UNISTD_H) | - | ||||||||||||
| 26 | # include <unistd.h> | - | ||||||||||||
| 27 | #endif | - | ||||||||||||
| 28 | - | |||||||||||||
| 29 | #include <signal.h> | - | ||||||||||||
| 30 | #include <errno.h> | - | ||||||||||||
| 31 | - | |||||||||||||
| 32 | #if !defined (errno) | - | ||||||||||||
| 33 | extern int errno; | - | ||||||||||||
| 34 | #endif | - | ||||||||||||
| 35 | - | |||||||||||||
| 36 | #ifndef SEEK_CUR | - | ||||||||||||
| 37 | # define SEEK_CUR 1 | - | ||||||||||||
| 38 | #endif | - | ||||||||||||
| 39 | - | |||||||||||||
| 40 | extern int executing_builtin; | - | ||||||||||||
| 41 | - | |||||||||||||
| 42 | extern void check_signals_and_traps (void); | - | ||||||||||||
| 43 | extern void check_signals (void); | - | ||||||||||||
| 44 | extern int signal_is_trapped (int); | - | ||||||||||||
| 45 | - | |||||||||||||
| 46 | /* Read LEN bytes from FD into BUF. Retry the read on EINTR. Any other | - | ||||||||||||
| 47 | error causes the loop to break. */ | - | ||||||||||||
| 48 | ssize_t | - | ||||||||||||
| 49 | zread (fd, buf, len) | - | ||||||||||||
| 50 | int fd; | - | ||||||||||||
| 51 | char *buf; | - | ||||||||||||
| 52 | size_t len; | - | ||||||||||||
| 53 | { | - | ||||||||||||
| 54 | ssize_t r; | - | ||||||||||||
| 55 | - | |||||||||||||
| 56 | check_signals (); /* check for signals before a blocking read */ | - | ||||||||||||
| 57 | while ((r = read (fd, buf, len)) < 0 && errno == EINTR)
| 1-8538293 | ||||||||||||
| 58 | /* XXX - bash-5.0 */ | - | ||||||||||||
| 59 | /* We check executing_builtin and run traps here for backwards compatibility */ | - | ||||||||||||
| 60 | if (executing_builtin)
| 0-1 | ||||||||||||
| 61 | check_signals_and_traps (); /* XXX - should it be check_signals()? */ executed 1 time by 1 test: check_signals_and_traps ();Executed by:
| 1 | ||||||||||||
| 62 | else | - | ||||||||||||
| 63 | check_signals (); never executed: check_signals (); | 0 | ||||||||||||
| 64 | - | |||||||||||||
| 65 | return r; executed 8538294 times by 1 test: return r;Executed by:
| 8538294 | ||||||||||||
| 66 | } | - | ||||||||||||
| 67 | - | |||||||||||||
| 68 | /* Read LEN bytes from FD into BUF. Retry the read on EINTR, up to three | - | ||||||||||||
| 69 | interrupts. Any other error causes the loop to break. */ | - | ||||||||||||
| 70 | - | |||||||||||||
| 71 | #ifdef NUM_INTR | - | ||||||||||||
| 72 | # undef NUM_INTR | - | ||||||||||||
| 73 | #endif | - | ||||||||||||
| 74 | #define NUM_INTR 3 | - | ||||||||||||
| 75 | - | |||||||||||||
| 76 | ssize_t | - | ||||||||||||
| 77 | zreadretry (fd, buf, len) | - | ||||||||||||
| 78 | int fd; | - | ||||||||||||
| 79 | char *buf; | - | ||||||||||||
| 80 | size_t len; | - | ||||||||||||
| 81 | { | - | ||||||||||||
| 82 | ssize_t r; | - | ||||||||||||
| 83 | int nintr; | - | ||||||||||||
| 84 | - | |||||||||||||
| 85 | for (nintr = 0; ; ) | - | ||||||||||||
| 86 | { | - | ||||||||||||
| 87 | r = read (fd, buf, len); | - | ||||||||||||
| 88 | if (r >= 0)
| 0 | ||||||||||||
| 89 | return r; never executed: return r; | 0 | ||||||||||||
| 90 | if (r == -1 && errno == EINTR)
| 0 | ||||||||||||
| 91 | { | - | ||||||||||||
| 92 | if (++nintr >= NUM_INTR)
| 0 | ||||||||||||
| 93 | return -1; never executed: return -1; | 0 | ||||||||||||
| 94 | continue; never executed: continue; | 0 | ||||||||||||
| 95 | } | - | ||||||||||||
| 96 | return r; never executed: return r; | 0 | ||||||||||||
| 97 | } | - | ||||||||||||
| 98 | } never executed: end of block | 0 | ||||||||||||
| 99 | - | |||||||||||||
| 100 | /* Call read(2) and allow it to be interrupted. Just a stub for now. */ | - | ||||||||||||
| 101 | ssize_t | - | ||||||||||||
| 102 | zreadintr (fd, buf, len) | - | ||||||||||||
| 103 | int fd; | - | ||||||||||||
| 104 | char *buf; | - | ||||||||||||
| 105 | size_t len; | - | ||||||||||||
| 106 | { | - | ||||||||||||
| 107 | check_signals (); | - | ||||||||||||
| 108 | return (read (fd, buf, len)); never executed: return (read (fd, buf, len)); | 0 | ||||||||||||
| 109 | } | - | ||||||||||||
| 110 | - | |||||||||||||
| 111 | /* Read one character from FD and return it in CP. Return values are as | - | ||||||||||||
| 112 | in read(2). This does some local buffering to avoid many one-character | - | ||||||||||||
| 113 | calls to read(2), like those the `read' builtin performs. */ | - | ||||||||||||
| 114 | - | |||||||||||||
| 115 | static char lbuf[128]; | - | ||||||||||||
| 116 | static size_t lind, lused; | - | ||||||||||||
| 117 | - | |||||||||||||
| 118 | ssize_t | - | ||||||||||||
| 119 | zreadc (fd, cp) | - | ||||||||||||
| 120 | int fd; | - | ||||||||||||
| 121 | char *cp; | - | ||||||||||||
| 122 | { | - | ||||||||||||
| 123 | ssize_t nr; | - | ||||||||||||
| 124 | - | |||||||||||||
| 125 | if (lind == lused || lused == 0)
| 0-6514 | ||||||||||||
| 126 | { | - | ||||||||||||
| 127 | nr = zread (fd, lbuf, sizeof (lbuf)); | - | ||||||||||||
| 128 | lind = 0; | - | ||||||||||||
| 129 | if (nr <= 0)
| 39-507 | ||||||||||||
| 130 | { | - | ||||||||||||
| 131 | lused = 0; | - | ||||||||||||
| 132 | return nr; executed 39 times by 1 test: return nr;Executed by:
| 39 | ||||||||||||
| 133 | } | - | ||||||||||||
| 134 | lused = nr; | - | ||||||||||||
| 135 | } executed 507 times by 1 test: end of blockExecuted by:
| 507 | ||||||||||||
| 136 | if (cp)
| 0-7021 | ||||||||||||
| 137 | *cp = lbuf[lind++]; executed 7021 times by 1 test: *cp = lbuf[lind++];Executed by:
| 7021 | ||||||||||||
| 138 | return 1; executed 7021 times by 1 test: return 1;Executed by:
| 7021 | ||||||||||||
| 139 | } | - | ||||||||||||
| 140 | - | |||||||||||||
| 141 | /* Don't mix calls to zreadc and zreadcintr in the same function, since they | - | ||||||||||||
| 142 | use the same local buffer. */ | - | ||||||||||||
| 143 | ssize_t | - | ||||||||||||
| 144 | zreadcintr (fd, cp) | - | ||||||||||||
| 145 | int fd; | - | ||||||||||||
| 146 | char *cp; | - | ||||||||||||
| 147 | { | - | ||||||||||||
| 148 | ssize_t nr; | - | ||||||||||||
| 149 | - | |||||||||||||
| 150 | if (lind == lused || lused == 0)
| 0 | ||||||||||||
| 151 | { | - | ||||||||||||
| 152 | nr = zreadintr (fd, lbuf, sizeof (lbuf)); | - | ||||||||||||
| 153 | lind = 0; | - | ||||||||||||
| 154 | if (nr <= 0)
| 0 | ||||||||||||
| 155 | { | - | ||||||||||||
| 156 | lused = 0; | - | ||||||||||||
| 157 | return nr; never executed: return nr; | 0 | ||||||||||||
| 158 | } | - | ||||||||||||
| 159 | lused = nr; | - | ||||||||||||
| 160 | } never executed: end of block | 0 | ||||||||||||
| 161 | if (cp)
| 0 | ||||||||||||
| 162 | *cp = lbuf[lind++]; never executed: *cp = lbuf[lind++]; | 0 | ||||||||||||
| 163 | return 1; never executed: return 1; | 0 | ||||||||||||
| 164 | } | - | ||||||||||||
| 165 | - | |||||||||||||
| 166 | /* Like zreadc, but read a specified number of characters at a time. Used | - | ||||||||||||
| 167 | for `read -N'. */ | - | ||||||||||||
| 168 | ssize_t | - | ||||||||||||
| 169 | zreadn (fd, cp, len) | - | ||||||||||||
| 170 | int fd; | - | ||||||||||||
| 171 | char *cp; | - | ||||||||||||
| 172 | size_t len; | - | ||||||||||||
| 173 | { | - | ||||||||||||
| 174 | ssize_t nr; | - | ||||||||||||
| 175 | - | |||||||||||||
| 176 | if (lind == lused || lused == 0)
| 0-3 | ||||||||||||
| 177 | { | - | ||||||||||||
| 178 | if (len > sizeof (lbuf))
| 0-1 | ||||||||||||
| 179 | len = sizeof (lbuf); never executed: len = sizeof (lbuf); | 0 | ||||||||||||
| 180 | nr = zread (fd, lbuf, len); | - | ||||||||||||
| 181 | lind = 0; | - | ||||||||||||
| 182 | if (nr <= 0)
| 0-1 | ||||||||||||
| 183 | { | - | ||||||||||||
| 184 | lused = 0; | - | ||||||||||||
| 185 | return nr; never executed: return nr; | 0 | ||||||||||||
| 186 | } | - | ||||||||||||
| 187 | lused = nr; | - | ||||||||||||
| 188 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||
| 189 | if (cp)
| 0-4 | ||||||||||||
| 190 | *cp = lbuf[lind++]; executed 4 times by 1 test: *cp = lbuf[lind++];Executed by:
| 4 | ||||||||||||
| 191 | return 1; executed 4 times by 1 test: return 1;Executed by:
| 4 | ||||||||||||
| 192 | } | - | ||||||||||||
| 193 | - | |||||||||||||
| 194 | void | - | ||||||||||||
| 195 | zreset () | - | ||||||||||||
| 196 | { | - | ||||||||||||
| 197 | lind = lused = 0; | - | ||||||||||||
| 198 | } executed 23 times by 1 test: end of blockExecuted by:
| 23 | ||||||||||||
| 199 | - | |||||||||||||
| 200 | /* Sync the seek pointer for FD so that the kernel's idea of the last char | - | ||||||||||||
| 201 | read is the last char returned by zreadc. */ | - | ||||||||||||
| 202 | void | - | ||||||||||||
| 203 | zsyncfd (fd) | - | ||||||||||||
| 204 | int fd; | - | ||||||||||||
| 205 | { | - | ||||||||||||
| 206 | off_t off, r; | - | ||||||||||||
| 207 | - | |||||||||||||
| 208 | off = lused - lind; | - | ||||||||||||
| 209 | r = 0; | - | ||||||||||||
| 210 | if (off > 0)
| 171-352 | ||||||||||||
| 211 | r = lseek (fd, -off, SEEK_CUR); executed 352 times by 1 test: r = lseek (fd, -off, 1 );Executed by:
| 352 | ||||||||||||
| 212 | - | |||||||||||||
| 213 | if (r != -1)
| 0-523 | ||||||||||||
| 214 | lused = lind = 0; executed 523 times by 1 test: lused = lind = 0;Executed by:
| 523 | ||||||||||||
| 215 | } executed 523 times by 1 test: end of blockExecuted by:
| 523 | ||||||||||||
| Source code | Switch to Preprocessed file |