Skip to content

Commit 6144cfd

Browse files
committed
chapter 8 sample code to 8.5
1 parent 583fe84 commit 6144cfd

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <stdio.h>
2+
#include <fcntl.h>
3+
#include <unistd.h>
4+
#include <stdarg.h>
5+
#include <stdlib.h>
6+
7+
#define PERMS 0666
8+
9+
void error(char *fmt, ...);
10+
11+
main(int argc, char *argv[])
12+
{
13+
int f1, f2, n;
14+
char buf[BUFSIZ];
15+
16+
if (argc != 3)
17+
error("usage: cp from to");
18+
if ((f1 = open(argv[1], O_RDONLY, 0)) == -1)
19+
error("cp: can't open %s", argv[1]);
20+
if ((f2 = open(argv[2], PERMS, 0)) == -1)
21+
error("cp: can't create %s, mode %03o", argv[2], PERMS);
22+
23+
while ((n = read(f1, buf, BUFSIZ)) > 0)
24+
if (write(f2, buf, n) != n)
25+
error("cp: write error on file %s", argv[2]);
26+
27+
return 0;
28+
}
29+
30+
void error(char *fmt, ...)
31+
{
32+
va_list args;
33+
34+
va_start(args, fmt);
35+
fprintf(stderr, "error: ");
36+
vfprintf(stderr, fmt, args);
37+
fprintf(stderr, "\n");
38+
va_end(args);
39+
exit(1);
40+
}
41+
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#include <fcntl.h>
2+
#include <unistd.h>
3+
#include <stdlib.h>
4+
#include <assert.h>
5+
6+
//#define NULL 0
7+
#define EOF (-1)
8+
#define BUFSIZ 1024
9+
#define OPEN_MAX 20
10+
11+
#define PERMS 0666
12+
13+
typedef struct _iobuf {
14+
int cnt;
15+
char *ptr;
16+
char *base;
17+
int flag;
18+
int fd;
19+
} FILE;
20+
21+
FILE _iob[OPEN_MAX];
22+
23+
enum _flags {
24+
_READ = 01,
25+
_WRITE = 02,
26+
_UNBUF = 04,
27+
_EOF = 010,
28+
_ERR = 020
29+
};
30+
31+
FILE *fopen(char *name, char *mode);
32+
int _fillbuf(FILE *fp);
33+
34+
int main()
35+
{
36+
FILE *fp = fopen("fopen.c", "rw");
37+
assert(fp != NULL);
38+
return 0;
39+
}
40+
41+
FILE *fopen(char *name, char *mode)
42+
{
43+
int fd;
44+
FILE *fp;
45+
46+
if (*mode != 'r' && *mode != 'w' && *mode != 'a')
47+
return NULL;
48+
for (fp = _iob; fp < _iob + OPEN_MAX; fp++)
49+
if ((fp->flag & (_READ | _WRITE)) == 0)
50+
break;
51+
if (fp >= _iob + OPEN_MAX)
52+
return NULL;
53+
54+
if (*mode == 'w')
55+
fd = creat(name, PERMS);
56+
else if (*mode == 'a') {
57+
if ((fd = open(name, O_WRONLY, 0)) == -1)
58+
fd = creat(name, PERMS);
59+
lseek(fd, 0L, 2);
60+
} else
61+
fd = open(name, O_RDONLY, 0);
62+
if (fd == -1)
63+
return NULL;
64+
fp->fd = fd;
65+
fp->cnt = 0;
66+
fp->base = NULL;
67+
fp->flag = (*mode == 'r') ? _READ : _WRITE;
68+
return fp;
69+
}
70+
71+
int _fillbuf(FILE *fp)
72+
{
73+
int bufsize;
74+
75+
if ((fp->flag & (_READ | EOF | _ERR)) != _READ)
76+
return EOF;
77+
bufsize = (fp->flag & _UNBUF) ? 1 : BUFSIZ;
78+
if (fp->base == NULL)
79+
if ((fp->base = (char *) malloc(bufsize)) == NULL)
80+
return EOF;
81+
fp->ptr = fp->base;
82+
fp->cnt = read(fp->fd, fp->ptr, bufsize);
83+
if (--fp->cnt < 0) {
84+
if (fp->cnt == -1)
85+
fp->flag |= _EOF;
86+
else
87+
fp->flag |= _ERR;
88+
fp->cnt = 0;
89+
return EOF;
90+
}
91+
return (unsigned char) *fp->ptr++;
92+
}
93+
94+
FILE _iob[OPEN_MAX] = {
95+
{ 0, (char *) 0, (char *) 0, _READ, 0 },
96+
{ 0, (char *) 0, (char *) 0, _WRITE, 1 },
97+
{ 0, (char *) 0, (char *) 0, _WRITE | _UNBUF, 2 },
98+
};
99+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <unistd.h>
2+
#include <stdio.h>
3+
4+
main()
5+
{
6+
char buf[BUFSIZ];
7+
int n;
8+
9+
while ((n = read(0, buf, BUFSIZ)) > 0)
10+
write(1, buf, n);
11+
return 0;
12+
}
13+

0 commit comments

Comments
 (0)