home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / fileutils-3.6 / src / ln.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-14  |  7.5 KB  |  320 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. #include <stdio.h>
  24. #include <sys/types.h>
  25. #include <getopt.h>
  26. #include "system.h"
  27. #include "backupfile.h"
  28. #include "version.h"
  29.  
  30. int link ();            /* Some systems don't declare this anywhere. */
  31.  
  32. #ifdef S_ISLNK
  33. int symlink ();
  34. #endif
  35.  
  36. char *basename ();
  37. enum backup_type get_version ();
  38. int isdir ();
  39. int yesno ();
  40. void error ();
  41. void strip_trailing_slashes ();
  42.  
  43. static void usage ();
  44. static int do_link ();
  45.  
  46. /* The name by which the program was run, for error messages.  */
  47. char *program_name;
  48.  
  49. /* A pointer to the function used to make links.  This will point to either
  50.    `link' or `symlink'. */
  51. static int (*linkfunc) ();
  52.  
  53. /* If nonzero, make symbolic links; otherwise, make hard links.  */
  54. static int symbolic_link;
  55.  
  56. /* If nonzero, ask the user before removing existing files.  */
  57. static int interactive;
  58.  
  59. /* If nonzero, remove existing files unconditionally.  */
  60. static int remove_existing_files;
  61.  
  62. /* If nonzero, list each file as it is moved. */
  63. static int verbose;
  64.  
  65. /* If nonzero, allow the superuser to make hard links to directories. */
  66. static int hard_dir_link;
  67.  
  68. /* If non-zero, display usage information and exit.  */
  69. static int flag_help;
  70.  
  71. /* If non-zero, print the version on standard error.  */
  72. static int flag_version;
  73.  
  74. static struct option const long_options[] = 
  75. {
  76.   {"backup", no_argument, NULL, 'b'},
  77.   {"directory", no_argument, &hard_dir_link, 1},
  78.   {"force", no_argument, NULL, 'f'},
  79.   {"interactive", no_argument, NULL, 'i'},
  80.   {"suffix", required_argument, NULL, 'S'},
  81.   {"symbolic", no_argument, &symbolic_link, 1},
  82.   {"verbose", no_argument, &verbose, 1},
  83.   {"version-control", required_argument, NULL, 'V'},
  84.   {"help", no_argument, &flag_help, 1},
  85.   {"version", no_argument, &flag_version, 1},
  86.   {NULL, 0, NULL, 0}
  87. };
  88.  
  89. void
  90. main (argc, argv)
  91.      int argc;
  92.      char **argv;
  93. {
  94.   int c;
  95.   int errors;
  96.   int make_backups = 0;
  97.   char *version;
  98.  
  99.   version = getenv ("SIMPLE_BACKUP_SUFFIX");
  100.   if (version)
  101.     simple_backup_suffix = version;
  102.   version = getenv ("VERSION_CONTROL");
  103.   program_name = argv[0];
  104.   linkfunc = link;
  105.   symbolic_link = remove_existing_files = interactive = verbose
  106.     = hard_dir_link = 0;
  107.   errors = 0;
  108.  
  109.   while ((c = getopt_long (argc, argv, "bdfisvFS:V:", long_options, (int *) 0))
  110.      != EOF)
  111.     {
  112.       switch (c)
  113.     {
  114.     case 0:            /* Long-named option. */
  115.        break;
  116.     case 'b':
  117.       make_backups = 1;
  118.       break;
  119.     case 'd':
  120.     case 'F':
  121.       hard_dir_link = 1;
  122.       break;
  123.     case 'f':
  124.       remove_existing_files = 1;
  125.       interactive = 0;
  126.       break;
  127.     case 'i':
  128.       remove_existing_files = 0;
  129.       interactive = 1;
  130.       break;
  131.     case 's':
  132. #ifdef S_ISLNK
  133.       symbolic_link = 1;
  134. #else
  135.       error (1, 0, "symbolic links are not supported on this system");
  136. #endif
  137.       break;
  138.     case 'v':
  139.       verbose = 1;
  140.       break;
  141.     case 'S':
  142.       simple_backup_suffix = optarg;
  143.       break;
  144.     case 'V':
  145.       version = optarg;
  146.       break;
  147.     default:
  148.       usage ();
  149.       break;
  150.     }
  151.     }
  152.  
  153.   if (flag_version)
  154.     {
  155.       fprintf (stderr, "%s\n", version_string);
  156.       exit (0);
  157.     }
  158.  
  159.   if (flag_help)
  160.     usage ();
  161.  
  162.   if (optind == argc)
  163.     usage ();
  164.  
  165.   if (make_backups)
  166.     backup_type = get_version (version);
  167.  
  168. #ifdef S_ISLNK
  169.   if (symbolic_link)
  170.     linkfunc = symlink;
  171. #endif
  172.  
  173.   if (optind == argc - 1)
  174.     errors = do_link (argv[optind], ".");
  175.   else if (optind == argc - 2)
  176.     {
  177.       errors = do_link (argv[optind], argv[optind + 1]);
  178.     }
  179.   else
  180.     {
  181.       char *to;
  182.  
  183.       to = argv[argc - 1];
  184.       if (!isdir (to))
  185.     error (1, 0, "when making multiple links, last argument must be a directory");
  186.       for (; optind < argc - 1; ++optind)
  187.     errors += do_link (argv[optind], to);
  188.     }
  189.  
  190.   exit (errors != 0);
  191. }
  192.  
  193. /* Make a link DEST to existing file SOURCE.
  194.    If DEST is a directory, put the link to SOURCE in that directory.
  195.    Return 1 if there is an error, otherwise 0.  */
  196.  
  197. static int
  198. do_link (source, dest)
  199.      char *source;
  200.      char *dest;
  201. {
  202.   struct stat dest_stats;
  203.   char *dest_backup = NULL;
  204.  
  205.   /* isdir uses stat instead of lstat.
  206.      On SVR4, link does not follow symlinks, so this check disallows
  207.      making hard links to symlinks that point to directories.  Big deal.
  208.      On other systems, link follows symlinks, so this check is right.  */
  209.   if (!symbolic_link && !hard_dir_link && isdir (source))
  210.     {
  211.       error (0, 0, "%s: hard link not allowed for directory", source);
  212.       return 1;
  213.     }
  214.   if (isdir (dest))
  215.     {
  216.       /* Target is a directory; build the full filename. */
  217.       char *new_dest;
  218.       char *source_base;
  219.       char *tmp_source;
  220.  
  221.       tmp_source = (char *) alloca (strlen (source) + 1);
  222.       strcpy (tmp_source, source);
  223.       strip_trailing_slashes (tmp_source);
  224.  
  225.       source_base = basename (tmp_source);
  226.       new_dest = (char *)
  227.     alloca (strlen (source_base) + 1 + strlen (dest) + 1);
  228.       sprintf (new_dest, "%s/%s", dest, source_base);
  229.       dest = new_dest;
  230.     }
  231.  
  232.   if (lstat (dest, &dest_stats) == 0)
  233.     {
  234.       if (S_ISDIR (dest_stats.st_mode))
  235.     {
  236.       error (0, 0, "%s: cannot overwrite directory", dest);
  237.       return 1;
  238.     }
  239.       if (interactive)
  240.     {
  241.       fprintf (stderr, "%s: replace `%s'? ", program_name, dest);
  242.       if (!yesno ())
  243.         return 0;
  244.     }
  245.       else if (!remove_existing_files)
  246.     {
  247.       error (0, 0, "%s: File exists", dest);
  248.       return 1;
  249.     }
  250.  
  251.       if (backup_type != none)
  252.     {
  253.       char *tmp_backup = find_backup_file_name (dest);
  254.       if (tmp_backup == NULL)
  255.         error (1, 0, "virtual memory exhausted");
  256.       dest_backup = (char *) alloca (strlen (tmp_backup) + 1);
  257.       strcpy (dest_backup, tmp_backup);
  258.       free (tmp_backup);
  259.       if (rename (dest, dest_backup))
  260.         {
  261.           if (errno != ENOENT)
  262.         {
  263.           error (0, errno, "cannot backup `%s'", dest);
  264.           return 1;
  265.         }
  266.           else
  267.         dest_backup = NULL;
  268.         }
  269.     }
  270.       else if (unlink (dest) && errno != ENOENT)
  271.     {
  272.       error (0, errno, "cannot remove old link to `%s'", dest);
  273.       return 1;
  274.     }
  275.     }
  276.   else if (errno != ENOENT)
  277.     {
  278.       error (0, errno, "%s", dest);
  279.       return 1;
  280.     }
  281.        
  282.   if (verbose)
  283.     printf ("%s -> %s\n", source, dest);
  284.  
  285.   if ((*linkfunc) (source, dest) == 0)
  286.     {
  287.       return 0;
  288.     }
  289.  
  290.   error (0, errno, "cannot %slink `%s' to `%s'",
  291. #ifdef S_ISLNK
  292.          linkfunc == symlink ? "symbolic " : "",
  293. #else
  294.          "",
  295. #endif
  296.          source, dest);
  297.  
  298.   if (dest_backup)
  299.     {
  300.       if (rename (dest_backup, dest))
  301.     error (0, errno, "cannot un-backup `%s'", dest);
  302.     }
  303.   return 1;
  304. }
  305.  
  306. static void
  307. usage ()
  308. {
  309.   fprintf (stderr, "\
  310. Usage: %s [options] source [dest]\n\
  311.        %s [options] source... directory\n\
  312. Options:\n\
  313.        [-bdfisvF] [-S backup-suffix] [-V {numbered,existing,simple}]\n\
  314.        [--version-control={numbered,existing,simple}] [--backup] [--directory]\n\
  315.        [--force] [--interactive] [--symbolic] [--verbose]\n\
  316.        [--suffix=backup-suffix] [--help] [--version]\n",
  317.        program_name, program_name);
  318.   exit (1);
  319. }
  320.