Rewrite the program cat from Chapter 7 using read , write , open and close instead of their standard library equivalents. Perform experiments to determine the relative speeds of the two versions.
Solution by Andrew Tesker
/*
Andrew Tesker
ucat.c
a version of cat using UNIX system access
*/
#include <stdio.h>
#include <fcntl.h>
#define BUFSIZE 1024
int main(int argc, char *argv[])
{
int fd1;
void filecopy(int f, int t);
if(argc == 1)
filecopy(0, 1);
else {
while(--argc > 0)
if(( fd1 = open(*++argv, O_RDONLY, 0)) == -1) {
printf("unix cat: can't open %s\n", *argv);
return 1;
}
else {
filecopy(fd1, 1);
close(fd1);
}
}
return 0;
}
void filecopy(int from, int to)
{
int n;
char buf[BUFSIZE];
while((n=read(from, buf, BUFSIZE)) > 0 )
write(to, buf, n);
}
'The C Programming Language' 카테고리의 다른 글
Chapter 8 - The UNIX System Interface 3 (0) | 2017.12.22 |
---|---|
Chapter 8 - The UNIX System Interface 2 (0) | 2017.12.22 |
Chapter 7 - Input and Output 9 (0) | 2017.12.22 |
Chapter 7 - Input and Output 8 (0) | 2017.12.22 |
Chapter 7 - Input and Output 7 (0) | 2017.12.22 |