LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-08-2010, 08:23 AM   #1
ronbarak
LQ Newbie
 
Registered: Nov 2004
Posts: 11

Rep: Reputation: 0
Question 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);

}
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 11-08-2010, 08:30 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,141

Rep: Reputation: 233Reputation: 233Reputation: 233
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.
 
2 members found this post helpful.
Old 11-08-2010, 11:35 AM   #3
ronbarak
LQ Newbie
 
Registered: Nov 2004
Posts: 11

Original Poster
Rep: Reputation: 0
Thumbs up

Quote:
Originally Posted by Guttorm View Post
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.
 
Old 11-08-2010, 12:14 PM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 808

Rep: Reputation: 178Reputation: 178
Quote:
Originally Posted by ronbarak View Post
I visited Trondheim, and still remember its somber beauty.
lstat() has a somber beauty of its own.
 
  


Reply

Tags
linux, stat


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Maxtor Onetouch II: symlink points to wrong target Robert S Linux - Hardware 2 12-23-2007 03:26 PM
'Stat' getting infro from a symlink khanrockz Linux - Newbie 3 07-10-2007 08:57 PM
Copy the target from a symlink? menator Linux - Newbie 1 01-25-2007 07:04 AM
How to find symlink target name in script germanyzulu Linux - Software 3 08-09-2006 03:14 PM
Tip: How to copy a symlink and its target file in a script fotoguy Programming 4 08-05-2006 03:24 PM


All times are GMT -5. The time now is 08:09 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration