Sh: 0: Getcwd() Failed: No Such File or Directory on Cited Drive

C - is calling getcwd causing a bug here?

readlink does not null-terminate the string it returns, so p2 may contain arbitrary garbage following /.

You should do

ssize_t len = readlink(fdpath, p2, sizeof(p2));
if (len == -1) { /* error */ }
if (len == sizeof(p2)) { /* truncated */ }
p2[len] = '\0';
if (!stat(p2, &st2)) // ...

You should do error checking in many other places, too.

Adding and removing the unrelated code at the end probably changed the stack layout (because of the additional local variables), meaning that p2 contained different garbage with and without it. It could even be that in some cases, the garbage was null bytes, so that the string happened to be properly terminated after all. This sort of thing happens all the time with bugs in C code and isn't particularly meaningful; it's undefined behavior in the language of the C standard. Don't think too hard about why it happens; just try to find and fix the bug and don't write the same bug again :-)

Print getcwd path

strcmp returns an integer and not a boolean.

int strcmp (const char* str1, const char* str2);

It returns a 0 if the two strings are equal, so you should be checking the returned value in your if statement like this:

if(strcmp(argv[0],"pwd")==0)

getcwd error when $HOME

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:

  • Why is stat() failing after readdir()?
  • stat() error 'No such file or directory' when file name is returned by readdir()


Related Topics



Leave a reply



Submit