/* * the simplest copy ever * Usage: scopy fromfile tofile * * Matt Bishop, ECS 36A, Fall 2019 * * October 24, 2019 */ #include /* * macros */ #define BUFFERSIZE 1024 /* * the main routine */ int main(int argc, char *argv[]) { FILE *infp, *outfp; /* input, output file pointers */ char buf[BUFFERSIZE]; /* input buffer */ /* * open the files */ /* be sure there are 2 file names! */ if (argc != 3){ fprintf(stderr, "Usage: scopy fromfile tofile\n"); return(1); } /* open the input and output files and give */ /* an error message and exit on failure */ if ((infp = fopen(argv[1], "r")) == NULL){ fprintf(stderr, "%s: could not open\n", argv[1]); return(1); } if ((outfp = fopen(argv[2], "w")) == NULL){ fprintf(stderr, "%s: could not open\n", argv[2]); return(1); } /* * now copy from the source file t the destination file */ while(fgets(buf, BUFFERSIZE, infp) != NULL) if (fputs(buf, outfp) == EOF){ fprintf(stderr, "%s: write error; quitting\n", argv[2]); return(1); } /* all done -- close everything up and exit */ (void) fclose(infp); (void) fclose(outfp); return(0); }