Search code examples
carraysargv

How to properly pass file path with from the command line?


I've got a project about moving certain files from one directory to another. I have all finished except that the output is kind of strange. I am required to provide the destination path in the argsv array, but when I try to execute my code, it compiles and works but shows the wrong path containing many paths in one! Here is the relevant part, if you need more code I will add! Thank you in advance!

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

int size = NFILES;
int index = -1;
file * files = malloc(size * sizeof(file));

listFilesRecursively(argv[1], &files, &size, &index);

if (index != -1) {
      int N = atoi(argv[2]);

if(N==1) qsort(files, index + 1, sizeof(file), compPathname);
else if(N==2) qsort(files, index + 1, sizeof(file), compPathsize);

for (int i = 0; i <= index; ++i) {
        char *dest = argv[3];
                strcat(dest, "/");
                strcat(dest, files[i].justname);
  printf("%s : %s : %ld\n", files[i].name, dest , (long)   files[i].file_info.st_size);
//  if(rename(files[i].name, dest)==0) printf("Success!\n"); else     printf("Failed!/n");
}

So this is the main. The desired output is like this (I have many files):

./copyto.c : /home/nik/copyto.c : 676
Success!
./mvfilrd.c : /home/nik/mvfilrd.c : 957
Success!
./sortall.c : /home/nik/sortall.c : 992
Success!

and so on... but instead I get

./newdir/newfile.txt : /home/nik/Music/newfile.txt : 0
Success!
./newdir/3.exe : /home/nik/Music/newfile.txt/3.exe : 0
Failed!/n./newdir/compil : /home/nik/Music/newfile.txt/3.exe/test :     0
Failed!/n./newdir/2.c : /home/nik/Music/newfile.txt/3.exe/test/exe :         0 

and then even more garbage

Failed!/n./newf.exe : /home/nik/Music/newfile.txt/3.exe/test/exe    /1//Q�/~�dZ /�l�G^ /
                                                                                    ��`(/4�a^d /a.txt/range/1.txt/1.exe/print.exe/filrd.exeC/2.exre/filrd.exe/2.exe/fi.txt/fil.txt/dest.txt/sorcopy.c/filew.exe/.filer.c.swp    /progfilrd.exe/compile/myfile/.m

and the first argument seems to have crashed as well...


Solution

  • char *dest = argv[3]; 
    strcat(dest, "/"); 
    strcat(dest, files[i].justname); 
    

    ouch you modify a string you do not own, do not do that, you probably write out of the string, work on a copy

    replace

    for (int i = 0; i <= index; ++i) {
        char *dest = argv[3];
        strcat(dest, "/");
        strcat(dest, files[i].justname);
        printf("%s : %s : %ld\n", files[i].name, dest , (long)   files[i].file_info.st_size);
        if(rename(files[i].name, dest)==0) 
           printf("Success!\n");
        else
           printf("Failed!/n");
    }
    

    by

    for (int i = 0; i <= index; ++i) {
        size_t sz = strlen(argv[3]);
        char *dest = malloc(sz + strlen(files[i].justname) + 2);
    
        strcpy(dest, argv[3]);
        dest[sz] = '/';
        strcpy(dest + sz + 1, files[i].justname);
    
        printf("%s : %s : %ld\n", files[i].name, dest , (long)   files[i].file_info.st_size);
        if(rename(files[i].name, dest)==0)
          printf("Success!\n");
        else
          printf("Failed!/n");
    
        free(dest);
    }