Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get absolute path of a symbolic link?

Tags:

c

linux

How can I get the absolute path of a symbolic link? If I do it in the following way:

char buf[100];
realpath(symlink, buf);

I won't get the absolute path of the symlink, but instead I would get the absolute path this symlink links to. Now my question is: What If I want to get the absolute path of the symlink itself? Is there any function in Linux C that allows me to do so?

Note: What I'd like to achieve is the absolute path of the symbolic link itself, not the path it's pointing to! For example, if the relative path of a symbolic link is Task2/sym_lnk, I want its absolute path, which can be /home/user/kp/Task2/sym_lnk.

like image 965
Kindermann Avatar asked Nov 03 '15 08:11

Kindermann


1 Answers

You can use realpath() function with the parent folder of the symlink, then you concatenate the symlink name.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>

// Find the last occurrence of c in str, otherwise returns NULL
char* find_last_of( char *str, char c )
{
    for( char *i = str + strlen(str) ; i >= str ; i-- )
        if( *i == c )
            return i;
    return NULL;
}

// Does the job
char* getAbsPath( char *path  )
{
    char *name; // Stores the symlink name
    char *tmp; // Aux for store the last /
    char *absPath = malloc( PATH_MAX ); // Stores the absolute path

    tmp = find_last_of( path, '/' );

    // If path is only the symlink name (there's no /), then the
    // parent folder is the current work directory
    if( tmp == NULL ){ 
        name = strdup( path );
        getcwd( absPath, PATH_MAX ); // Is already absolute path
    }
    else{
        // Extract the name and erase it from the original
        // path.
        name = strdup( tmp + 1 );
        *tmp = '\0';
        // Get the real path of the parent folder.
        realpath( path, absPath );
    }
    // Concatenate the realpath of the parent and  "/name"
    strcat( absPath, "/" );
    strcat( absPath, name );
    free( name );
    return absPath;
}

// Test the function
int main( int argc, char **argv )
{
    char *absPath;

    if( argc != 2 ){
        fprintf( stderr, "Use:\n\n %s <symlink>\n", *argv );
        return -1;
    }
    // Verify if path exists
    if( access( argv[1], F_OK ) ){
        perror( argv[1] );
        return -1;
    }

    absPath = getAbsPath( argv[1] );
    printf( "Absolute Path: %s\n", absPath );
    free( absPath );
    return 0;
}

If you use the above code with directories, it needs an special case for "." and "..", but works with "./" and "../"

like image 73
reroman Avatar answered Oct 03 '22 23:10

reroman