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"
32 #include "safe-lstat.h"
33 #include "safe-stat.h"
36 int link (); /* Some systems don't declare this anywhere. */
42 /* Construct a string NEW_DEST by concatenating DEST, a slash, and
43 basename(SOURCE) in alloca'd memory. Don't modify DEST or SOURCE. */
45 #define PATH_BASENAME_CONCAT(new_dest, dest, source) \
51 tmp_source = (char *) alloca (strlen ((source)) + 1); \
52 strcpy (tmp_source, (source)); \
53 strip_trailing_slashes (tmp_source); \
54 source_base = basename (tmp_source); \
56 (new_dest) = (char *) alloca (strlen ((dest)) + 1 \
57 + strlen (source_base) + 1); \
58 stpcpy (stpcpy (stpcpy ((new_dest), (dest)), "/"), source_base);\
63 enum backup_type get_version ();
66 void strip_trailing_slashes ();
70 static int do_link ();
72 /* The name by which the program was run, for error messages. */
75 /* A pointer to the function used to make links. This will point to either
76 `link' or `symlink'. */
77 static int (*linkfunc) ();
79 /* If nonzero, make symbolic links; otherwise, make hard links. */
80 static int symbolic_link;
82 /* If nonzero, ask the user before removing existing files. */
83 static int interactive;
85 /* If nonzero, remove existing files unconditionally. */
86 static int remove_existing_files;
88 /* If nonzero, list each file as it is moved. */
91 /* If nonzero, allow the superuser to make hard links to directories. */
92 static int hard_dir_link;
94 /* If nonzero, and the specified destination is a symbolic link to a
95 directory, treat it just as if it were a directory. Otherwise, the
96 command `ln --force --no-dereference file symlink-to-dir' deletes
97 symlink-to-dir before creating the new link. */
98 static int dereference_dest_dir_symlinks = 1;
100 /* If non-zero, display usage information and exit. */
101 static int show_help;
103 /* If non-zero, print the version on standard output and exit. */
104 static int show_version;
106 static struct option const long_options[] =
108 {"backup", no_argument, NULL, 'b'},
109 {"directory", no_argument, &hard_dir_link, 1},
110 {"no-dereference", no_argument, NULL, 'n'},
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},
129 int make_backups = 0;
132 version = getenv ("SIMPLE_BACKUP_SUFFIX");
134 simple_backup_suffix = version;
135 version = getenv ("VERSION_CONTROL");
136 program_name = argv[0];
138 symbolic_link = remove_existing_files = interactive = verbose
142 while ((c = getopt_long (argc, argv, "bdfinsvFS:V:", long_options, (int *) 0))
147 case 0: /* Long-named option. */
157 remove_existing_files = 1;
161 remove_existing_files = 0;
165 dereference_dest_dir_symlinks = 0;
171 error (1, 0, "symbolic links are not supported on this system");
178 simple_backup_suffix = optarg;
191 printf ("%s\n", version_string);
200 error (0, 0, "missing file argument");
205 backup_type = get_version (version);
212 if (optind == argc - 1)
213 errors = do_link (argv[optind], ".");
214 else if (optind == argc - 2)
216 struct stat source_stats;
221 source = argv[optind];
222 dest = argv[optind + 1];
224 /* When the destination is specified with a trailing slash and the
225 source exists but is not a directory, convert the user's command
226 `ln source dest/' to `ln source dest/basename(source)'. */
228 if (dest[strlen (dest) - 1] == '/'
229 && safe_lstat (source, &source_stats) == 0
230 && !S_ISDIR (source_stats.st_mode))
232 PATH_BASENAME_CONCAT (new_dest, dest, source);
239 errors = do_link (source, new_dest);
247 error (1, 0, "when making multiple links, last argument must be a directory");
248 for (; optind < argc - 1; ++optind)
249 errors += do_link (argv[optind], to);
255 /* Make a link DEST to the (usually) existing file SOURCE.
256 Symbolic links to nonexistent files are allowed.
257 If DEST is a directory, put the link to SOURCE in that directory.
258 Return 1 if there is an error, otherwise 0. */
261 do_link (source, dest)
265 struct stat dest_stats;
266 char *dest_backup = NULL;
269 /* Use stat here instead of lstat.
270 On SVR4, link does not follow symlinks, so this check disallows
271 making hard links to symlinks that point to directories. Big deal.
272 On other systems, link follows symlinks, so this check is right. */
275 struct stat source_stats;
277 if (safe_stat (source, &source_stats) != 0)
279 error (0, errno, "%s", source);
282 if (!hard_dir_link && S_ISDIR (source_stats.st_mode))
284 error (0, 0, "%s: hard link not allowed for directory", source);
289 if (safe_lstat (dest, &dest_stats) != 0 && errno != ENOENT)
291 error (0, errno, "%s", dest);
295 /* If the destination is a directory or (it is a symlink to a directory
296 and the user has not specified --no-dereference), then form the
297 actual destination name by appending basename (source) to the
298 specified destination directory. */
299 lstat_status = safe_lstat (dest, &dest_stats);
301 if (lstat_status != 0 && errno != ENOENT)
303 error (0, errno, "%s", dest);
307 if ((lstat_status == 0
308 && S_ISDIR (dest_stats.st_mode))
310 || (dereference_dest_dir_symlinks
311 && (S_ISLNK (dest_stats.st_mode)
316 /* Target is a directory; build the full filename. */
318 PATH_BASENAME_CONCAT (new_dest, dest, source);
320 /* Set this to non-zero to force another call to safe_lstat
321 with the new destination. */
325 if (lstat_status == 0 || safe_lstat (dest, &dest_stats) == 0)
327 if (S_ISDIR (dest_stats.st_mode))
329 error (0, 0, "%s: cannot overwrite directory", dest);
334 fprintf (stderr, "%s: replace `%s'? ", program_name, dest);
338 else if (!remove_existing_files)
340 error (0, 0, "%s: File exists", dest);
344 if (backup_type != none)
346 char *tmp_backup = find_backup_file_name (dest);
347 if (tmp_backup == NULL)
348 error (1, 0, "virtual memory exhausted");
349 dest_backup = (char *) alloca (strlen (tmp_backup) + 1);
350 strcpy (dest_backup, tmp_backup);
352 if (rename (dest, dest_backup))
356 error (0, errno, "cannot backup `%s'", dest);
363 else if (unlink (dest) && errno != ENOENT)
365 error (0, errno, "cannot remove `%s'", dest);
369 else if (errno != ENOENT)
371 error (0, errno, "%s", dest);
376 printf ("%s -> %s\n", source, dest);
378 if ((*linkfunc) (source, dest) == 0)
383 error (0, errno, "cannot %slink `%s' to `%s'",
385 symbolic_link ? "symbolic " : "",
393 if (rename (dest_backup, dest))
394 error (0, errno, "cannot un-backup `%s'", dest);
404 fprintf (stderr, "Try `%s --help' for more information.\n",
409 Usage: %s [OPTION]... SOURCE [DEST]\n\
410 or: %s [OPTION]... SOURCE... DIRECTORY\n\
412 program_name, program_name);
415 -b, --backup make backups for removed files\n\
416 -d, -F, --directory hard link directories (super-user only)\n\
417 -f, --force remove existing destinations\n\
418 -n, --no-dereference with --force, remove destination that is a\n\
419 symlink to a directory\n\
420 -i, --interactive prompt whether to remove destinations\n\
421 -s, --symbolic make symbolic links, instead of hard links\n\
422 -v, --verbose print name of each file before linking\n\
423 -S, --suffix=SUFFIX override the usual backup suffix\n\
424 -V, --version-control=WORD override the usual version control\n\
425 --help display this help and exit\n\
426 --version output version information and exit\n\
428 The backup suffix is ~, unless set with SIMPLE_BACKUP_SUFFIX. The\n\
429 version control may be set with VERSION_CONTROL, values are:\n\
431 t, numbered make numbered backups\n\
432 nil, existing numbered if numbered backups exist, simple otherwise\n\
433 never, simple always make simple backups\n");