create a link to an existing file
#include <unistd.h> int link( const char *existing, const char *new );
The link() function creates a new directory entry named by new to refer to (that is, to be a link to) an existing file named by existing. The function atomically creates a new link for the existing file, and increments the link count of the file by one.
This implementation does not support using link() on directories or the linking of files across file systems (different logical disks). |
If the function fails, no link is created, and the link count of the file remains unchanged.
If link() succeeds, the st_ctime field of the file and the st_ctime and st_mtime fields of the directory that contains the new entry are marked for update.
/* * The following program performs a rename * operation of argv[1] to argv[2]. * Please note that this example, unlike the * library function rename(), ONLY works if * argv[2] doesn't already exist. */ #include <stdio.h> #include <unistd.h> #include <stdlib.h> void main( int argc, char **argv ) { /* create a link of argv[1] to argv[2]. */ if( link( argv[1], argv[2] ) == -1 ) { perror( "link" ); exit( EXIT_FAILURE ); } if( unlink( argv[1] ) == -1 ) { perror( argv[1] ); exit( EXIT_FAILURE ); } exit( EXIT_SUCCESS ); }
POSIX 1003.1
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | Yes |
Thread | No |
errno, rename(), symlink(), unlink()