/* * cat.c * * Read several file names from the command line. * Concatenate these files and print the result to the standard output. */ #include // Copy the content of one file to another; assume that the files are open. // Return the number of characters copied int filecopy(FILE *in, FILE *out) { int c, nc=0; while ( (c = getc(in)) != EOF) { putc (c, out); nc++; } fprintf(stderr, "%d characters copied\n", nc); return nc; } int main(int argc, char *argv[]) { FILE *in; int i, nchar = 0; for (i=1; i< argc; i++) { in = fopen(argv[i], "r"); if (in == NULL) { fprintf(stderr, "Could not open file %s\n", argv[i]); return -1; } else { nchar += filecopy(in, stdout); fclose (in); } } fprintf (stderr, "%d total characters copied\n", nchar); return 0; }