source: vendor/bash/3.1/lib/sh/zread.c

Last change on this file was 3228, checked in by bird, 18 years ago

bash 3.1

File size: 2.7 KB
Line 
1/* Copyright (C) 1999-2002 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)
30extern 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. */
39ssize_t
40zread (fd, buf, len)
41 int fd;
42 char *buf;
43 size_t len;
44{
45 ssize_t r;
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
60ssize_t
61zreadintr (fd, buf, len)
62 int fd;
63 char *buf;
64 size_t len;
65{
66 ssize_t r;
67 int nintr;
68
69 for (nintr = 0; ; )
70 {
71 r = read (fd, buf, len);
72 if (r >= 0)
73 return r;
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
88static char lbuf[128];
89static size_t lind, lused;
90
91ssize_t
92zreadc (fd, cp)
93 int fd;
94 char *cp;
95{
96 ssize_t nr;
97
98 if (lind == lused || lused == 0)
99 {
100 nr = zread (fd, lbuf, sizeof (lbuf));
101 lind = 0;
102 if (nr <= 0)
103 {
104 lused = 0;
105 return nr;
106 }
107 lused = nr;
108 }
109 if (cp)
110 *cp = lbuf[lind++];
111 return 1;
112}
113
114void
115zreset ()
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. */
122void
123zsyncfd (fd)
124 int fd;
125{
126 off_t off;
127 int r;
128
129 off = lused - lind;
130 r = 0;
131 if (off > 0)
132 r = lseek (fd, -off, SEEK_CUR);
133
134 if (r >= 0)
135 lused = lind = 0;
136}
Note: See TracBrowser for help on using the repository browser.