-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_write.c
56 lines (46 loc) · 1.09 KB
/
read_write.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
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
void err_quit (const char * mesg)
{
printf ("%s\n", mesg);
exit(1);
}
void err_sys (const char * mesg)
{
perror(mesg);
exit(errno);
}
int main (int argc, char *argv[])
{
int fdin, fdout, bufsz;
char *src;
struct stat statbuf;
if (argc != 4)
err_quit ("usage: read_write <fromfile> <tofile> <buf_size>");
/* open the input file */
if ((fdin = open (argv[1], O_RDONLY)) < 0) {
char buf[256];
sprintf(buf, "can't open %s for reading", argv[1]);
perror(buf);
exit(errno);
}
/* open/create the output file */
if ((fdout = open (argv[2], O_RDWR | O_CREAT | O_TRUNC, 0644)) < 0) {
char buf[256];
sprintf (buf, "can't create %s for writing", argv[2]);
perror(buf);
exit(errno);
}
/* Allocate a buffer of the size specified */
bufsz = atoi(argv[3]);
src = malloc(bufsz);
/* And use it to copy the file */
while ((read (fdin, src, bufsz)) > 0) {
write (fdout, src, bufsz);
}
} /* main */