Hi so im trying to build a version of the linux cp command in C,that can handle the following test cases
- cpy f1 f2 - copies content of f1 to f2
- cpy f d - copies f to d/f
- cpy d1/d2/d3/f1 f2 - copies content of f1 to f2
- cpy d1/d2/d3/f d - copies f to d/f
- cpy d1/d2/d3/f d4/d5/d6/f d - copies files to directory d
I'm already given the code for the Copyfile function and all im trying to do is create my main function to call Copyfile and verify that the test cases are handled correctly. Here is the CopyFile function and the main function. Am i on the right track? I havent been able to test because I currently have errors when compiling ( error:confilcting types for Copyfile)and i cant seem to figure out whats wrong.
int copyFiles(int argC, char* argV[]){
int srcFd;
int dstFd;
int charCnt;
int buffersize = strtol(argV[3], NULL, 10);
char buf[buffersize];
/*Check args*/
if( argC!=4 ){
fprintf( stderr, "usage: %s source destination\n", argV[0]);
exit(1);
}
/*Open the files*/
srcFd= open(argV[1],O_RDONLY);
if( srcFd==-1 ){
errExit("Cannot open ", argV[1]);
}
dstFd= creat(argV[2],COPYMODE);
if( dstFd==-1 ){
errExit( "Cannot create ", argV[2]);
}
/*Copy the data*/
while( (charCnt= read(srcFd,buf,buffersize)) > 0 ){
if( write(dstFd,buf,charCnt ) != charCnt ){
errExit("Write error to ", argV[2]);
}
}
if( charCnt==-1 ){
errExit("Read error from ", argV[1]);
}
/*Close files*/
if ( close(srcFd) == -1 || close(dstFd) == -1 ){
errExit("Error closing files","");
}
}
int copyFiles(char *srcFd,char *dstFd);
int main(int argC, char* argV[])
{
char *srcFd = argV[1];
char *dstFd = argV[2];
if( srcFd[0] != '/' && dstFd[0] != '/' )//cpy f1 f2
{
copyFiles(srcFd, dstFd);
}
else if( srcFd[0] != '/' && dstFd[0] == '/' )//cpy f1 /d
{
int i;
for(i=1; i<=strlen(dstFd); i++)
{
dstFd[(i-1)] = dstFd[i];
}
strcat(dstFd, "/");
strcat(dstFd, srcFd);
copyFiles(srcFd, dstFd);
}
else
{
fprintf(stderr, "usage: cp1 source destination\n");
exit(1);
}
}