LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Question How to get stat() of symlink and not of target of symlink ? (http://www.linuxquestions.org/questions/programming-9/question-how-to-get-stat-of-symlink-and-not-of-target-of-symlink-843018/)

ronbarak 11-08-2010 09:23 AM

Question How to get stat() of symlink and not of target of symlink ?
 
The below C program, on ubuntu 10.04, does not recognize symbolic links, but prints the status of the target of the link.
What should I change to get from stat() that the file is a symlink and not the status of the target of the symlink ?

Looking on google didn't give me any ideas, and other documentation I found doesn't help either.

Thanks,
Ron.

$ ls -lsa ./RCS_link

0 lrwxrwxrwx 1 ronbarak ronbarak 5 2010-11-08 15:47 ./RCS_link -> ./RCS

$ ./stat_ex ./RCS_link

argv[1]: ./RCS_link

directory

$
$ ls -lsa .bashrc

0 lrwxrwxrwx 1 ronbarak ronbarak 22 2010-11-08 14:40 .bashrc -> /home/ronbarak/.bashrc

$ ./stat_ex .bashrc

argv[1]: .bashrc

regular file

$

$ ls -lsa /proc/self/fd/0

0 lrwx------ 1 ronbarak ronbarak 64 2010-11-08 15:51 /proc/self/fd/0 -> /dev/pts/5

$ ./stat_ex /proc/self/fd/0

argv[1]: /proc/self/fd/0

character device

$


$ cat ./stat_ex.c

#include <sys/types.h>

#include <sys/stat.h>

#include <sys/dir.h>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <string.h>

#include <errno.h>



main(int argc, char *argv[])

{

struct stat sbuf;



if (argc < 2)

{

fprintf(stderr, "Usage: %s <pathname> [<pathname>]\n",

argv[0]);

exit(EXIT_FAILURE);

}



while (--argc)

{

printf("argv[%d]: %s\n", argc,argv[argc]);

if (stat(argv[argc], &sbuf) == -1)

{

perror("stat");

exit(EXIT_SUCCESS);

}

switch (sbuf.st_mode & S_IFMT)

{

case S_IFBLK: printf("block device\n");

break;

case S_IFCHR: printf("character device\n");

break;

case S_IFDIR: printf("directory\n");

break;

case S_IFIFO: printf("FIFO/pipe\n");

break;

case S_IFLNK: printf("symlink\n");

break;

case S_IFREG: printf("regular file\n");

break;

case S_IFSOCK: printf("socket\n");

break;

default: printf("unknown?\n");

break;

}

}

exit(EXIT_SUCCESS);

}

Guttorm 11-08-2010 09:30 AM

Hi

Use lstat instead of stat.

From man stat:
Quote:

lstat() is identical to stat(), except that if path is a symbolic link, then the link itself is stat-ed, not the file that it refers to.

ronbarak 11-08-2010 12:35 PM

Quote:

Originally Posted by Guttorm (Post 4152669)
Hi

Use lstat instead of stat.

From man stat:

Thanks Guttorm, That's what I need.
BTW, years ago (1996) I visited Trondheim, and still remember its somber beauty.

wje_lq 11-08-2010 01:14 PM

Quote:

Originally Posted by ronbarak (Post 4152824)
I visited Trondheim, and still remember its somber beauty.

lstat() has a somber beauty of its own.


All times are GMT -5. The time now is 03:08 PM.