1 /* `ln' program to create links between files.
2 Copyright (C) 86, 89, 90, 91, 95, 1996 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>
30 #include "backupfile.h"
33 int link (); /* Some systems don't declare this anywhere. */
39 /* Construct a string NEW_DEST by concatenating DEST, a slash, and
40 basename(SOURCE) in alloca'd memory. Don't modify DEST or SOURCE. */
42 #define PATH_BASENAME_CONCAT(new_dest, dest, source) \
48 tmp_source = (char *) alloca (strlen ((source)) + 1); \
49 strcpy (tmp_source, (source)); \
50 strip_trailing_slashes (tmp_source); \
51 source_base = basename (tmp_source); \
53 (new_dest) = (char *) alloca (strlen ((dest)) + 1 \
54 + strlen (source_base) + 1); \
55 stpcpy (stpcpy (stpcpy ((new_dest), (dest)), "/"), source_base);\
61 enum backup_type get_version ();
64 void strip_trailing_slashes ();
67 /* The name by which the program was run, for error messages. */
70 /* A pointer to the function used to make links. This will point to either
71 `link' or `symlink'. */
72 static int (*linkfunc) ();
74 /* If nonzero, make symbolic links; otherwise, make hard links. */
75 static int symbolic_link;
77 /* A string describing type of link to make. For use in verbose
78 diagnostics and in error messages. */
79 static const char *link_type_string;
81 /* If nonzero, ask the user before removing existing files. */
82 static int interactive;
84 /* If nonzero, remove existing files unconditionally. */
85 static int remove_existing_files;
87 /* If nonzero, list each file as it is moved. */
90 /* If nonzero, allow the superuser to make hard links to directories. */
91 static int hard_dir_link;
93 /* If nonzero, and the specified destination is a symbolic link to a
94 directory, treat it just as if it were a directory. Otherwise, the
95 command `ln --force --no-dereference file symlink-to-dir' deletes
96 symlink-to-dir before creating the new link. */
97 static int dereference_dest_dir_symlinks = 1;
99 /* If nonzero, display usage information and exit. */
100 static int show_help;
102 /* If nonzero, print the version on standard output and exit. */
103 static int show_version;
105 static struct option const long_options[] =
107 {"backup", no_argument, NULL, 'b'},
108 {"directory", no_argument, &hard_dir_link, 1},
109 {"no-dereference", no_argument, NULL, 'n'},
110 {"force", no_argument, NULL, 'f'},
111 {"interactive", no_argument, NULL, 'i'},
112 {"suffix", required_argument, NULL, 'S'},
113 {"symbolic", no_argument, &symbolic_link, 1},
114 {"verbose", no_argument, &verbose, 1},
115 {"version-control", required_argument, NULL, 'V'},
116 {"help", no_argument, &show_help, 1},
117 {"version", no_argument, &show_version, 1},
121 /* Return nonzero if SOURCE and DEST point to the same name in the same
125 same_name (const char *source, const char *dest)
127 struct stat source_dir_stats;
128 struct stat dest_dir_stats;
129 char *source_dirname, *dest_dirname;
131 source_dirname = dirname (source);
132 dest_dirname = dirname (dest);
133 if (source_dirname == NULL || dest_dirname == NULL)
134 error (1, 0, _("virtual memory exhausted"));
136 if (stat (source_dirname, &source_dir_stats))
137 /* Shouldn't happen. */
138 error (1, errno, "%s", source_dirname);
140 if (stat (dest_dirname, &dest_dir_stats))
141 /* Shouldn't happen. */
142 error (1, errno, "%s", dest_dirname);
144 free (source_dirname);
147 return (source_dir_stats.st_dev == dest_dir_stats.st_dev
148 && source_dir_stats.st_ino == dest_dir_stats.st_ino
149 && STREQ (basename (source), basename (dest)));
152 /* Make a link DEST to the (usually) existing file SOURCE.
153 Symbolic links to nonexistent files are allowed.
154 If DEST is a directory, put the link to SOURCE in that directory.
155 Return 1 if there is an error, otherwise 0. */
158 do_link (const char *source, const char *dest)
160 struct stat source_stats;
161 struct stat dest_stats;
162 char *dest_backup = NULL;
165 /* Use stat here instead of lstat.
166 On SVR4, link does not follow symlinks, so this check disallows
167 making hard links to symlinks that point to directories. Big deal.
168 On other systems, link follows symlinks, so this check is right. */
171 if (stat (source, &source_stats) != 0)
173 error (0, errno, "%s", source);
176 if (!hard_dir_link && S_ISDIR (source_stats.st_mode))
178 error (0, 0, _("%s: hard link not allowed for directory"), source);
183 lstat_status = lstat (dest, &dest_stats);
185 if (lstat_status != 0 && errno != ENOENT)
187 error (0, errno, "%s", dest);
191 /* If --force (-f) has been specified without --backup, then before
192 making a link ln must remove the destination file if it exists.
193 (with --backup, it just renames any existing destination file)
194 But if the source and destination are the same, don't remove
195 anything and fail right here. */
196 if (remove_existing_files
198 /* Allow `ln -sf --backup k k' to succeed in creating the
199 self-referential symlink, but don't allow the hard-linking
200 equivalent: `ln -f k k' (with or without --backup) to get
201 beyond this point, because the error message you'd get is
203 && (backup_type == none || !symlink)
204 && (!symlink || stat (source, &source_stats) == 0)
205 && source_stats.st_dev == dest_stats.st_dev
206 && source_stats.st_ino == dest_stats.st_ino
207 /* The following detects whether removing DEST will also remove
208 SOURCE. If the file has only one link then both are surely
209 the same link. Otherwise check whether they point to the same
210 name in the same directory. */
211 && (source_stats.st_nlink == 1 || same_name (source, dest)))
213 error (0, 0, _("`%s' and `%s' are the same file"), source, dest);
217 /* If the destination is a directory or (it is a symlink to a directory
218 and the user has not specified --no-dereference), then form the
219 actual destination name by appending basename (source) to the
220 specified destination directory. */
221 if ((lstat_status == 0
222 && S_ISDIR (dest_stats.st_mode))
224 || (dereference_dest_dir_symlinks
225 && (S_ISLNK (dest_stats.st_mode)
230 /* Target is a directory; build the full filename. */
232 PATH_BASENAME_CONCAT (new_dest, dest, source);
234 /* Set this to nonzero to force another call to lstat
235 with the new destination. */
239 if (lstat_status == 0 || lstat (dest, &dest_stats) == 0)
241 if (S_ISDIR (dest_stats.st_mode))
243 error (0, 0, _("%s: cannot overwrite directory"), dest);
248 fprintf (stderr, _("%s: replace `%s'? "), program_name, dest);
252 else if (!remove_existing_files)
254 error (0, 0, _("%s: File exists"), dest);
258 if (backup_type != none)
260 char *tmp_backup = find_backup_file_name (dest);
261 if (tmp_backup == NULL)
262 error (1, 0, _("virtual memory exhausted"));
263 dest_backup = (char *) alloca (strlen (tmp_backup) + 1);
264 strcpy (dest_backup, tmp_backup);
266 if (rename (dest, dest_backup))
270 error (0, errno, _("cannot backup `%s'"), dest);
277 else if (unlink (dest) && errno != ENOENT)
279 error (0, errno, _("cannot remove `%s'"), dest);
283 else if (errno != ENOENT)
285 error (0, errno, "%s", dest);
290 printf (_("create %s %s to %s\n"), link_type_string, dest, source);
292 if ((*linkfunc) (source, dest) == 0)
297 error (0, errno, _("cannot create %s `%s' to `%s'"), link_type_string,
302 if (rename (dest_backup, dest))
303 error (0, errno, _("cannot un-backup `%s'"), dest);
312 fprintf (stderr, _("Try `%s --help' for more information.\n"),
317 Usage: %s [OPTION]... SOURCE [DEST]\n\
318 or: %s [OPTION]... SOURCE... DIRECTORY\n\
320 program_name, program_name);
322 Link SOURCE to DEST (. by default), or multiple SOURCE(s) to DIRECTORY.\n\
323 Makes hard links by default, symbolic links with -s.\n\
325 -b, --backup make backups for removed files\n\
326 -d, -F, --directory hard link directories (super-user only)\n\
327 -f, --force remove existing destinations\n\
328 -n, --no-dereference treat destination that is a symlink to a\n\
329 directory as if it were a normal file\n\
330 -i, --interactive prompt whether to remove destinations\n\
331 -s, --symbolic make symbolic links instead of hard links\n\
332 -v, --verbose print name of each file before linking\n\
333 -S, --suffix=SUFFIX override the usual backup suffix\n\
334 -V, --version-control=WORD override the usual version control\n\
335 --help display this help and exit\n\
336 --version output version information and exit\n\
340 The backup suffix is ~, unless set with SIMPLE_BACKUP_SUFFIX. The\n\
341 version control may be set with VERSION_CONTROL, values are:\n\
343 t, numbered make numbered backups\n\
344 nil, existing numbered if numbered backups exist, simple otherwise\n\
345 never, simple always make simple backups\n\
347 puts (_("\nReport bugs to fileutils-bugs@gnu.ai.mit.edu"));
353 main (int argc, char **argv)
357 int make_backups = 0;
360 program_name = argv[0];
361 setlocale (LC_ALL, "");
362 bindtextdomain (PACKAGE, LOCALEDIR);
363 textdomain (PACKAGE);
365 version = getenv ("SIMPLE_BACKUP_SUFFIX");
367 simple_backup_suffix = version;
368 version = getenv ("VERSION_CONTROL");
370 symbolic_link = remove_existing_files = interactive = verbose
374 while ((c = getopt_long (argc, argv,
375 "bdfinsvFS:V:", long_options, (int *) 0))
380 case 0: /* Long-named option. */
390 remove_existing_files = 1;
394 remove_existing_files = 0;
398 dereference_dest_dir_symlinks = 0;
404 error (1, 0, _("symbolic links are not supported on this system"));
411 simple_backup_suffix = optarg;
424 printf ("ln (%s) %s\n", GNU_PACKAGE, VERSION);
433 error (0, 0, _("missing file argument"));
438 backup_type = get_version (version);
444 link_type_string = _("symbolic link");
449 link_type_string = _("hard link");
452 link_type_string = _("link");
455 if (optind == argc - 1)
456 errors = do_link (argv[optind], ".");
457 else if (optind == argc - 2)
459 struct stat source_stats;
464 source = argv[optind];
465 dest = argv[optind + 1];
467 /* When the destination is specified with a trailing slash and the
468 source exists but is not a directory, convert the user's command
469 `ln source dest/' to `ln source dest/basename(source)'. */
471 if (dest[strlen (dest) - 1] == '/'
472 && lstat (source, &source_stats) == 0
473 && !S_ISDIR (source_stats.st_mode))
475 PATH_BASENAME_CONCAT (new_dest, dest, source);
482 errors = do_link (source, new_dest);
490 error (1, 0, _("when making multiple links, last argument must be a directory"));
491 for (; optind < argc - 1; ++optind)
492 errors += do_link (argv[optind], to);