Standards / Extensions | C or C++ | Dependencies |
---|---|---|
POSIX.1a |
both |
#define _POSIX1_SOURCE 2
#include <unistd.h>
int symlink(const char *pathname, const char *slink);
Creates the symbolic link named by slink with the file specified by pathname. File access checking is not performed on the file pathname, and the file need not exist. In addition, a symbolic link can cross file system boundaries.
If the path name in the symbolic link does not start with / (slash), the symbolic link path name is resolved relative to the directory that contains the symbolic link.
If successful, symlink() returns 0.
If unsuccessful, symlink() returns -1, does not affect any file it names, and sets errno to one of the following values:
/* This example works only under z/OS XL C, not z/OS XL C++ */
#define _POSIX1_SOURCE 2
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
main() {
char fn[]="test.file";
char sln[]="test.symlink";
int fd;
if ((fd = creat(fn, S_IWUSR)) < 0)
perror("creat() error");
else {
close(fd);
puts("before symlink()");
system("ls -il test.*");
if (symlink(fn, sln) != 0) {
perror("symlink() error");
unlink(fn);
}
else {
puts("after symlink()");
system("ls -il test.*");
unlink(fn);
puts("after first unlink()");
system("ls -il test.*");
unlink(sln);
}
}
}
before symlink()
4030 --w------- 1 MVSUSR1 SYS1 0 Apr 20 13:57 test.file
after symlink()
4030 --w------- 1 MVSUSR1 SYS1 0 Apr 20 13:57 test.file
4031 l--------- 1 MVSUSR1 SYS1 9 Apr 20 13:57 test.symlink -> test.file
after first unlink()
4031 l--------- 1 MVSUSR1 SYS1 9 Apr 20 13:57 test.symlink -> test.file