-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy path8-4-linebuf.c
270 lines (234 loc) · 6.86 KB
/
8-4-linebuf.c
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
/*
* Exercise 8-4. The standard library function
*
* int fseek(FILE *fp, long offset, int origin)
*
* is identical to lseek except that fp is a file pointer instead of a file
* descriptor and the return value is an int status, not a position. Write
* fseek. Make sure that your fseek coordinates properly with the buffering
* done for the other functions of the library.
*
* Note: this is a quick and dirty implementation of line buffing for stdout.
* By Faisal Saadatmand
*/
#define EOF (-1)
#define BUFSIZ 1024
#define OPEN_MAX 20 /* max #files open at once */
#define NEWLINE '\n'
typedef struct _iobuf {
int cnt; /* characters left */
char *ptr; /* next character position */
char *base; /* location of buffer */
int flag; /* mode of file access */
int fd; /* file descriptor */
} FILE;
extern FILE _iob[OPEN_MAX];
#define stdin (&_iob[0])
#define stdout (&_iob[1])
#define stderr (&_iob[2])
enum _flags {
_READ = 01, /* file open for reading */
_WRITE = 02, /* file open for writing */
_UNBUF = 04, /* file is unbuffered */
_EOF = 010, /* EOF has occurred on this file */
_ERR = 020, /* error occurred on this file */
_LNBUF = 040, /* file is line buffered */
};
/* functions */
FILE *_fopen(char *, char *);
int _fillbuf(FILE *);
int _flushbuf(int, FILE *);
int fflush(FILE *);
int fclose (FILE *);
int fseek(FILE *, long, int);
#define feof(p) (((p)->flag & _EOF) != 0)
#define ferror(p) (((p)->flag & _ERR) != 0)
#define fileno(p) ((p)->fd)
#define linebuf(p) (((p)->flag & _LNBUF) !=0)
#define unbuf(p) (((p)->flag & _UNBUF) !=0)
#define getc(p) (--(p)->cnt >= 0 \
? (unsigned char) *(p)->ptr++ : _fillbuf(p))
#define putc(x,p) (--(p)->cnt >= 0 \
? (linebuf((p)) \
? ((x) == NEWLINE \
? _flushbuf((x),(p)) : (*(p)->ptr++ = (x))) \
: (*(p)->ptr++ = (x))) : _flushbuf((x),(p)))
#define getchar() getc(stdin)
#define putchar(x) putc((x), stdout)
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h> /* for malloc */
#define PERMS 0666 /* RW for owner, group, others */
/* _fopen: open file, return ptr */
FILE *_fopen(char *name, char *mode)
{
int fd;
FILE *fp;
if (*mode != 'r' && *mode != 'w' && *mode != 'a')
return NULL;
for (fp = _iob; fp < _iob + OPEN_MAX; fp++)
if ((fp->flag & (_READ | _WRITE)) == 0)
break; /* found free slot */
if ( fp >= _iob + OPEN_MAX) /* no free slots */
return NULL;
if (*mode == 'w')
fd = creat(name, PERMS);
else if (*mode == 'a') {
if ((fd = open(name, O_WRONLY, 0)) == -1)
fd = creat(name, PERMS);
lseek(fd, 0L, 2);
} else
fd = open(name, O_RDONLY, 0);
if (fd == -1)
return NULL; /* couldn't access name */
fp->fd = fd;
fp->cnt = 0;
fp->base = NULL;
fp->flag = (*mode == 'r') ? _READ : _WRITE;
return fp;
}
/* _fillbuf: allocate and fill input buffer */
int _fillbuf(FILE *fp)
{
int bufsize;
if ((fp->flag & (_READ | _EOF | _ERR)) != _READ)
return EOF;
bufsize = (fp->flag & _UNBUF) ? 1 : BUFSIZ;
if (fp->base == NULL) /* no buffer yet */
if ((fp->base = (char *) malloc(bufsize)) == NULL)
return EOF; /* can't get buffer */
fp->ptr = fp->base;
fp->cnt = read(fileno(fp), fp->ptr, bufsize);
if (--fp->cnt < 0) {
if (fp->cnt == -1)
fp->flag |= _EOF;
else
fp->flag |= _ERR;
fp->cnt = 0;
return EOF;
}
return (unsigned char) *fp->ptr++;
}
/* _flushbuf: allocate and flush output buffer */
int _flushbuf(int x, FILE *fp)
{
int bufsize;
int n;
if ((fp->flag & (_WRITE | _ERR)) != _WRITE)
return EOF;
if (fp->flag & _UNBUF)
bufsize = 1;
else if (fp->flag & _EOF) /* flush written data only (fflush) */
bufsize = BUFSIZ - fp->cnt;
else
bufsize = BUFSIZ;
if (fp->base == NULL) { /* no buffer yet */
if ((fp->base = (char *) malloc(bufsize)) == NULL)
return EOF; /* can't get buffer */
*fp->base = '\0'; /* initialize string */
}
if (linebuf(fp) && x == NEWLINE) /* line buffered output */
if ((bufsize = BUFSIZ - fp->cnt) != 1)
*fp->ptr = x;
fp->ptr = fp->base;
if (bufsize == 1) /* unbuffered output */
*fp->ptr = x;
if (*fp->ptr != '\0' || bufsize == 1) {
n = write(fileno(fp), fp->ptr, bufsize);
if (n != bufsize) {
fp->flag |= _ERR;
fp->cnt = 0;
return EOF;
}
}
if (linebuf(fp) && x == NEWLINE) {
fp->cnt = BUFSIZ; /* rest buffering */
return (unsigned char) *fp->ptr;
} else if (bufsize != 1) {
*fp->ptr = x;
fp->cnt = BUFSIZ - 1;
}
return (unsigned char) *fp->ptr++;
}
/* fflush: on output stream, write unwritten buffered data. On input stream,
* the effect is undefined. NULL flushes all output streams. */
int fflush(FILE *fp)
{
FILE *cond; /* loop condition */
if (unbuf(fp) || linebuf(fp))
return 0;
if (fp == NULL) { /* flush all output stream */
fp = _iob;
cond = _iob + OPEN_MAX;
} else /* flush fp's buffer */
cond = fp + 1;
for ( ; fp < cond; fp++) {
if ((fp->flag & (_WRITE | _EOF | _ERR)) != _WRITE)
return EOF;
fp->flag |= _EOF; /* singal EOF to _flushbuf */
if (_flushbuf(*fp->ptr, fp) < 0)
return EOF;
*fp->ptr = '\0';
fp->cnt = 0;
}
return 0;
}
/* fclose: flushes unwritten date from stream, discard unread buffered input,
* frees allocated memory, and closes stream. */
int fclose (FILE *fp)
{
if ((fp->flag & (_WRITE | _EOF | _ERR)) == _WRITE)
if (!(fp->flag & _UNBUF) || !linebuf(fp))
if (fflush(fp) < 0)
return EOF;
free(fp->base);
if (close(fileno(fp)) < 0) {
fp->flag |= _ERR;
return EOF;
}
return 0;
}
/* fseek: sets the file position indicator for fp */
int fseek(FILE *fp, long offset, int origin)
{
if ((fp->flag & _READ) == 0)
return -1;
if (lseek(fileno(fp), offset, origin) < 0) {
fp->flag |= _ERR;
return -1;
}
fp->ptr = fp->base; /* reset buffer */
*fp->ptr = '\0';
fp->cnt = 0;
fp->flag &= ~(_EOF);
return 0;
}
FILE _iob[OPEN_MAX] = { /* stdin, stdout, stderr */
{ 0, (char *) 0, (char *) 0, _READ, 0 },
{ 0, (char *) 0, (char *) 0, _WRITE | _LNBUF, 1 },
{ 0, (char *) 0, (char *) 0, _WRITE | _UNBUF, 2 }
};
int main(int argc, char *argv[])
{
FILE *fp;
int c;
if (argc == 1)
while ((c = getchar()) != EOF)
putchar(c);
else
while (--argc > 0)
if ((fp = _fopen(*++argv, "r")) == NULL)
exit(EXIT_FAILURE);
else {
while ((c = getc(fp)) != EOF)
putchar(c);
// fseek(fp, -5L, 1);
// while ((c = getc(fp)) != EOF)
// putchar(c);
fclose(fp);
}
if (ferror(stdout))
exit(EXIT_FAILURE);
fclose(stdout);
exit(0);
}