-4

Hi so im trying to build a version of the linux cp command in C,that can handle the following test cases

  1. cpy f1 f2 - copies content of f1 to f2
  2. cpy f d - copies f to d/f
  3. cpy d1/d2/d3/f1 f2 - copies content of f1 to f2
  4. cpy d1/d2/d3/f d - copies f to d/f
  5. 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);
  }
}
Kimeru
  • 21
  • 1
  • 3
  • The prototype `int copyFiles(char *srcFd,char *dstFd);` does not match the function signature `int copyFiles(int argC, char* argV[]){...}`. They must be the same. In fact, you do not need the prototype at all, simply remove it. – DYZ May 19 '17 at 03:04
  • In the test cases 1-5, are the directories absolute path or relative path? In the test cases, it appears to be relative paths. But the code has parsing for absolute paths. – Nguai al May 19 '17 at 03:52

1 Answers1

1

You have defined the function with arguments as int and char* as int copyFiles(int argC, char* argV[])

but declaring in main program as char* and char* int copyFiles(char *srcFd,char *dstFd);

Which C don't accepts and thus conflicting. that`s why it is showing the error.

Krishnom
  • 1,348
  • 12
  • 39