1 /* `ln' program to create links between files.
2 Copyright (C) 1986, 1989, 1990, 1991, 1995 Free Software Foundation, Inc.
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)
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.
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. */
18 /* Written by Mike Parker and David MacKenzie. */
26 #include <sys/types.h>
30 #include "backupfile.h"
34 int link (); /* Some systems don't declare this anywhere. */
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. */
43 #define PATH_BASENAME_CONCAT(new_dest, dest, source) \
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); \
54 (new_dest) = (char *) alloca (strlen ((dest)) + 1 \
55 + strlen (source_base) + 1); \
56 stpcpy (stpcpy (stpcpy ((new_dest), (dest)), "/"), source_base);\
61 enum backup_type get_version ();
64 void strip_trailing_slashes ();
68 static int do_link ();
70 /* The name by which the program was run, for error messages. */
73 /* A pointer to the function used to make links. This will point to either
74 `link' or `symlink'. */
75 static int (*linkfunc) ();
77 /* If nonzero, make symbolic links; otherwise, make hard links. */
78 static int symbolic_link;
80 /* If nonzero, ask the user before removing existing files. */
81 static int interactive;
83 /* If nonzero, remove existing files unconditionally. */
84 static int remove_existing_files;
86 /* If nonzero, list each file as it is moved. */
89 /* If nonzero, allow the superuser to make hard links to directories. */
90 static int hard_dir_link;
92 /* If nonzero, and the specified destination is a symbolic link to a
93 directory, treat it just as if it were a directory. Otherwise, the
94 command `ln --force --no-dereference file symlink-to-dir' deletes
95 symlink-to-dir before creating the new link. */
96 static int dereference_dest_dir_symlinks = 1;
98 /* If non-zero, display usage information and exit. */
101 /* If non-zero, print the version on standard output and exit. */
102 static int show_version;
104 static struct option const long_options[] =
106 {"backup", no_argument, NULL, 'b'},
107 {"directory", no_argument, &hard_dir_link, 1},
108 {"no-dereference", no_argument, NULL, 'n'},
109 {"force", no_argument, NULL, 'f'},
110 {"interactive", no_argument, NULL, 'i'},
111 {"suffix", required_argument, NULL, 'S'},
112 {"symbolic", no_argument, &symbolic_link, 1},
113 {"verbose", no_argument, &verbose, 1},
114 {"version-control", required_argument, NULL, 'V'},
115 {"help", no_argument, &show_help, 1},
116 {"version", no_argument, &show_version, 1},
127 int make_backups = 0;
130 version = getenv ("SIMPLE_BACKUP_SUFFIX");
132 simple_backup_suffix = version;
133 version = getenv ("VERSION_CONTROL");
134 program_name = argv[0];
136 symbolic_link = remove_existing_files = interactive = verbose
140 while ((c = getopt_long (argc, argv, "bdfinsvFS:V:", long_options, (int *) 0))
145 case 0: /* Long-named option. */
155 remove_existing_files = 1;
159 remove_existing_files = 0;
163 dereference_dest_dir_symlinks = 0;
169 error (1, 0, "symbolic links are not supported on this system");
176 simple_backup_suffix = optarg;
189 printf ("%s\n", version_string);
198 error (0, 0, "missing file argument");
203 backup_type = get_version (version);
210 if (optind == argc - 1)
211 errors = do_link (argv[optind], ".");
212 else if (optind == argc - 2)
214 struct stat source_stats;
219 source = argv[optind];
220 dest = argv[optind + 1];
222 /* When the destination is specified with a trailing slash and the
223 source exists but is not a directory, convert the user's command
224 `ln source dest/' to `ln source dest/basename(source)'. */
226 if (dest[strlen (dest) - 1] == '/'
227 && lstat (source, &source_stats) == 0
228 && !S_ISDIR (source_stats.st_mode))
230 PATH_BASENAME_CONCAT (new_dest, dest, source);
237 errors = do_link (source, new_dest);
245 error (1, 0, "when making multiple links, last argument must be a directory");
246 for (; optind < argc - 1; ++optind)
247 errors += do_link (argv[optind], to);
253 /* Make a link DEST to the (usually) existing file SOURCE.
254 Symbolic links to nonexistent files are allowed.
255 If DEST is a directory, put the link to SOURCE in that directory.
256 Return 1 if there is an error, otherwise 0. */
259 do_link (source, dest)
263 struct stat dest_stats;
264 char *dest_backup = NULL;
267 /* Use stat here instead of lstat.
268 On SVR4, link does not follow symlinks, so this check disallows
269 making hard links to symlinks that point to directories. Big deal.
270 On other systems, link follows symlinks, so this check is right. */
273 struct stat source_stats;
275 if (stat (source, &source_stats) != 0)
277 error (0, errno, "%s", source);
280 if (!hard_dir_link && S_ISDIR (source_stats.st_mode))
282 error (0, 0, "%s: hard link not allowed for directory", source);
287 if (lstat (dest, &dest_stats) != 0 && errno != ENOENT)
289 error (0, errno, "%s", dest);
293 /* If the destination is a directory or (it is a symlink to a directory
294 and the user has not specified --no-dereference), then form the
295 actual destination name by appending basename (source) to the
296 specified destination directory. */
297 lstat_status = lstat (dest, &dest_stats);
299 if (lstat_status != 0 && errno != ENOENT)
301 error (0, errno, "%s", dest);
305 if ((lstat_status == 0
306 && S_ISDIR (dest_stats.st_mode))
308 || (dereference_dest_dir_symlinks
309 && (S_ISLNK (dest_stats.st_mode)
314 /* Target is a directory; build the full filename. */
316 PATH_BASENAME_CONCAT (new_dest, dest, source);
318 /* Set this to non-zero to force another call to lstat
319 with the new destination. */
323 if (lstat_status == 0 || lstat (dest, &dest_stats) == 0)
325 if (S_ISDIR (dest_stats.st_mode))
327 error (0, 0, "%s: cannot overwrite directory", dest);
332 fprintf (stderr, "%s: replace `%s'? ", program_name, dest);
336 else if (!remove_existing_files)
338 error (0, 0, "%s: File exists", dest);
342 if (backup_type != none)
344 char *tmp_backup = find_backup_file_name (dest);
345 if (tmp_backup == NULL)
346 error (1, 0, "virtual memory exhausted");
347 dest_backup = (char *) alloca (strlen (tmp_backup) + 1);
348 strcpy (dest_backup, tmp_backup);
350 if (rename (dest, dest_backup))
354 error (0, errno, "cannot backup `%s'", dest);
361 else if (unlink (dest) && errno != ENOENT)
363 error (0, errno, "cannot remove `%s'", dest);
367 else if (errno != ENOENT)
369 error (0, errno, "%s", dest);
374 printf ("%s -> %s\n", source, dest);
376 if ((*linkfunc) (source, dest) == 0)
381 error (0, errno, "cannot %slink `%s' to `%s'",
383 symbolic_link ? "symbolic " : "",
391 if (rename (dest_backup, dest))
392 error (0, errno, "cannot un-backup `%s'", dest);
402 fprintf (stderr, "Try `%s --help' for more information.\n",
407 Usage: %s [OPTION]... SOURCE [DEST]\n\
408 or: %s [OPTION]... SOURCE... DIRECTORY\n\
410 program_name, program_name);
413 -b, --backup make backups for removed files\n\
414 -d, -F, --directory hard link directories (super-user only)\n\
415 -f, --force remove existing destinations\n\
416 -n, --no-dereference with --force, remove destination that is a\n\
417 symlink to a directory\n\
418 -i, --interactive prompt whether to remove destinations\n\
419 -s, --symbolic make symbolic links, instead of hard links\n\
420 -v, --verbose print name of each file before linking\n\
421 -S, --suffix=SUFFIX override the usual backup suffix\n\
422 -V, --version-control=WORD override the usual version control\n\
423 --help display this help and exit\n\
424 --version output version information and exit\n\
426 The backup suffix is ~, unless set with SIMPLE_BACKUP_SUFFIX. The\n\
427 version control may be set with VERSION_CONTROL, values are:\n\
429 t, numbered make numbered backups\n\
430 nil, existing numbered if numbered backups exist, simple otherwise\n\
431 never, simple always make simple backups\n");