Exercise 8.1. 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.
#include <stdio.h>
#include <fcntl.h>
#include "syscalls.h"
/* cat */
int main(int argc, char *argv[])
{
int fp;
void filecopy(int ifp, int ofp);
if(argc == 1) //no arguments, default stdin
filecopy(0, 1);
else
while(--argc > 0)
if((fp = open(*++argv, 0_RDONLY)) == -1)
error("cat: can`t open the file %s\n", *argv);
else
{
filecopy(fp, 1);
close(fp);
}
return 0;
}
/* filecopy: copy file ifp into file ofp */
void filecopy(int ifp, int ofp)
{
int n;
char buf[BUFSIZ];
while((n = read(ifp, buf, BUFSIZ)) > 0)
if(write(ofp, buf, n) != n)
error("cat:write error");
}
Комментариев нет:
Отправить комментарий