1 /* `ln' program to create links between files.
2 Copyright (C) 86, 89, 90, 91, 1995-2003 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Mike Parker and David MacKenzie. */
26 #include <sys/types.h>
31 #include "backupfile.h"
36 /* The official name of this program (e.g., no `g' prefix). */
37 #define PROGRAM_NAME "ln"
39 #define AUTHORS N_ ("Mike Parker and David MacKenzie")
41 #ifndef ENABLE_HARD_LINK_TO_SYMLINK_WARNING
42 # define ENABLE_HARD_LINK_TO_SYMLINK_WARNING 0
45 /* For long options that have no equivalent short option, use a
46 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
49 TARGET_DIRECTORY_OPTION = CHAR_MAX + 1
52 int link (); /* Some systems don't declare this anywhere. */
58 /* In being careful not even to try to make hard links to directories,
59 we have to know whether link(2) follows symlinks. If it does, then
60 we have to *stat* the `source' to see if the resulting link would be
61 to a directory. Otherwise, we have to use *lstat* so that we allow
62 users to make hard links to symlinks-that-point-to-directories. */
64 #if LINK_FOLLOWS_SYMLINKS
65 # define STAT_LIKE_LINK(File, Stat_buf) \
68 # define STAT_LIKE_LINK(File, Stat_buf) \
69 lstat (File, Stat_buf)
72 /* Construct a string NEW_DEST by concatenating DEST, a slash, and
73 basename(SOURCE) in alloca'd memory. Don't modify DEST or SOURCE. */
75 #define PATH_BASENAME_CONCAT(new_dest, dest, source) \
78 const char *source_base; \
81 tmp_source = (char *) alloca (strlen ((source)) + 1); \
82 strcpy (tmp_source, (source)); \
83 strip_trailing_slashes (tmp_source); \
84 source_base = base_name (tmp_source); \
86 (new_dest) = (char *) alloca (strlen ((dest)) + 1 \
87 + strlen (source_base) + 1); \
88 stpcpy (stpcpy (stpcpy ((new_dest), (dest)), "/"), source_base);\
95 /* The name by which the program was run, for error messages. */
99 enum backup_type backup_type;
101 /* A pointer to the function used to make links. This will point to either
102 `link' or `symlink'. */
103 static int (*linkfunc) ();
105 /* If nonzero, make symbolic links; otherwise, make hard links. */
106 static int symbolic_link;
108 /* If nonzero, ask the user before removing existing files. */
109 static int interactive;
111 /* If nonzero, remove existing files unconditionally. */
112 static int remove_existing_files;
114 /* If nonzero, list each file as it is moved. */
117 /* If nonzero, allow the superuser to make hard links to directories. */
118 static int hard_dir_link;
120 /* If nonzero, and the specified destination is a symbolic link to a
121 directory, treat it just as if it were a directory. Otherwise, the
122 command `ln --force --no-dereference file symlink-to-dir' deletes
123 symlink-to-dir before creating the new link. */
124 static int dereference_dest_dir_symlinks = 1;
126 static struct option const long_options[] =
128 {"backup", optional_argument, NULL, 'b'},
129 {"directory", no_argument, NULL, 'F'},
130 {"no-dereference", no_argument, NULL, 'n'},
131 {"force", no_argument, NULL, 'f'},
132 {"interactive", no_argument, NULL, 'i'},
133 {"suffix", required_argument, NULL, 'S'},
134 {"target-directory", required_argument, NULL, TARGET_DIRECTORY_OPTION},
135 {"symbolic", no_argument, NULL, 's'},
136 {"verbose", no_argument, NULL, 'v'},
137 {"version-control", required_argument, NULL, 'V'}, /* Deprecated. FIXME. */
138 {GETOPT_HELP_OPTION_DECL},
139 {GETOPT_VERSION_OPTION_DECL},
143 /* Make a link DEST to the (usually) existing file SOURCE.
144 Symbolic links to nonexistent files are allowed.
145 If DEST is a directory, put the link to SOURCE in that directory.
146 Return 1 if there is an error, otherwise 0. */
149 do_link (const char *source, const char *dest)
151 struct stat source_stats;
152 struct stat dest_stats;
153 char *dest_backup = NULL;
155 int backup_succeeded = 0;
157 /* Use stat here instead of lstat.
158 On SVR4, link does not follow symlinks, so this check disallows
159 making hard links to symlinks that point to directories. Big deal.
160 On other systems, link follows symlinks, so this check is right. */
163 if (STAT_LIKE_LINK (source, &source_stats) != 0)
165 error (0, errno, _("accessing %s"), quote (source));
169 if (ENABLE_HARD_LINK_TO_SYMLINK_WARNING
170 && S_ISLNK (source_stats.st_mode))
172 error (0, 0, _("%s: warning: making a hard link to a symbolic link\
177 if (!hard_dir_link && S_ISDIR (source_stats.st_mode))
179 error (0, 0, _("%s: hard link not allowed for directory"),
185 lstat_status = lstat (dest, &dest_stats);
186 if (lstat_status != 0 && errno != ENOENT)
188 error (0, errno, _("accessing %s"), quote (dest));
192 /* If the destination is a directory or (it is a symlink to a directory
193 and the user has not specified --no-dereference), then form the
194 actual destination name by appending base_name (source) to the
195 specified destination directory. */
196 if ((lstat_status == 0
197 && S_ISDIR (dest_stats.st_mode))
199 || (dereference_dest_dir_symlinks
200 && (lstat_status == 0
201 && S_ISLNK (dest_stats.st_mode)
206 /* Target is a directory; build the full filename. */
208 PATH_BASENAME_CONCAT (new_dest, dest, source);
211 /* Get stats for new DEST. */
212 lstat_status = lstat (dest, &dest_stats);
213 if (lstat_status != 0 && errno != ENOENT)
215 error (0, errno, _("accessing %s"), quote (dest));
220 /* If --force (-f) has been specified without --backup, then before
221 making a link ln must remove the destination file if it exists.
222 (with --backup, it just renames any existing destination file)
223 But if the source and destination are the same, don't remove
224 anything and fail right here. */
225 if (remove_existing_files
227 /* Allow `ln -sf --backup k k' to succeed in creating the
228 self-referential symlink, but don't allow the hard-linking
229 equivalent: `ln -f k k' (with or without --backup) to get
230 beyond this point, because the error message you'd get is
232 && (backup_type == none || !symbolic_link)
233 && (!symbolic_link || stat (source, &source_stats) == 0)
234 && source_stats.st_dev == dest_stats.st_dev
235 && source_stats.st_ino == dest_stats.st_ino
236 /* The following detects whether removing DEST will also remove
237 SOURCE. If the file has only one link then both are surely
238 the same link. Otherwise check whether they point to the same
239 name in the same directory. */
240 && (source_stats.st_nlink == 1 || same_name (source, dest)))
242 error (0, 0, _("%s and %s are the same file"),
243 quote_n (0, source), quote_n (1, dest));
247 if (lstat_status == 0 || lstat (dest, &dest_stats) == 0)
249 if (S_ISDIR (dest_stats.st_mode))
251 error (0, 0, _("%s: cannot overwrite directory"), quote (dest));
256 fprintf (stderr, _("%s: replace %s? "), program_name, quote (dest));
260 else if (!remove_existing_files && backup_type == none)
262 error (0, 0, _("%s: File exists"), quote (dest));
266 if (backup_type != none)
268 char *tmp_backup = find_backup_file_name (dest, backup_type);
269 if (tmp_backup == NULL)
271 dest_backup = (char *) alloca (strlen (tmp_backup) + 1);
272 strcpy (dest_backup, tmp_backup);
274 if (rename (dest, dest_backup))
278 error (0, errno, _("cannot backup %s"), quote (dest));
286 backup_succeeded = 1;
290 /* Try to unlink DEST even if we may have renamed it. In some unusual
291 cases (when DEST and DEST_BACKUP are hard-links that refer to the
292 same file), rename succeeds and DEST remains. If we didn't remove
293 DEST in that case, the subsequent LINKFUNC call would fail. */
294 if (unlink (dest) && errno != ENOENT)
296 error (0, errno, _("cannot remove %s"), quote (dest));
300 else if (errno != ENOENT)
302 error (0, errno, _("accessing %s"), quote (dest));
308 printf ((symbolic_link
309 ? _("create symbolic link %s to %s")
310 : _("create hard link %s to %s")),
311 quote_n (0, dest), quote_n (1, source));
312 if (backup_succeeded)
313 printf (_(" (backup: %s)"), quote (dest_backup));
317 if ((*linkfunc) (source, dest) == 0)
324 ? _("creating symbolic link %s to %s")
325 : _("creating hard link %s to %s")),
326 quote_n (0, dest), quote_n (1, source));
330 if (rename (dest_backup, dest))
331 error (0, errno, _("cannot un-backup %s"), quote (dest));
340 fprintf (stderr, _("Try `%s --help' for more information.\n"),
345 Usage: %s [OPTION]... TARGET [LINK_NAME]\n\
346 or: %s [OPTION]... TARGET... DIRECTORY\n\
347 or: %s [OPTION]... --target-directory=DIRECTORY TARGET...\n\
349 program_name, program_name, program_name);
351 Create a link to the specified TARGET with optional LINK_NAME.\n\
352 If LINK_NAME is omitted, a link with the same basename as the TARGET is\n\
353 created in the current directory. When using the second form with more\n\
354 than one TARGET, the last argument must be a directory; create links\n\
355 in DIRECTORY to each TARGET. Create hard links by default, symbolic\n\
356 links with --symbolic. When creating hard links, each TARGET must exist.\n\
360 Mandatory arguments to long options are mandatory for short options too.\n\
363 --backup[=CONTROL] make a backup of each existing destination file\n\
364 -b like --backup but does not accept an argument\n\
365 -d, -F, --directory hard link directories (super-user only)\n\
366 -f, --force remove existing destination files\n\
369 -n, --no-dereference treat destination that is a symlink to a\n\
370 directory as if it were a normal file\n\
371 -i, --interactive prompt whether to remove destinations\n\
372 -s, --symbolic make symbolic links instead of hard links\n\
375 -S, --suffix=SUFFIX override the usual backup suffix\n\
376 --target-directory=DIRECTORY specify the DIRECTORY in which to create\n\
378 -v, --verbose print name of each file before linking\n\
380 fputs (HELP_OPTION_DESCRIPTION, stdout);
381 fputs (VERSION_OPTION_DESCRIPTION, stdout);
384 The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
385 The version control method may be selected via the --backup option or through\n\
386 the VERSION_CONTROL environment variable. Here are the values:\n\
390 none, off never make backups (even if --backup is given)\n\
391 numbered, t make numbered backups\n\
392 existing, nil numbered if numbered backups exist, simple otherwise\n\
393 simple, never always make simple backups\n\
395 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
401 main (int argc, char **argv)
405 int make_backups = 0;
406 char *backup_suffix_string;
407 char *version_control_string = NULL;
408 char *target_directory = NULL;
409 int target_directory_specified;
410 unsigned int n_files;
414 initialize_main (&argc, &argv);
415 program_name = argv[0];
416 setlocale (LC_ALL, "");
417 bindtextdomain (PACKAGE, LOCALEDIR);
418 textdomain (PACKAGE);
420 atexit (close_stdout);
422 /* FIXME: consider not calling getenv for SIMPLE_BACKUP_SUFFIX unless
423 we'll actually use backup_suffix_string. */
424 backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
426 symbolic_link = remove_existing_files = interactive = verbose
430 while ((c = getopt_long (argc, argv, "bdfinsvFS:V:", long_options, NULL))
435 case 0: /* Long-named option. */
438 case 'V': /* FIXME: this is deprecated. Remove it in 2001. */
440 _("warning: --version-control (-V) is obsolete; support for\
441 it\nwill be removed in some future release. Use --backup=%s instead."
448 version_control_string = optarg;
455 remove_existing_files = 1;
459 remove_existing_files = 0;
463 dereference_dest_dir_symlinks = 0;
469 error (EXIT_FAILURE, 0,
470 _("symbolic links are not supported on this system"));
473 case TARGET_DIRECTORY_OPTION:
474 target_directory = optarg;
481 backup_suffix_string = optarg;
483 case_GETOPT_HELP_CHAR;
484 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
486 usage (EXIT_FAILURE);
493 error (0, 0, _("missing file argument"));
494 usage (EXIT_FAILURE);
497 n_files = argc - optind;
498 file = argv + optind;
500 target_directory_specified = (target_directory != NULL);
501 if (!target_directory)
502 target_directory = file[n_files - 1];
504 /* If target directory is not specified, and there's only one
505 file argument, then pretend `.' was given as the second argument. */
506 if (!target_directory_specified && n_files == 1)
508 static char *dummy[2];
517 dest_is_dir = isdir (target_directory);
525 if (target_directory_specified && !dest_is_dir)
527 error (0, 0, _("%s: specified target directory is not a directory"),
528 quote (target_directory));
529 usage (EXIT_FAILURE);
532 if (backup_suffix_string)
533 simple_backup_suffix = xstrdup (backup_suffix_string);
535 backup_type = (make_backups
536 ? xget_version (_("backup type"), version_control_string)
539 if (target_directory_specified || n_files > 2)
542 unsigned int last_file_idx = (target_directory_specified
546 if (!target_directory_specified && !dest_is_dir)
547 error (EXIT_FAILURE, 0,
548 _("when making multiple links, last argument must be a directory"));
549 for (i = 0; i <= last_file_idx; ++i)
550 errors += do_link (file[i], target_directory);
554 struct stat source_stats;
562 /* When the destination is specified with a trailing slash and the
563 source exists but is not a directory, convert the user's command
564 `ln source dest/' to `ln source dest/basename(source)'. */
566 if (dest[strlen (dest) - 1] == '/'
567 && lstat (source, &source_stats) == 0
568 && !S_ISDIR (source_stats.st_mode))
570 PATH_BASENAME_CONCAT (new_dest, dest, source);
577 errors = do_link (source, new_dest);