home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / a / bin / fileutil.12 / fileutil / fileutils-3.12 / src / ln.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-08  |  11.0 KB  |  436 lines

  1. /* `ln' program to create links between files.
  2.    Copyright (C) 1986, 1989, 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by Mike Parker and David MacKenzie. */
  19.  
  20. #ifdef _AIX
  21.  #pragma alloca
  22. #endif
  23.  
  24. #include <config.h>
  25. #include <stdio.h>
  26. #include <sys/types.h>
  27. #include <getopt.h>
  28. #include "system.h"
  29. #include "backupfile.h"
  30. #include "version.h"
  31. #include "safe-lstat.h"
  32. #include "safe-stat.h"
  33.  
  34. int link ();            /* Some systems don't declare this anywhere. */
  35.  
  36. #ifdef S_ISLNK
  37. int symlink ();
  38. #endif
  39.  
  40. /* Construct a string NEW_DEST by concatenating DEST, a slash, and
  41.    basename(SOURCE) in alloca'd memory.  Don't modify DEST or SOURCE.  */
  42.  
  43. #define PATH_BASENAME_CONCAT(new_dest, dest, source)            \
  44.     do                                    \
  45.       {                                    \
  46.     char *source_base;                        \
  47.     char *tmp_source;                        \
  48.                                     \
  49.     tmp_source = (char *) alloca (strlen ((source)) + 1);        \
  50.     strcpy (tmp_source, (source));                    \
  51.     strip_trailing_slashes (tmp_source);                \
  52.     source_base = basename (tmp_source);                \
  53.                                     \
  54.     (new_dest) = (char *) alloca (strlen ((dest)) + 1        \
  55.                       + strlen (source_base) + 1);    \
  56.     stpcpy (stpcpy (stpcpy ((new_dest), (dest)), "/"), source_base);\
  57.       }                                    \
  58.     while (0)
  59.  
  60. char *basename ();
  61. enum backup_type get_version ();
  62. int isdir ();
  63. int yesno ();
  64. void error ();
  65. void strip_trailing_slashes ();
  66. char *stpcpy ();
  67.  
  68. static void usage ();
  69. static int do_link ();
  70.  
  71. /* The name by which the program was run, for error messages.  */
  72. char *program_name;
  73.  
  74. /* A pointer to the function used to make links.  This will point to either
  75.    `link' or `symlink'. */
  76. static int (*linkfunc) ();
  77.  
  78. /* If nonzero, make symbolic links; otherwise, make hard links.  */
  79. static int symbolic_link;
  80.  
  81. /* If nonzero, ask the user before removing existing files.  */
  82. static int interactive;
  83.  
  84. /* If nonzero, remove existing files unconditionally.  */
  85. static int remove_existing_files;
  86.  
  87. /* If nonzero, list each file as it is moved. */
  88. static int verbose;
  89.  
  90. /* If nonzero, allow the superuser to make hard links to directories. */
  91. static int hard_dir_link;
  92.  
  93. /* If nonzero, and the specified destination is a symbolic link to a
  94.    directory, treat it just as if it were a directory.  Otherwise, the
  95.    command `ln --force --no-dereference file symlink-to-dir' deletes
  96.    symlink-to-dir before creating the new link.  */
  97. static int dereference_dest_dir_symlinks = 1;
  98.  
  99. /* If non-zero, display usage information and exit.  */
  100. static int show_help;
  101.  
  102. /* If non-zero, print the version on standard output and exit.  */
  103. static int show_version;
  104.  
  105. static struct option const long_options[] =
  106. {
  107.   {"backup", no_argument, NULL, 'b'},
  108.   {"directory", no_argument, &hard_dir_link, 1},
  109.   {"no-dereference", no_argument, NULL, 'n'},
  110.   {"force", no_argument, NULL, 'f'},
  111.   {"interactive", no_argument, NULL, 'i'},
  112.   {"suffix", required_argument, NULL, 'S'},
  113.   {"symbolic", no_argument, &symbolic_link, 1},
  114.   {"verbose", no_argument, &verbose, 1},
  115.   {"version-control", required_argument, NULL, 'V'},
  116.   {"help", no_argument, &show_help, 1},
  117.   {"version", no_argument, &show_version, 1},
  118.   {NULL, 0, NULL, 0}
  119. };
  120.  
  121. void
  122. main (argc, argv)
  123.      int argc;
  124.      char **argv;
  125. {
  126.   int c;
  127.   int errors;
  128.   int make_backups = 0;
  129.   char *version;
  130.  
  131.   version = getenv ("SIMPLE_BACKUP_SUFFIX");
  132.   if (version)
  133.     simple_backup_suffix = version;
  134.   version = getenv ("VERSION_CONTROL");
  135.   program_name = argv[0];
  136.   linkfunc = link;
  137.   symbolic_link = remove_existing_files = interactive = verbose
  138.     = hard_dir_link = 0;
  139.   errors = 0;
  140.  
  141.   while ((c = getopt_long (argc, argv, "bdfinsvFS:V:", long_options, (int *) 0))
  142.      != EOF)
  143.     {
  144.       switch (c)
  145.     {
  146.     case 0:            /* Long-named option. */
  147.        break;
  148.     case 'b':
  149.       make_backups = 1;
  150.       break;
  151.     case 'd':
  152.     case 'F':
  153.       hard_dir_link = 1;
  154.       break;
  155.     case 'f':
  156.       remove_existing_files = 1;
  157.       interactive = 0;
  158.       break;
  159.     case 'i':
  160.       remove_existing_files = 0;
  161.       interactive = 1;
  162.       break;
  163.     case 'n':
  164.       dereference_dest_dir_symlinks = 0;
  165.       break;
  166.     case 's':
  167. #ifdef S_ISLNK
  168.       symbolic_link = 1;
  169. #else
  170.       error (1, 0, "symbolic links are not supported on this system");
  171. #endif
  172.       break;
  173.     case 'v':
  174.       verbose = 1;
  175.       break;
  176.     case 'S':
  177.       simple_backup_suffix = optarg;
  178.       break;
  179.     case 'V':
  180.       version = optarg;
  181.       break;
  182.     default:
  183.       usage (1);
  184.       break;
  185.     }
  186.     }
  187.  
  188.   if (show_version)
  189.     {
  190.       printf ("%s\n", version_string);
  191.       exit (0);
  192.     }
  193.  
  194.   if (show_help)
  195.     usage (0);
  196.  
  197.   if (optind == argc)
  198.     {
  199.       error (0, 0, "missing file argument");
  200.       usage (1);
  201.     }
  202.  
  203.   if (make_backups)
  204.     backup_type = get_version (version);
  205.  
  206. #ifdef S_ISLNK
  207.   if (symbolic_link)
  208.     linkfunc = symlink;
  209. #endif
  210.  
  211.   if (optind == argc - 1)
  212.     errors = do_link (argv[optind], ".");
  213.   else if (optind == argc - 2)
  214.     {
  215.       struct stat source_stats;
  216.       char *source;
  217.       char *dest;
  218.       char *new_dest;
  219.  
  220.       source = argv[optind];
  221.       dest = argv[optind + 1];
  222.  
  223.       /* When the destination is specified with a trailing slash and the
  224.      source exists but is not a directory, convert the user's command
  225.      `ln source dest/' to `ln source dest/basename(source)'.  */
  226.  
  227.       if (dest[strlen (dest) - 1] == '/'
  228.       && SAFE_LSTAT (source, &source_stats) == 0
  229.       && !S_ISDIR (source_stats.st_mode))
  230.     {
  231.       PATH_BASENAME_CONCAT (new_dest, dest, source);
  232.     }
  233.       else
  234.     {
  235.       new_dest = dest;
  236.     }
  237.  
  238.       errors = do_link (source, new_dest);
  239.     }
  240.   else
  241.     {
  242.       char *to;
  243.  
  244.       to = argv[argc - 1];
  245.       if (!isdir (to))
  246.     error (1, 0, "when making multiple links, last argument must be a directory");
  247.       for (; optind < argc - 1; ++optind)
  248.     errors += do_link (argv[optind], to);
  249.     }
  250.  
  251.   exit (errors != 0);
  252. }
  253.  
  254. /* Make a link DEST to the (usually) existing file SOURCE.
  255.    Symbolic links to nonexistent files are allowed.
  256.    If DEST is a directory, put the link to SOURCE in that directory.
  257.    Return 1 if there is an error, otherwise 0.  */
  258.  
  259. static int
  260. do_link (source, dest)
  261.      char *source;
  262.      char *dest;
  263. {
  264.   struct stat dest_stats;
  265.   char *dest_backup = NULL;
  266.   int lstat_status;
  267.  
  268.   /* Use stat here instead of lstat.
  269.      On SVR4, link does not follow symlinks, so this check disallows
  270.      making hard links to symlinks that point to directories.  Big deal.
  271.      On other systems, link follows symlinks, so this check is right.  */
  272.   if (!symbolic_link)
  273.     {
  274.       struct stat source_stats;
  275.  
  276.       if (SAFE_STAT (source, &source_stats) != 0)
  277.     {
  278.       error (0, errno, "%s", source);
  279.       return 1;
  280.     }
  281.       if (!hard_dir_link && S_ISDIR (source_stats.st_mode))
  282.     {
  283.       error (0, 0, "%s: hard link not allowed for directory", source);
  284.       return 1;
  285.     }
  286.     }
  287.  
  288.   if (SAFE_LSTAT (dest, &dest_stats) != 0 && errno != ENOENT)
  289.     {
  290.       error (0, errno, "%s", dest);
  291.       return 1;
  292.     }
  293.  
  294.   /* If the destination is a directory or (it is a symlink to a directory
  295.      and the user has not specified --no-dereference), then form the
  296.      actual destination name by appending basename (source) to the
  297.      specified destination directory.  */
  298.   lstat_status = SAFE_LSTAT (dest, &dest_stats);
  299.  
  300.   if (lstat_status != 0 && errno != ENOENT)
  301.     {
  302.       error (0, errno, "%s", dest);
  303.       return 1;
  304.     }
  305.  
  306.   if ((lstat_status == 0
  307.        && S_ISDIR (dest_stats.st_mode))
  308. #ifdef S_ISLNK
  309.       || (dereference_dest_dir_symlinks
  310.       && (S_ISLNK (dest_stats.st_mode)
  311.       && isdir (dest)))
  312. #endif
  313.      )
  314.     {
  315.       /* Target is a directory; build the full filename. */
  316.       char *new_dest;
  317.       PATH_BASENAME_CONCAT (new_dest, dest, source);
  318.       dest = new_dest;
  319.       /* Set this to non-zero to force another call to SAFE_LSTAT
  320.      with the new destination.  */
  321.       lstat_status = 1;
  322.     }
  323.  
  324.   if (lstat_status == 0 || SAFE_LSTAT (dest, &dest_stats) == 0)
  325.     {
  326.       if (S_ISDIR (dest_stats.st_mode))
  327.     {
  328.       error (0, 0, "%s: cannot overwrite directory", dest);
  329.       return 1;
  330.     }
  331.       if (interactive)
  332.     {
  333.       fprintf (stderr, "%s: replace `%s'? ", program_name, dest);
  334.       if (!yesno ())
  335.         return 0;
  336.     }
  337.       else if (!remove_existing_files)
  338.     {
  339.       error (0, 0, "%s: File exists", dest);
  340.       return 1;
  341.     }
  342.  
  343.       if (backup_type != none)
  344.     {
  345.       char *tmp_backup = find_backup_file_name (dest);
  346.       if (tmp_backup == NULL)
  347.         error (1, 0, "virtual memory exhausted");
  348.       dest_backup = (char *) alloca (strlen (tmp_backup) + 1);
  349.       strcpy (dest_backup, tmp_backup);
  350.       free (tmp_backup);
  351.       if (rename (dest, dest_backup))
  352.         {
  353.           if (errno != ENOENT)
  354.         {
  355.           error (0, errno, "cannot backup `%s'", dest);
  356.           return 1;
  357.         }
  358.           else
  359.         dest_backup = NULL;
  360.         }
  361.     }
  362.       else if (unlink (dest) && errno != ENOENT)
  363.     {
  364.       error (0, errno, "cannot remove `%s'", dest);
  365.       return 1;
  366.     }
  367.     }
  368.   else if (errno != ENOENT)
  369.     {
  370.       error (0, errno, "%s", dest);
  371.       return 1;
  372.     }
  373.  
  374.   if (verbose)
  375.     printf ("%s -> %s\n", source, dest);
  376.  
  377.   if ((*linkfunc) (source, dest) == 0)
  378.     {
  379.       return 0;
  380.     }
  381.  
  382.   error (0, errno, "cannot %slink `%s' to `%s'",
  383. #ifdef S_ISLNK
  384.      symbolic_link ? "symbolic " : "",
  385. #else
  386.      "",
  387. #endif
  388.      source, dest);
  389.  
  390.   if (dest_backup)
  391.     {
  392.       if (rename (dest_backup, dest))
  393.     error (0, errno, "cannot un-backup `%s'", dest);
  394.     }
  395.   return 1;
  396. }
  397.  
  398. static void
  399. usage (status)
  400.      int status;
  401. {
  402.   if (status != 0)
  403.     fprintf (stderr, "Try `%s --help' for more information.\n",
  404.          program_name);
  405.   else
  406.     {
  407.       printf ("\
  408. Usage: %s [OPTION]... SOURCE [DEST]\n\
  409.   or:  %s [OPTION]... SOURCE... DIRECTORY\n\
  410. ",
  411.           program_name, program_name);
  412.       printf ("\
  413. \n\
  414.   -b, --backup                 make backups for removed files\n\
  415.   -d, -F, --directory          hard link directories (super-user only)\n\
  416.   -f, --force                  remove existing destinations\n\
  417.   -n, --no-dereference         with --force, remove destination that is a\n\
  418.                                  symlink to a directory\n\
  419.   -i, --interactive            prompt whether to remove destinations\n\
  420.   -s, --symbolic               make symbolic links, instead of hard links\n\
  421.   -v, --verbose                print name of each file before linking\n\
  422.   -S, --suffix=SUFFIX          override the usual backup suffix\n\
  423.   -V, --version-control=WORD   override the usual version control\n\
  424.       --help                   display this help and exit\n\
  425.       --version                output version information and exit\n\
  426. \n\
  427. The backup suffix is ~, unless set with SIMPLE_BACKUP_SUFFIX.  The\n\
  428. version control may be set with VERSION_CONTROL, values are:\n\
  429. \n\
  430.   t, numbered     make numbered backups\n\
  431.   nil, existing   numbered if numbered backups exist, simple otherwise\n\
  432.   never, simple   always make simple backups\n");
  433.     }
  434.   exit (status);
  435. }
  436.