1

I wrote my own find() function. When I do:

./myown $HOME/Documents test.txt

I get:

/Users/CJ/Documents/test/test.txt
/Users/CJ/Documents/test/test1/test.txt
/Users/CJ/Documents/test/test2/test.txt

However when I do:

./myown $HOME test.txt

I get:

getcwd error

My code for getcwd is here and it is in a helper function:

findit(*directory, *pattern){
    ...
    if(getcwd(cwd, 2048+1) == NULL){ 
        fprintf(stderr, "getcwd error\n");
        exit(1);
    }
    ...
}

How can I solve this issue?

EDIT: closedir() solve this issue but there is another issue now. Result is the same when I do : ./myown $HOME/Documents test.txt but when I do the other way I get : stat error

` 
struct stat mode;
if(stat(entry->d_name, &mode) == -1){ 
        fprintf(stderr, "stat error\n");
        continue;
 }`

I didn't use stat error anywhere else in the code.

This can be helpful too, this is how I used open

DIR *dir dir = opendir(".");

The error is in readdir().

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
C.J
  • 169
  • 1
  • 12
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/174064/discussion-on-question-by-c-j-getcwd-error-when-home). –  Jun 29 '18 at 23:42
  • Even though there's an answer, the question still severely needs to be edited. – o11c Jun 30 '18 at 00:10
  • without an [mcve] how do you expect us to determine the root cause of the problem in your code? – user3629249 Jul 02 '18 at 03:16
  • @user3629249 this problem is solved – C.J Jul 02 '18 at 19:40

1 Answers1

2

One suggested step in debugging was:

Since getcwd() sets errno when it fails, you should probably report errno, maybe with perror("getcwd"). Although I'm not keen on perror(), it is probably simplest here.

It turns out that the error set was EMFILE Too many open files.

So, now you know what the trouble is. The getcwd() is failing because you have opened a lot of files and not closed enough of them, and it needs some available file descriptors but you've not left it any that it can use.

And, when requested, I elaborated on that with:

You've opened files and/or directories (opening a directory with opendir() usually uses a file descriptor), and you've not closed them. Consequently, the system won't allow you to open any more files — and the getcwd() fails. It probably isn't immediate; your program has probably done some processing before that failure.

The OP observed:

I just saw that I haven't used fclose; give me a second and I will check it if that's it.

Making sure you've used fclose() and closedir() — and plain close() if you've used any file descriptors by calling open() directly — should help. If, however, the call to getcwd() is the very first thing your code is doing, it won't be because you've opened many files (you haven't).

If there are still problems after files have been closed, then you need to take a step back and review the larger context.

For example:

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • EDIT: I didn't use `closedir()`. Now, I am. The rest(fclose and close) I don't know why I need them here – C.J Jun 29 '18 at 22:59
  • You need to use matching open/close pairs — `opendir()`/`closedir()`, `fopen()`/`fclose()`, `open()`/`close()`. If you don't use `fopen()`, you don't need to use `fclose()`. If you don't use `open()` — or any of the large number of other system calls that create open file descriptors — you don't need to use `close()`. Just make sure you close what you open with the correct function (system) call. – Jonathan Leffler Jun 29 '18 at 23:09
  • Yes, `opendir` and `closedir` pair is what I am using now. – C.J Jun 29 '18 at 23:10