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