You are not logged in.
Has anyone experienced the following issue with stat() ?
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int main( int argc, char **argv )
{
struct stat buf;
if( argc < 2 )
return 0;
char *link = strdup( argv[1] );
if( stat( link, &buf ) == -1 ) {
puts( "Error" );
return 1;
}
if( ( S_IFLNK & buf.st_mode ) == S_IFLNK )
puts( "Is link" );
else
puts( "Not link" );
return 0;
}
If you compile and run the above code (or similar code), you would expect it to report that the given argument is a symbolic link when indeed the argument is a symbolic link; though, instead of the expected happening, the following happens as shown below:
$ gcc -o islink islink.c
$ ln -s /tmp link
$ ./islink link
Not link
$
Is anyone else experiencing this? My filesystem is ext4. The same error happens when using the S_ISLNK() macro.
Offline
man 3p lstat
DESCRIPTION
The lstat() function shall be equivalent to stat(), except when path
refers to a symbolic link. In that case lstat() shall return informa‐
tion about the link, while stat() shall return information about the
file the link references.
Offline
Ah! Thank you. I forgot about that.
Offline