"The C Programming Language", 2nd edition, Kernighan and Ritchie

Answer to Exercise 8-1, page 174

Solution by Andrew Tesker


Ron Scott has also sent me a solution to this exercise. Once he has granted me permission to display it here, I will post it on this site.
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.

/*
  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);
}


Back to index





You are visitor number - call again soon!