symlink()

Updated: October 26, 2022

Create a symbolic link to a path

Synopsis:

#include <unistd.h>

int symlink( const char* pname,
             const char* slink );

Arguments:

pname
The path that you want to link to.
slink
The symbolic link that you want to create.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The symlink() function creates a symbolic link named slink that contains the pathname specified by pname (slink is the name of the symbolic link created, pname is the pathname contained in the symbolic link).

File access checking isn't performed on the file named by pname, and the file need not exist.

If symlink() is unsuccessful, any file named by slink is unaffected.

Returns:

0
Success.
-1
An error occurred (errno is set).

Errors:

EACCES
A component of the slink path prefix denies search permission, or write permission is denied in the parent directory of the symbolic link to be created.
EEXIST
A file named by slink already exists.
ELOOP
A loop exists in symbolic links encountered during resolution of the slink argument, and it resolves to more than SYMLOOP_MAX levels.
ENAMETOOLONG
A component of the path specified by slink exceeds NAME_MAX bytes, or the length of the entire pathname exceeds PATH_MAX characters.
ENOSPC
The new symbolic link can't be created because there's no space left on the filesystem that will contain the symbolic link.
ENOSYS
The symlink() function isn't implemented for the filesystem underlying the path specified in slink.
ENOTDIR
A component of the path prefix in slink isn't a directory.
EROFS
The file slink would reside on a read-only filesystem.

Examples:

/*
 * create a symbolic link to "/usr/nto/include"
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main( void )
  {
    if( symlink( "/usr/nto/include", "slink" ) == -1) {
      perror( "slink -> /usr/nto/include" );
      exit( EXIT_FAILURE );
    }
    exit( EXIT_SUCCESS );
  }

Classification:

POSIX 1003.1

Safety:  
Cancellation point No
Interrupt handler No
Signal handler Yes
Thread Yes