1 /* `ln' program to create links between files.
2 Copyright (C) 86, 89, 90, 91, 1995-1999 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"
35 int link (); /* Some systems don't declare this anywhere. */
41 /* Construct a string NEW_DEST by concatenating DEST, a slash, and
42 basename(SOURCE) in alloca'd memory. Don't modify DEST or SOURCE. */
44 #define PATH_BASENAME_CONCAT(new_dest, dest, source) \
50 tmp_source = (char *) alloca (strlen ((source)) + 1); \
51 strcpy (tmp_source, (source)); \
52 strip_trailing_slashes (tmp_source); \
53 source_base = base_name (tmp_source); \
55 (new_dest) = (char *) alloca (strlen ((dest)) + 1 \
56 + strlen (source_base) + 1); \
57 stpcpy (stpcpy (stpcpy ((new_dest), (dest)), "/"), source_base);\
63 void strip_trailing_slashes ();
65 /* The name by which the program was run, for error messages. */
69 enum backup_type backup_type
;
71 /* A pointer to the function used to make links. This will point to either
72 `link' or `symlink'. */
73 static int (*linkfunc
) ();
75 /* If nonzero, make symbolic links; otherwise, make hard links. */
76 static int symbolic_link
;
78 /* A string describing type of link to make. For use in verbose
79 diagnostics and in error messages. */
80 static const char *link_type_string
;
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 nonzero, display usage information and exit. */
101 static int show_help
;
103 /* If nonzero, 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
, NULL
, 'F'},
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
, NULL
, 's'},
115 {"verbose", no_argument
, NULL
, 'v'},
116 {"version-control", required_argument
, NULL
, 'V'},
117 {"help", no_argument
, &show_help
, 1},
118 {"version", no_argument
, &show_version
, 1},
122 /* Return nonzero if SOURCE and DEST point to the same name in the same
126 same_name (const char *source
, const char *dest
)
128 struct stat source_dir_stats
;
129 struct stat dest_dir_stats
;
130 char *source_dirname
, *dest_dirname
;
132 source_dirname
= dir_name (source
);
133 dest_dirname
= dir_name (dest
);
134 if (source_dirname
== NULL
|| dest_dirname
== NULL
)
135 error (1, 0, _("virtual memory exhausted"));
137 if (stat (source_dirname
, &source_dir_stats
))
139 /* Shouldn't happen. */
140 error (1, errno
, "%s", source_dirname
);
143 if (stat (dest_dirname
, &dest_dir_stats
))
145 /* Shouldn't happen. */
146 error (1, errno
, "%s", dest_dirname
);
149 free (source_dirname
);
152 return (SAME_INODE (source_dir_stats
, dest_dir_stats
)
153 && STREQ (base_name (source
), base_name (dest
)));
156 /* Make a link DEST to the (usually) existing file SOURCE.
157 Symbolic links to nonexistent files are allowed.
158 If DEST is a directory, put the link to SOURCE in that directory.
159 Return 1 if there is an error, otherwise 0. */
162 do_link (const char *source
, const char *dest
)
164 struct stat source_stats
;
165 struct stat dest_stats
;
166 char *dest_backup
= NULL
;
169 /* Use stat here instead of lstat.
170 On SVR4, link does not follow symlinks, so this check disallows
171 making hard links to symlinks that point to directories. Big deal.
172 On other systems, link follows symlinks, so this check is right. */
175 if (stat (source
, &source_stats
) != 0)
177 /* This still could be a legitimate request:
178 if SOURCE is a dangling symlink. */
180 || lstat (source
, &source_stats
) != 0)
182 error (0, errno
, "%s", source
);
186 if (!hard_dir_link
&& S_ISDIR (source_stats
.st_mode
))
188 error (0, 0, _("%s: hard link not allowed for directory"), source
);
193 lstat_status
= lstat (dest
, &dest_stats
);
194 if (lstat_status
!= 0 && errno
!= ENOENT
)
196 error (0, errno
, "%s", dest
);
200 /* If the destination is a directory or (it is a symlink to a directory
201 and the user has not specified --no-dereference), then form the
202 actual destination name by appending base_name (source) to the
203 specified destination directory. */
204 if ((lstat_status
== 0
205 && S_ISDIR (dest_stats
.st_mode
))
207 || (dereference_dest_dir_symlinks
208 && (lstat_status
== 0
209 && S_ISLNK (dest_stats
.st_mode
)
214 /* Target is a directory; build the full filename. */
216 PATH_BASENAME_CONCAT (new_dest
, dest
, source
);
219 /* Get stats for new DEST. */
220 lstat_status
= lstat (dest
, &dest_stats
);
221 if (lstat_status
!= 0 && errno
!= ENOENT
)
223 error (0, errno
, "%s", dest
);
228 /* If --force (-f) has been specified without --backup, then before
229 making a link ln must remove the destination file if it exists.
230 (with --backup, it just renames any existing destination file)
231 But if the source and destination are the same, don't remove
232 anything and fail right here. */
233 if (remove_existing_files
235 /* Allow `ln -sf --backup k k' to succeed in creating the
236 self-referential symlink, but don't allow the hard-linking
237 equivalent: `ln -f k k' (with or without --backup) to get
238 beyond this point, because the error message you'd get is
240 && (backup_type
== none
|| !symlink
)
241 && (!symlink
|| stat (source
, &source_stats
) == 0)
242 && source_stats
.st_dev
== dest_stats
.st_dev
243 && source_stats
.st_ino
== dest_stats
.st_ino
244 /* The following detects whether removing DEST will also remove
245 SOURCE. If the file has only one link then both are surely
246 the same link. Otherwise check whether they point to the same
247 name in the same directory. */
248 && (source_stats
.st_nlink
== 1 || same_name (source
, dest
)))
250 error (0, 0, _("`%s' and `%s' are the same file"), source
, dest
);
254 if (lstat_status
== 0 || lstat (dest
, &dest_stats
) == 0)
256 if (S_ISDIR (dest_stats
.st_mode
))
258 error (0, 0, _("%s: cannot overwrite directory"), dest
);
263 fprintf (stderr
, _("%s: replace `%s'? "), program_name
, dest
);
267 else if (!remove_existing_files
&& backup_type
== none
)
269 error (0, 0, _("%s: File exists"), dest
);
273 if (backup_type
!= none
)
275 char *tmp_backup
= find_backup_file_name (dest
, backup_type
);
276 if (tmp_backup
== NULL
)
277 error (1, 0, _("virtual memory exhausted"));
278 dest_backup
= (char *) alloca (strlen (tmp_backup
) + 1);
279 strcpy (dest_backup
, tmp_backup
);
281 if (rename (dest
, dest_backup
))
285 error (0, errno
, _("cannot backup `%s'"), dest
);
293 /* Try to unlink DEST even if we may have renamed it. In some unusual
294 cases (when DEST and DEST_BACKUP are hard-links that refer to the
295 same file), rename succeeds and DEST remains. If we didn't remove
296 DEST in that case, the subsequent LINKFUNC call would fail. */
297 if (unlink (dest
) && errno
!= ENOENT
)
299 error (0, errno
, _("cannot remove `%s'"), dest
);
303 else if (errno
!= ENOENT
)
305 error (0, errno
, "%s", dest
);
310 printf (_("create %s %s to %s\n"), link_type_string
, dest
, source
);
312 if ((*linkfunc
) (source
, dest
) == 0)
317 error (0, errno
, _("cannot create %s `%s' to `%s'"), link_type_string
,
322 if (rename (dest_backup
, dest
))
323 error (0, errno
, _("cannot un-backup `%s'"), dest
);
332 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
337 Usage: %s [OPTION]... TARGET [LINK_NAME]\n\
338 or: %s [OPTION]... TARGET... DIRECTORY\n\
340 program_name
, program_name
);
342 Create a link to the specified TARGET with optional LINK_NAME. If there is\n\
343 more than one TARGET, the last argument must be a directory; create links\n\
344 in DIRECTORY to each TARGET. Create hard links by default, symbolic links\n\
345 with --symbolic. When creating hard links, each TARGET must exist.\n\
347 -b, --backup make a backup of each existing destination file\n\
348 -d, -F, --directory hard link directories (super-user only)\n\
349 -f, --force remove existing destination files\n\
350 -n, --no-dereference treat destination that is a symlink to a\n\
351 directory as if it were a normal file\n\
352 -i, --interactive prompt whether to remove destinations\n\
353 -s, --symbolic make symbolic links instead of hard links\n\
354 -S, --suffix=SUFFIX override the usual backup suffix\n\
355 -v, --verbose print name of each file before linking\n\
356 -V, --version-control=WORD override the usual version control\n\
357 --help display this help and exit\n\
358 --version output version information and exit\n\
362 The backup suffix is ~, unless set with SIMPLE_BACKUP_SUFFIX. The\n\
363 version control may be set with VERSION_CONTROL, values are:\n\
365 none, off never make backups (even if --backup is given)\n\
366 numbered, t make numbered backups\n\
367 existing, nil numbered if numbered backups exist, simple otherwise\n\
368 simple, never always make simple backups\n\
370 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
377 main (int argc
, char **argv
)
381 int make_backups
= 0;
384 program_name
= argv
[0];
385 setlocale (LC_ALL
, "");
386 bindtextdomain (PACKAGE
, LOCALEDIR
);
387 textdomain (PACKAGE
);
389 /* FIXME: consider not calling getenv for SIMPLE_BACKUP_SUFFIX unless
390 we'll actually use simple_backup_suffix. */
391 version
= getenv ("SIMPLE_BACKUP_SUFFIX");
393 simple_backup_suffix
= version
;
396 symbolic_link
= remove_existing_files
= interactive
= verbose
400 while ((c
= getopt_long (argc
, argv
, "bdfinsvFS:V:", long_options
, NULL
))
405 case 0: /* Long-named option. */
415 remove_existing_files
= 1;
419 remove_existing_files
= 0;
423 dereference_dest_dir_symlinks
= 0;
429 error (1, 0, _("symbolic links are not supported on this system"));
436 simple_backup_suffix
= optarg
;
449 printf ("ln (%s) %s\n", GNU_PACKAGE
, VERSION
);
459 error (0, 0, _("missing file argument"));
463 backup_type
= (make_backups
464 ? xget_version (_("--version-control"), version
)
471 link_type_string
= _("symbolic link");
476 link_type_string
= _("hard link");
479 link_type_string
= _("link");
482 if (optind
== argc
- 1)
483 errors
= do_link (argv
[optind
], ".");
484 else if (optind
== argc
- 2)
486 struct stat source_stats
;
491 source
= argv
[optind
];
492 dest
= argv
[optind
+ 1];
494 /* When the destination is specified with a trailing slash and the
495 source exists but is not a directory, convert the user's command
496 `ln source dest/' to `ln source dest/basename(source)'. */
498 if (dest
[strlen (dest
) - 1] == '/'
499 && lstat (source
, &source_stats
) == 0
500 && !S_ISDIR (source_stats
.st_mode
))
502 PATH_BASENAME_CONCAT (new_dest
, dest
, source
);
509 errors
= do_link (source
, new_dest
);
517 error (1, 0, _("when making multiple links, last argument must be a directory"));
518 for (; optind
< argc
- 1; ++optind
)
519 errors
+= do_link (argv
[optind
], to
);