Readlink and symbolic link file

Posted on 2006-06-06
Last Modified: 2010-04-15
Hi folks,

I have a soft link file, and I know I can use lstat to read the soft link file information. Now I want to read the contents (i.e: name, path) of the file this soft link file referred to. I know I need to use readlink() to do that. But could you please give me an example of how to get the desitination file's attibutes (i.e: path, name, etc).

Question by:tiger0516
    1 Comment
    LVL 24

    Accepted Solution

    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <string.h>
    #include <errno.h>
    #include <limits.h>

    static void usage (void){
      fprintf(stdout, "program-name <file to run stat on>\n");
      exit (EXIT_FAILURE);

    int main (int argc, char* argv[]){
      struct stat stats = {0};
      char *file_to_stat = NULL;
      int ret_val = 0;
      enum {BUFSIZE=512};
      char error_msg [BUFSIZE];
      char buf[BUFSIZE];

      if (argc < 2) {
      file_to_stat = argv[1];
      errno = 0;
      ret_val = lstat(file_to_stat, &stats);
      if (ret_val < 0) {
          strncpy(error_msg, strerror(errno), (size_t) BUFSIZE);
        fprintf (stderr, "stat failed on %s with %s\n", file_to_stat,
        exit (EXIT_FAILURE);

      if (S_ISLNK(stats.st_mode)){
        ret_val = readlink(file_to_stat, buf, BUFSIZE);
        printf("file_to_stat points to %s\n", buf);
      } else {
        puts("file is not a symbolic link");

      exit (EXIT_SUCCESS);

    The second parameter to readlink is an out parameter, that means the information is stored in that buffer, this buffer has to be allocated and the lenght of this buffer is the third parameter. Applied to the following sym link:
    07:55 t1 -> ugly_c.c

    I got. this output:
    /a.out t1
    file_to_stat points to ugly_c.c

    which obviously is ok ;-)



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
    The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now