Chapter Contents

Previous

Next
symlink

symlink



Make Symbolic Link

Portability: POSIX.1 conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <sys/types.h>
#include <unistd.h>

int symlink(const char *path, const char *sl);


DESCRIPTION

symlink creates a symbolic link to an USS HFS file. The file need not already exist. path is the pathname of the file. sl is the pathname to be assigned to the symbolic link.

When you call symlink in a non- posix -compiled application, the pathname and the link name both are interpreted according to the normal rules for interpretation of filenames. These names should include a style prefix if the default style is not "hfs") . Note that the style prefix is not actually stored in the symbolic link.


RETURN VALUE

symlink returns 0 if it is successful and- 1 if it is not successful.


EXAMPLE

This example attempts to define the second argument as a hard link to the first argument. If this fails with errno equal to EXDEV , indicating that links are not supported between file systems, the second argument is created as a symbolic link instead.

/* This example must be compiled using the posix compiler option. */

#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

main(int argc, char *argv[]) {
   int rc;
   if (argc != 3) {
      fputs("Incorrect number of arguments.", stderr);
      exit(EXIT_FAILURE);
   }
   rc = link(argv[1], argv[2]);  /* Try to make hard link. */
   if (rc != 0)                  /* if link failed         */
      if (errno != EXDEV) {      /* unexpected error       */
         perror("link error");
         exit(EXIT_FAILURE);
      } else {                   /* Attempt symbolic link. */
         rc = symlink(argv[1], argv[2]);
         if (rc != 0) {
            perror("symlink error");
            exit(EXIT_FAILURE);
         }
         printf("%s was created as a symbolic link to %s.\n",
                argv[2], argv[1]);
      }
   else printf("%s was created as a hard link to %s.\n",
               argv[2], argv[1]);
   exit(EXIT_SUCCESS);
}


RELATED FUNCTIONS

link , lstat , readlink


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.