logoISU  

CS 456 - Systems Programming

Spring 2024

Displaying ./code/jan24/mycopy.c

// a simple implentation of the cp command

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv){

	//check if two files were provided , otherwise print usage statement and exit
	if(argc < 3){
		fprintf(stderr, "Usage: %s source destination\n", argv[0]);
		exit(1);
	}	

	//read in a file from the command line
	
	FILE *src = fopen(argv[1], "r");
	FILE *dst = fopen(argv[2], "w");

	if (src == NULL){
		fprintf(stderr, "File %s could not be opened\n", argv[1]);
		exit(1);
	}	

	int c;

	//counting each character
	while((c =fgetc(src))!= EOF){
		fputc(c, dst);
	}	

	
	fclose(src);
	fclose(dst);

	//print the count and exit
	//printf("Characters in %s: %d\n", argv[1], count);
	return 0;

}