0

While traversing from my current path , and searching for a file , I'm having problems locating the file .

I suspect the that the path is the blame for that due to chdir , in the next code :

The associated folders are :

  • 12,13,14 , 12 is father directory
  • 13 inside 12 , 14 inside 13
  • david1.txt inside 14
  • get_current_dir_name() is a Linux function , not mine

I run the executable file from Desktop , like this (here are printf-s for trying to locating the problem) :

    // doesn't work 

   a@ubuntu:~/Desktop$ ./exer4 12 david1.txt

    Current path is :12

    Current path is :/home/a/Desktop/12/13

    Current path is :/home/a/Desktop/12/13/14

//////////////////////////////////////////////////
    // doesn't work      

    a@ubuntu:~/Desktop$ ./exer4 12/13 david1.txt

    Current path is :12/13

    Current path is :/home/a/Desktop/12/13/14

////////////////////////////////////////////////
    // doesn't work 

    a@ubuntu:~/Desktop$ ./exer4 12/13/14 david1.txt

    Current path is :12/13/14

If I remove these lines :

chdir(path);                          // the problem is probably here 
strcpy(path, get_current_dir_name()); // and here 

Then every query that involves only a single library name - works , for example :

// works : 

a@ubuntu:~/Desktop$ ./exer4 12 david1.txt

Current path is :12

Current path is :12/13

File found!

12/13/14/david1.txt-rw-rw-r-- 1 1000 1000 0 2012-06-17 08:10 

Current path is :12/13/14
////////////////////////////////////////////////////////////
// works 
a@ubuntu:~/Desktop$ ./exer4 12/13 david1.txt

Current path is :12/13

File found!

12/13/14/david1.txt-rw-rw-r-- 1 1000 1000 0 2012-06-17 08:10 

Current path is :12/13/14

////////////////////////////////////////////////////////////
// Doesn't work
a@ubuntu:~/Desktop$ ./exer4 12/13/14 david1.txt

Current path is :12/13/14

Any idea what I'm doing wrong here ? I've tried to change countless times but nothing fixed it . Thanks

JAN
  • 21,236
  • 66
  • 181
  • 318

3 Answers3

2

When doing strcpy(path, get_current_dir_name()), you don't check that the buffer isn't exceeded.
If path is argv[1], you're overrunning the stack.
My guess is that you overrun the file name (argv[2]), and then you start looking for the wrong name (and don't find it).

ugoren
  • 16,023
  • 3
  • 35
  • 65
2

Add some my debugging information to help you know the reason clearly.

Before running strcpy(path, get_current_dir_name());

 Breakpoint 1, traverseDirectories (path=0xbfbfeda8 "src", recursive=1, filename=0xbfbfedac "2.db") at main.c:30
    30               chdir(path);

0xbfbfeda8:     0x73    0x72    0x63    0x00    0x32    0x2e    0x64    0x62
0xbfbfedb0:     0x00    0x54    0x45    0x52    0x4d    0x3d    0x78    0x74
0xbfbfedb8:     0x65    0x72    0x6d    0x00    0x53    0x48    0x45    0x4c
0xbfbfedc0:     0x4c    0x3d    0x2f    0x75    0x73    0x72    0x2f    0x6c

After running strcpy(path, get_current_dir_name());

0xbfbfeda8:     0x2f    0x75    0x73    0x72    0x2f    0x68    0x6f    0x6d
0xbfbfedb0:     0x65    0x2f    0x6e    0x65    0x6f    0x2f    0x73    0x72
0xbfbfedb8:     0x63    0x00    0x6d    0x00    0x53    0x48    0x45    0x4c
0xbfbfedc0:     0x4c    0x3d    0x2f    0x75    0x73    0x72    0x2f    0x6c

See the difference?It is why your function not works correctly.

MYMNeo
  • 818
  • 5
  • 9
1

I think the problem is this line:

if (S_ISDIR(fstat.st_mode))

you first check the fstat.st_mode whether it is directory, then find the your target file. If you use function chdir(), when the directory chang into 12/13/14, it will not make the condition false, displayAllFiles(full_name , filename) will not be called.

MYMNeo
  • 818
  • 5
  • 9
  • So if I'll move the call for `dhdir()` into `main` and only then call the recursion , you it would do ? I'll try that now – JAN Jun 18 '12 at 12:03
  • @ron, I have write a similar function to find what causing the problem, and I find why.Just as ugoren says, you have a stackoverruning, the first time you run traverseDirectories, the argv[1]'s size is determined, its size is the length of argv[1], and then you run strcpy(), it overrunning the content of argv[2] – MYMNeo Jun 18 '12 at 13:13
  • I did something else now , from main : ` char path[4096]; char filename[4096]; strcpy(path,argv[1]); // copy the folder to search strcpy(filename,argv[2]); // copy the file to search` – JAN Jun 18 '12 at 13:21
  • I want to see him overflowing/running to me now :) – JAN Jun 18 '12 at 13:31
  • 1
    @ron, it is easy, because you using strcpy, to avoid unexpected situation, you'd better to use strncpy.And you'd better to give a tick to ugoren, he help you find the problem first. – MYMNeo Jun 18 '12 at 13:37