lliinnkk() -- System Call (libc)

Create a link
#iinncclluuddee <uunniissttdd.hh>
lliinnkk(_o_l_d, _n_e_w)
cchhaarr *_o_l_d, *_n_e_w;

A _l_i_n_k to a file is  another name for the file.  All attributes of the file
appear identical among all links.

lliinnkk() creates a link called _n_e_w to an existing file named _o_l_d.

For  administrative  reasons, it  is  an  error for  users  other than  the
superuser to  create a link to  a directory.  Such links  can make the file
system  no  longer  tree  structured  unless carefully  controlled,  posing
problems for commands such as ffiinndd.

_E_x_a_m_p_l_e_s
The first  example, called lloocckk.cc,  demonstrates how lliinnkk() can  be used to
perform  intertask locking.   With this  technique, a  program can  start a
process  in  the background  and  stop  any other  user  from starting  the
identical process.

#include <unistd.h>
main()
{
    if(link("lock.c", "lockfile") == -1) {
        printf("Cannot link\n");
        exit(1);
    }

    sleep(50);  /* do nothing for 50 seconds */
    unlink("lockfile");
    printf("done\n");
    exit(EXIT_SUCCESS);
}

The second example demonstrates how to  use lliinnkk() and uunnlliinnkk() to rename a
file.

#include <stdio.h>
#include <unistd.h>
main(argc, argv) int argc; char *argv[];
{
    register char *old, *new;

    if (argc != 3) {
        fprintf(stderr, "Usage: rename old new\n");
        exit(EXIT_FAILURE);
    }
    old = argv[1];
    new = argv[2];

    if (link(old, new) == -1) {
        fprintf(stderr, "rename: link(%s, %s) failed\n", old, new);
        exit(EXIT_FAILURE);
    } else if (unlink(old) == -1) {
        fprintf(stderr, "rename: unlink(%s) failed\n", old);
        exit(EXIT_FAILURE);
    }
    exit(EXIT_SUCCESS);
}

_S_e_e _A_l_s_o
ffiinndd, lliibbcc, llnn, rreennaammee(), uunnlliinnkk(), uunniissttdd.hh
POSIX Standard, section 5.3.4

_D_i_a_g_n_o_s_t_i_c_s
lliinnkk() returns  zero when successful.   It returns -1 on  errors, e.g., _o_l_d
does not exist, _n_e_w already exists, attempt to link across file systems, or
no permission to create _n_e_w in the target directory.

_N_o_t_e_s
Because each mounted file  system is a self-contained entity, links between
different mounted file systems fail.
