Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Readlink and symbolic link file

Posted on 2006-06-06
Medium Priority
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

fridom earned 400 total points
ID: 16849757
#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 ;-)



Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

578 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