/* cat.c */
/* demostrate: fopen, fclose, getc, putc, stdin, stdout */

#include <stdio.h>

void filecopy(FILE * ifp, FILE * ofp);

/* cat: concatenate files - into standard output */
int main(int argc, char * argv[])
{
  FILE *fp;

  if (argc == 1) {      /* no argc - copy from standard input */
    filecopy(stdin, stdout);
    return 0;
  }

  while (--argc > 0) {
    if ((fp = fopen (*++argv, "r")) == NULL) {
      printf ("cat: can't open %s\n", *argv);
      return 1;
    }
    filecopy(fp, stdout);
    fclose(fp);
  }
  return 0;
}


/* filecopy: copy file ifp to file ofp */
void filecopy (FILE * ifp, FILE * ofp)
{
  int c;

  while ((c = getc (ifp)) != EOF)
    putc (c, ofp);
}
