File: zread.c

package info (click to toggle)
bash 5.3-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 43,860 kB
  • sloc: ansic: 134,738; sh: 8,866; yacc: 5,966; makefile: 4,697; perl: 4,105; asm: 48; awk: 23; sed: 16
file content (308 lines) | stat: -rw-r--r-- 6,491 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* zread - read data from file descriptor into buffer with retries */

/* Copyright (C) 1999-2024 Free Software Foundation, Inc.

   This file is part of GNU Bash, the Bourne Again SHell.

   Bash is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   Bash is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <config.h>

#include <sys/types.h>

#if defined (HAVE_UNISTD_H)
#  include <unistd.h>
#endif

#include <signal.h>
#include <errno.h>

#if !defined (errno)
extern int errno;
#endif

#ifndef SEEK_CUR
#  define SEEK_CUR 1
#endif

#ifndef ZBUFSIZ
#  define ZBUFSIZ 4096
#endif

#ifndef EOF
#  define EOF -1
#endif

extern int executing_builtin, interrupt_state;

extern void check_signals_and_traps (void);
extern void check_signals (void);
extern int signal_is_trapped (int);
extern int read_builtin_timeout (int);

/* Forward declarations */
void zreset (void);

int zungetc (int);

/* Provide 16 bytes of pushback whether we are using read or zread. Only used
   by the read builtin when reading invalid multibyte characters. */
#define ZPUSHSIZE 16

static size_t zpushind, zpopind;
static unsigned char zpushbuf[ZPUSHSIZE];
static unsigned char zbufchar;

static inline int
zbufpop(unsigned char *cp)
{
  if (zpushind == zpopind)
    return (0);
  *cp = zpushbuf[zpopind++];
  if (zpopind == zpushind)
    zpopind = zpushind = 0;	/* reset, buffer empty */
  return 1;
}

static inline int
zbufpush(int c)
{
  if (zpushind == ZPUSHSIZE - 1)
    return 0;
  zpushbuf[zpushind++] = c;
  return 1;
}

/* Add C to the pushback buffer. Can't push back EOF */
int
zungetc (int c)
{
  zbufpush (c);
  return c;
}

/* Read LEN bytes from FD into BUF.  Retry the read on EINTR.  Any other
   error causes the loop to break. */
ssize_t
zread (int fd, char *buf, size_t len)
{
  ssize_t r;

  check_signals ();	/* check for signals before a blocking read */

  /* If we pushed chars back, return the oldest one immediately */
  if (zbufpop (&zbufchar))
    {
      *buf = zbufchar;
      return 1;
    }

  /* should generalize into a mechanism where different parts of the shell can
     `register' timeouts and have them checked here. */
  while (((r = read_builtin_timeout (fd)) < 0 || (r = read (fd, buf, len)) < 0) &&
	     errno == EINTR)
    {
      int t;
      t = errno;
      /* XXX - bash-5.0 */
      /* We check executing_builtin and run traps here for backwards compatibility */
      if (executing_builtin)
	{
	  if (interrupt_state)
	    zreset ();
	  check_signals_and_traps ();	/* XXX - should it be check_signals()? */
	}
      else
	check_signals ();
      errno = t;
    }

  return r;
}

/* Read LEN bytes from FD into BUF.  Retry the read on EINTR, up to three
   interrupts.  Any other error causes the loop to break. */

#ifdef NUM_INTR
#  undef NUM_INTR
#endif
#define NUM_INTR 3

ssize_t
zreadretry (int fd, char *buf, size_t len)
{
  ssize_t r;
  int nintr;

  /* If we pushed chars back, return the oldest one immediately */
  if (zbufpop (&zbufchar))
    {
      *buf = zbufchar;
      return 1;
    }

  for (nintr = 0; ; )
    {
      r = read (fd, buf, len);
      if (r >= 0)
	return r;
      if (r == -1 && errno == EINTR)
	{
	  if (++nintr >= NUM_INTR)
	    return -1;
	  continue;
	}
      return r;
    }
}

/* Call read(2) and allow it to be interrupted.  Just a stub for now. */
ssize_t
zreadintr (int fd, char *buf, size_t len)
{
  check_signals ();

  /* If we pushed chars back, return the oldest one immediately */
  if (zbufpop (&zbufchar))  
    {    
      *buf = zbufchar;        
      return 1;              
    }                            
        
  return (read (fd, buf, len));
}

/* Read one character from FD and return it in CP.  Return values are as
   in read(2).  This does some local buffering to avoid many one-character
   calls to read(2), like those the `read' builtin performs. */

static char lbuf[ZBUFSIZ];
static size_t lind, lused;

ssize_t
zreadc (int fd, char *cp)
{
  ssize_t nr;

  /* If we pushed chars back, return the oldest one immediately */
  if (cp && zbufpop (&zbufchar))  
    {    
      *cp = zbufchar;        
      return 1;              
    }                            
        
  if (lind == lused || lused == 0)
    {
      nr = zread (fd, lbuf, sizeof (lbuf));
      lind = 0;
      if (nr <= 0)
	{
	  lused = 0;
	  return nr;
	}
      lused = nr;
    }
  if (cp)
    *cp = lbuf[lind++];
  return 1;
}

/* Don't mix calls to zreadc and zreadcintr in the same function, since they
   use the same local buffer. */
ssize_t
zreadcintr (int fd, char *cp)
{
  ssize_t nr;

  /* If we pushed chars back, return the oldest one immediately */
  if (cp && zbufpop (&zbufchar))  
    {    
      *cp = zbufchar;        
      return 1;              
    }

  if (lind == lused || lused == 0)
    {
      nr = zreadintr (fd, lbuf, sizeof (lbuf));
      lind = 0;
      if (nr <= 0)
	{
	  lused = 0;
	  return nr;
	}
      lused = nr;
    }
  if (cp)
    *cp = lbuf[lind++];
  return 1;
}

/* Like zreadc, but read a specified number of characters at a time.  Used
   for `read -N'. */
ssize_t
zreadn (int fd, char *cp, size_t len)
{
  ssize_t nr;

  /* If we pushed chars back, return the oldest one immediately */
  if (cp && zbufpop (&zbufchar))  
    {    
      *cp = zbufchar;        
      return 1;              
    }

  if (lind == lused || lused == 0)
    {
      if (len > sizeof (lbuf))
	len = sizeof (lbuf);
      nr = zread (fd, lbuf, len);
      lind = 0;
      if (nr <= 0)
	{
	  lused = 0;
	  return nr;
	}
      lused = nr;
    }
  if (cp)
    *cp = lbuf[lind++];
  return 1;
}

void
zreset (void)
{
  lind = lused = 0;
  zpushind = zpopind = 0;
}

/* Sync the seek pointer for FD so that the kernel's idea of the last char
   read is the last char returned by zreadc. */
void
zsyncfd (int fd)
{
  off_t off, r;

  off = lused - lind;
  r = 0;
  if (off > 0)
    r = lseek (fd, -off, SEEK_CUR);

  if (r != -1)
    {
      lused = lind = 0;
      zpushind = zpopind = 0;
    }
}