You are not logged in.

#1 2010-04-17 14:53:58

LinuxFreak
Member
Registered: 2010-04-17
Posts: 5

Issue with stat() and symbolic links

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

#2 2010-04-17 14:57:18

toofishes
Developer
From: Chicago, IL
Registered: 2006-06-06
Posts: 602
Website

Re: Issue with stat() and symbolic links

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

#3 2010-04-17 14:59:32

LinuxFreak
Member
Registered: 2010-04-17
Posts: 5

Re: Issue with stat() and symbolic links

Ah! Thank you. I forgot about that.

Offline

Board footer

Powered by FluxBB