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 :-)
getcwd error when $HOME
One suggested step in debugging was:
Since
getcwd()
setserrno
when it fails, you should probably reporterrno
, maybe withperror("getcwd")
. Although I'm not keen onperror()
, 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 thegetcwd()
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 afterreaddir()
? stat()
error 'No such file or directory' when file name is returned byreaddir()
C programming getcwd variable manipulation
What about the following code it's work for me on ubuntu
-
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
int main( void ){
char* cwd;
char buff[PATH_MAX + 1];
cwd = getcwd( buff, PATH_MAX + 1 );
if( cwd != NULL ) {
printf( "My working directory is %s.\n", cwd );
if(strcmp("/home/razib/Desktop", cwd) == 0) {
printf("I'm in Desktop now\n");
}
}
return EXIT_SUCCESS;
}
Here you have to provide getcwd()
method a buff[]
. The buff[]
may be declared with size PATH_MAX+1
. PATH_MAX
can be found at limits.h
.
Hope it will help you.
Thanks a lot.
What's wrong with my function() in Shell Script! Syntax Token errors near {?
I guess this is your code.
#!/bin/bash
# I get the newest file in Directory
latest_file=$(ls -t | head -n 1)
# getting Token syntax error here
getAlldoublicate()
{
# here I try to find dublicate rows in csv
Alldoublicate=$(tr -s ',' ' ' < $1 | awk '{print $2" "$3" "$4}' | uniq -d)
}
if [[ -s $latest_file ]]; then
# here I check if file is emty
getAlldoublicate $latest_file
else
cat $latest_file | mailx -s "$latest_file is empty" bla.. @bla
fi
Three points you need to pay attention:
function
must be defined first before being use.- You can pass the
latest_file
as an argument when callinggetAlldoublicate
. Then you could use it by$1
in the function. ($0
stands for the function being called itself). - It would be better if you read the How to Format Tutorials before asking questions.
C, Linux, getcwd/chdir(): get binary path
This is exactly the kind of thing autoconf lives for, and supporting those standard directories is pretty much mandatory if you ever want anyone other than the programmers who wrote your software to use it. Once set up properly, to debug out of your home directory all you have to do is pass a different --prefix=
value to configure
.
Related Topics
Different File Owner Inside Docker Container and in Host MAChine
How to Group by Column Values into Row and Column Header and Then Sum the Value
How to Measure Separate CPU Core Usage for a Process
Total Size of the Contents of All the Files in a Directory
Linux: Which Process Is Causing "Device Busy" When Doing Umount
Are Tar.Gz and Tgz the Same Thing
How to Speed Up Linux Kernel Compilation
Should I Put Trailing Slash After Source and Destination When Copy Folders
Linux: Screen Desktop Video Capture Over Network, and Vnc Framerate
Xdg Basedir Directories for Windows
How to Append to a File Using X86-64 Linux System Calls
Dbd::Oracle Installation Causing Error
How to Get a List of Programs Running with Nohup
Check Battery Level of Connected Bluetooth Device on Linux
Indenting Multi-Line Output in a Shell Script
Why Does the Linux Kernel Repository Have Only One Branch
How to Conveniently Sync a File Between Two Git Repositories