Last active
August 29, 2015 14:14
-
-
Save Aruna-Hewapathirane/31614f180db5ac18620b to your computer and use it in GitHub Desktop.
symbolic link and readlink system call
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
man 2 readlink |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
freedom@debian:~/C$ ls -al /dev/block | |
total 0 | |
drwxr-xr-x 2 root root 480 Jan 29 22:26 . | |
drwxr-xr-x 14 root root 3500 Jan 29 22:27 .. | |
lrwxrwxrwx 1 root root 6 Jan 29 22:26 11:0 -> ../sr0 | |
lrwxrwxrwx 1 root root 8 Jan 29 22:26 7:0 -> ../loop0 | |
lrwxrwxrwx 1 root root 8 Jan 29 22:26 7:1 -> ../loop1 | |
lrwxrwxrwx 1 root root 8 Jan 29 22:26 7:2 -> ../loop2 | |
lrwxrwxrwx 1 root root 8 Jan 29 22:26 7:3 -> ../loop3 | |
lrwxrwxrwx 1 root root 8 Jan 29 22:26 7:4 -> ../loop4 | |
lrwxrwxrwx 1 root root 8 Jan 29 22:26 7:5 -> ../loop5 | |
lrwxrwxrwx 1 root root 8 Jan 29 22:26 7:6 -> ../loop6 | |
lrwxrwxrwx 1 root root 8 Jan 29 22:26 7:7 -> ../loop7 | |
lrwxrwxrwx 1 root root 6 Jan 29 22:26 8:0 -> ../sda | |
lrwxrwxrwx 1 root root 7 Jan 29 22:26 8:1 -> ../sda1 | |
lrwxrwxrwx 1 root root 6 Jan 29 22:26 8:16 -> ../sdb | |
lrwxrwxrwx 1 root root 7 Jan 29 22:26 8:2 -> ../sda2 | |
lrwxrwxrwx 1 root root 7 Jan 29 22:26 8:3 -> ../sda3 | |
lrwxrwxrwx 1 root root 6 Jan 29 22:26 8:32 -> ../sdc | |
lrwxrwxrwx 1 root root 7 Jan 29 22:26 8:4 -> ../sda4 | |
lrwxrwxrwx 1 root root 6 Jan 29 22:26 8:48 -> ../sdd | |
lrwxrwxrwx 1 root root 7 Jan 29 22:26 8:5 -> ../sda5 | |
lrwxrwxrwx 1 root root 7 Jan 29 22:26 8:6 -> ../sda6 | |
lrwxrwxrwx 1 root root 6 Jan 29 22:26 8:64 -> ../sde | |
lrwxrwxrwx 1 root root 7 Jan 29 22:26 8:7 -> ../sda7 | |
lrwxrwxrwx 1 root root 6 Jan 29 22:26 8:80 -> ../sdf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <errno.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
int main (int argc, char* argv[]) | |
{ | |
char target_path[256]; | |
char* link_path = argv[1]; | |
/* Attempt to read the target of the symbolic link. */ | |
int len = readlink (link_path, target_path, sizeof (target_path)); | |
if (len == -1) { | |
/* The call failed. */ | |
if (errno == EINVAL) | |
/* It's not a symbolic link; report that. */ | |
fprintf (stderr, "%s is not a symbolic link\n", link_path); | |
else | |
/* Some other problem occurred; print the generic message. */ | |
perror ("readlink"); | |
return 1; | |
} | |
else { | |
/* NUL-terminate the target path. */ | |
target_path[len] = '\0'; | |
/* Print it. */ | |
printf ("%s\n", target_path); | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment