symlink is another system call used to create symlinks of a real file. symlinks are many ways useful to shorthand represent a long path, to represent a generic name for paths with random names etc...
The symlink prototype is as follows.
int symlink(const char *target, const char *linkpath);
the target
is the original file and linkpath
is the link file. It is advised that both of the arguments should be represented with the absolute paths thus avoiding the broken links although the real directory or the file is present.
If the file that the link is pointing to is deleted or moved somewhere else, the link becomes invalid. This link is called as dangling symlink.
Another important note is that when a link gets deleted with the unlink
command, only the link will be removed not the original file that the link is pointing to.
The following example provides the symlink
API in use:
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int ret;
if (argc != 3) {
fprintf(stderr, "%s <original file> <symlink>\n", argv[0]);
return -1;
}
ret = symlink(argv[1], argv[2]);
if (ret != 0) {
perror("symlink");
fprintf(stderr, "failed to create symlink %s for file %s\n", argv[2], argv[1]);
return -1;
}
printf("created symlink %s for %s\n", argv[2], argv[1]);
return 0;
}
the <original file>
and <symlink>
must be represented with the absolute paths via the command line. such as the below example:
./a.out /home/dev/test.c /home/dev/work/test.c
created symlink /home/dev/work/test.c for /home/dev/test.c