|
CS456 - Systems Programming
Spring 2025
|
Displaying ./code/h1/copy.c
/*
implementing a version of the "cp" command
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv){
if(argc < 3){ //prints usage statement if not enough arguments
fprintf(stderr,"Usage: %s <src> <dst>\n", argv[0]);
exit(1);
}
//declares file pointer, tries to open file specified by user
FILE *src, *dst;
src = fopen(argv[1], "r"); //source file
dst = fopen(argv[2], "w"); //destination
//if file does not exist, print usage statement and exit the program
if(src == NULL){
fprintf(stderr, "ERROR: Cannot open %s, file probably does not exist.\n", argv[1]);
exit(-1);:
}
// if we get here, we then read every character in the source file,
// and then write the character to the destination file
char c;
while((c=fgetc(src))){
if(c == EOF) break; //we've reached the end of file, break the loop
fputc(c, dst); //writing the character to the destination file.
}
fclose(src); //close the source file
fclose(dst); //close the destination file
return 0;
}
|