home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / useful / dist / gnu / fileutils / fileutils-3.9-amiga / src / ln.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-13  |  9.6 KB  |  387 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. #ifdef HAVE_CONFIG_H
  25. #if defined (CONFIG_BROKETS)
  26. /* We use <config.h> instead of "config.h" so that a compilation
  27.    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  28.    (which it would do because it found this file in $srcdir).  */
  29. #include <config.h>
  30. #else
  31. #include "config.h"
  32. #endif
  33. #endif
  34.  
  35. #include <stdio.h>
  36. #include <sys/types.h>
  37. #include <getopt.h>
  38. #include "system.h"
  39. #include "backupfile.h"
  40. #include "version.h"
  41.  
  42. int link ();            /* Some systems don't declare this anywhere. */
  43.  
  44. #ifdef S_ISLNK
  45. int symlink ();
  46. #endif
  47.  
  48. /* Construct a string NEW_DEST by concatenating DEST, a slash, and
  49.    basename(SOURCE) in alloca'd memory.  Don't modify DEST or SOURCE.  */
  50.  
  51. #define PATH_BASENAME_CONCAT(new_dest, dest, source)            \
  52.     do                                    \
  53.       {                                    \
  54.     char *source_base;                        \
  55.     char *tmp_source;                        \
  56.                                     \
  57.     tmp_source = (char *) alloca (strlen ((source)) + 1);        \
  58.     strcpy (tmp_source, (source));                    \
  59.     strip_trailing_slashes (tmp_source);                \
  60.     source_base = basename (tmp_source);                \
  61.                                     \
  62.     (new_dest) = (char *) alloca (strlen ((dest)) + 1        \
  63.                       + strlen (source_base) + 1);    \
  64.     stpcpy (stpcpy (stpcpy ((new_dest), (dest)), "/"), source_base);\
  65.       }                                    \
  66.     while (0)
  67.  
  68. char *basename ();
  69. enum backup_type get_version ();
  70. int isdir ();
  71. int yesno ();
  72. void error ();
  73. void strip_trailing_slashes ();
  74. char *stpcpy ();
  75.  
  76. static void usage ();
  77. static int do_link ();
  78.  
  79. /* The name by which the program was run, for error messages.  */
  80. char *program_name;
  81.  
  82. /* A pointer to the function used to make links.  This will point to either
  83.    `link' or `symlink'. */
  84. static int (*linkfunc) ();
  85.  
  86. /* If nonzero, make symbolic links; otherwise, make hard links.  */
  87. static int symbolic_link;
  88.  
  89. /* If nonzero, ask the user before removing existing files.  */
  90. static int interactive;
  91.  
  92. /* If nonzero, remove existing files unconditionally.  */
  93. static int remove_existing_files;
  94.  
  95. /* If nonzero, list each file as it is moved. */
  96. static int verbose;
  97.  
  98. /* If nonzero, allow the superuser to make hard links to directories. */
  99. static int hard_dir_link;
  100.  
  101. /* If non-zero, display usage information and exit.  */
  102. static int show_help;
  103.  
  104. /* If non-zero, print the version on standard output and exit.  */
  105. static int show_version;
  106.  
  107. static struct option const long_options[] =
  108. {
  109.   {"backup", no_argument, NULL, 'b'},
  110.   {"directory", no_argument, &hard_dir_link, 1},
  111.   {"force", no_argument, NULL, 'f'},
  112.   {"interactive", no_argument, NULL, 'i'},
  113.   {"suffix", required_argument, NULL, 'S'},
  114.   {"symbolic", no_argument, &symbolic_link, 1},
  115.   {"verbose", no_argument, &verbose, 1},
  116.   {"version-control", required_argument, NULL, 'V'},
  117.   {"help", no_argument, &show_help, 1},
  118.   {"version", no_argument, &show_version, 1},
  119.   {NULL, 0, NULL, 0}
  120. };
  121.  
  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, "bdfisvFS: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 's':
  164. #ifdef S_ISLNK
  165.       symbolic_link = 1;
  166. #else
  167.       error (1, 0, "symbolic links are not supported on this system");
  168. #endif
  169.       break;
  170.     case 'v':
  171.       verbose = 1;
  172.       break;
  173.     case 'S':
  174.       simple_backup_suffix = optarg;
  175.       break;
  176.     case 'V':
  177.       version = optarg;
  178.       break;
  179.     default:
  180.       usage (1);
  181.       break;
  182.     }
  183.     }
  184.  
  185.   if (show_version)
  186.     {
  187.       printf ("%s\n", version_string);
  188.       exit (0);
  189.     }
  190.  
  191.   if (show_help)
  192.     usage (0);
  193.  
  194.   if (optind == argc)
  195.     usage (1);
  196.  
  197.   if (make_backups)
  198.     backup_type = get_version (version);
  199.  
  200. #ifdef S_ISLNK
  201.   if (symbolic_link)
  202.     linkfunc = symlink;
  203. #endif
  204.  
  205.   if (optind == argc - 1)
  206.     errors = do_link (argv[optind], ".");
  207.   else if (optind == argc - 2)
  208.     {
  209.       struct stat source_stats;
  210.       char *source;
  211.       char *dest;
  212.       char *new_dest;
  213.  
  214.       source = argv[optind];
  215.       dest = argv[optind + 1];
  216.  
  217.       /* When the destination is specified with a trailing slash and the
  218.      source exists but is not a directory, convert the user's command
  219.      `ln source dest/' to `ln source dest/basename(source)'.  */
  220.  
  221.       if (dest[strlen (dest) - 1] == '/'
  222.       && lstat (source, &source_stats) == 0
  223.       && !S_ISDIR (source_stats.st_mode))
  224.     {
  225.       PATH_BASENAME_CONCAT (new_dest, dest, source);
  226.     }
  227.       else
  228.     {
  229.       new_dest = dest;
  230.     }
  231.  
  232.       errors = do_link (source, new_dest);
  233.     }
  234.   else
  235.     {
  236.       char *to;
  237.  
  238.       to = argv[argc - 1];
  239.       if (!isdir (to))
  240.     error (1, 0, "when making multiple links, last argument must be a directory");
  241.       for (; optind < argc - 1; ++optind)
  242.     errors += do_link (argv[optind], to);
  243.     }
  244.  
  245.   exit (errors != 0);
  246. }
  247.  
  248. /* Make a link DEST to existing file SOURCE.
  249.    If DEST is a directory, put the link to SOURCE in that directory.
  250.    Return 1 if there is an error, otherwise 0.  */
  251.  
  252. static int
  253. do_link (source, dest)
  254.      char *source;
  255.      char *dest;
  256. {
  257.   struct stat dest_stats;
  258.   char *dest_backup = NULL;
  259.  
  260.   /* isdir uses stat instead of lstat.
  261.      On SVR4, link does not follow symlinks, so this check disallows
  262.      making hard links to symlinks that point to directories.  Big deal.
  263.      On other systems, link follows symlinks, so this check is right.  */
  264.   if (!symbolic_link && !hard_dir_link && isdir (source))
  265.     {
  266.       error (0, 0, "%s: hard link not allowed for directory", source);
  267.       return 1;
  268.     }
  269.   if (isdir (dest))
  270.     {
  271.       /* Target is a directory; build the full filename. */
  272.       char *new_dest;
  273.       PATH_BASENAME_CONCAT (new_dest, dest, source);
  274.       dest = new_dest;
  275.     }
  276.  
  277.   if (lstat (dest, &dest_stats) == 0)
  278.     {
  279.       if (S_ISDIR (dest_stats.st_mode))
  280.     {
  281.       error (0, 0, "%s: cannot overwrite directory", dest);
  282.       return 1;
  283.     }
  284.       if (interactive)
  285.     {
  286.       fprintf (stderr, "%s: replace `%s'? ", program_name, dest);
  287.       if (!yesno ())
  288.         return 0;
  289.     }
  290.       else if (!remove_existing_files)
  291.     {
  292.       error (0, 0, "%s: File exists", dest);
  293.       return 1;
  294.     }
  295.  
  296.       if (backup_type != none)
  297.     {
  298.       char *tmp_backup = find_backup_file_name (dest);
  299.       if (tmp_backup == NULL)
  300.         error (1, 0, "virtual memory exhausted");
  301.       dest_backup = (char *) alloca (strlen (tmp_backup) + 1);
  302.       strcpy (dest_backup, tmp_backup);
  303.       free (tmp_backup);
  304.       if (rename (dest, dest_backup))
  305.         {
  306.           if (errno != ENOENT)
  307.         {
  308.           error (0, errno, "cannot backup `%s'", dest);
  309.           return 1;
  310.         }
  311.           else
  312.         dest_backup = NULL;
  313.         }
  314.     }
  315.       else if (unlink (dest) && errno != ENOENT)
  316.     {
  317.       error (0, errno, "cannot remove old link to `%s'", dest);
  318.       return 1;
  319.     }
  320.     }
  321.   else if (errno != ENOENT)
  322.     {
  323.       error (0, errno, "%s", dest);
  324.       return 1;
  325.     }
  326.  
  327.   if (verbose)
  328.     printf ("%s -> %s\n", source, dest);
  329.  
  330.   if ((*linkfunc) (source, dest) == 0)
  331.     {
  332.       return 0;
  333.     }
  334.  
  335.   error (0, errno, "cannot %slink `%s' to `%s'",
  336. #ifdef S_ISLNK
  337.          linkfunc == symlink ? "symbolic " : "",
  338. #else
  339.          "",
  340. #endif
  341.          source, dest);
  342.  
  343.   if (dest_backup)
  344.     {
  345.       if (rename (dest_backup, dest))
  346.     error (0, errno, "cannot un-backup `%s'", dest);
  347.     }
  348.   return 1;
  349. }
  350.  
  351. static void
  352. usage (status)
  353.      int status;
  354. {
  355.   if (status != 0)
  356.     fprintf (stderr, "Try `%s --help' for more information.\n",
  357.          program_name);
  358.   else
  359.     {
  360.       printf ("\
  361. Usage: %s [OPTION]... SOURCE [DEST]\n\
  362.   or:  %s [OPTION]... SOURCE... DIRECTORY\n\
  363. ",
  364.           program_name, program_name);
  365.       printf ("\
  366. \n\
  367.   -b, --backup                 make backups for removed files\n\
  368.   -d, -F, --directory          hard link directories (super-user only)\n\
  369.   -f, --force                  remove existing destinations\n\
  370.   -i, --interactive            prompt whether to remove destinations\n\
  371.   -s, --symbolic               make symbolic links, instead of hard links\n\
  372.   -v, --verbose                print name of each file before linking\n\
  373.   -S, --suffix SUFFIX          override the usual backup suffix\n\
  374.   -V, --version-control WORD   override the usual version control\n\
  375.       --help                   display this help and exit\n\
  376.       --version                output version information and exit\n\
  377. \n\
  378. The backup suffix is ~, unless set with SIMPLE_BACKUP_SUFFIX.  The\n\
  379. version control may be set with VERSION_CONTROL, values are:\n\
  380. \n\
  381.   t, numbered     make numbered backups\n\
  382.   nil, existing   numbered if numbered backups exist, simple otherwise\n\
  383.   never, simple   always make simple backups\n");
  384.     }
  385.   exit (status);
  386. }
  387.