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>
31 #include "backupfile.h"
34 /* The official name of this program (e.g., no `g' prefix). */
35 #define PROGRAM_NAME "ln"
37 #define AUTHORS "Mike Parker and David MacKenzie"
39 int link (); /* Some systems don't declare this anywhere. */
45 /* Construct a string NEW_DEST by concatenating DEST, a slash, and
46 basename(SOURCE) in alloca'd memory. Don't modify DEST or SOURCE. */
48 #define PATH_BASENAME_CONCAT(new_dest, dest, source) \
51 const char *source_base; \
54 tmp_source = (char *) alloca (strlen ((source)) + 1); \
55 strcpy (tmp_source, (source)); \
56 strip_trailing_slashes (tmp_source); \
57 source_base = base_name (tmp_source); \
59 (new_dest) = (char *) alloca (strlen ((dest)) + 1 \
60 + strlen (source_base) + 1); \
61 stpcpy (stpcpy (stpcpy ((new_dest), (dest)), "/"), source_base);\
67 void strip_trailing_slashes ();
69 /* The name by which the program was run, for error messages. */
73 enum backup_type backup_type
;
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 /* A string describing type of link to make. For use in verbose
83 diagnostics and in error messages. */
84 static const char *link_type_string
;
86 /* If nonzero, ask the user before removing existing files. */
87 static int interactive
;
89 /* If nonzero, remove existing files unconditionally. */
90 static int remove_existing_files
;
92 /* If nonzero, list each file as it is moved. */
95 /* If nonzero, allow the superuser to make hard links to directories. */
96 static int hard_dir_link
;
98 /* If nonzero, and the specified destination is a symbolic link to a
99 directory, treat it just as if it were a directory. Otherwise, the
100 command `ln --force --no-dereference file symlink-to-dir' deletes
101 symlink-to-dir before creating the new link. */
102 static int dereference_dest_dir_symlinks
= 1;
104 static struct option
const long_options
[] =
106 {"backup", no_argument
, NULL
, 'b'},
107 {"directory", no_argument
, NULL
, 'F'},
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
, NULL
, 's'},
113 {"verbose", no_argument
, NULL
, 'v'},
114 {"version-control", required_argument
, NULL
, 'V'},
115 {GETOPT_HELP_OPTION_DECL
},
116 {GETOPT_VERSION_OPTION_DECL
},
120 /* Make a link DEST to the (usually) existing file SOURCE.
121 Symbolic links to nonexistent files are allowed.
122 If DEST is a directory, put the link to SOURCE in that directory.
123 Return 1 if there is an error, otherwise 0. */
126 do_link (const char *source
, const char *dest
)
128 struct stat source_stats
;
129 struct stat dest_stats
;
130 char *dest_backup
= NULL
;
133 /* Use stat here instead of lstat.
134 On SVR4, link does not follow symlinks, so this check disallows
135 making hard links to symlinks that point to directories. Big deal.
136 On other systems, link follows symlinks, so this check is right. */
139 if (stat (source
, &source_stats
) != 0)
141 /* This still could be a legitimate request:
142 if SOURCE is a dangling symlink. */
144 || lstat (source
, &source_stats
) != 0)
146 error (0, errno
, "%s", source
);
150 if (!hard_dir_link
&& S_ISDIR (source_stats
.st_mode
))
152 error (0, 0, _("%s: hard link not allowed for directory"), source
);
157 lstat_status
= lstat (dest
, &dest_stats
);
158 if (lstat_status
!= 0 && errno
!= ENOENT
)
160 error (0, errno
, "%s", dest
);
164 /* If the destination is a directory or (it is a symlink to a directory
165 and the user has not specified --no-dereference), then form the
166 actual destination name by appending base_name (source) to the
167 specified destination directory. */
168 if ((lstat_status
== 0
169 && S_ISDIR (dest_stats
.st_mode
))
171 || (dereference_dest_dir_symlinks
172 && (lstat_status
== 0
173 && S_ISLNK (dest_stats
.st_mode
)
178 /* Target is a directory; build the full filename. */
180 PATH_BASENAME_CONCAT (new_dest
, dest
, source
);
183 /* Get stats for new DEST. */
184 lstat_status
= lstat (dest
, &dest_stats
);
185 if (lstat_status
!= 0 && errno
!= ENOENT
)
187 error (0, errno
, "%s", dest
);
192 /* If --force (-f) has been specified without --backup, then before
193 making a link ln must remove the destination file if it exists.
194 (with --backup, it just renames any existing destination file)
195 But if the source and destination are the same, don't remove
196 anything and fail right here. */
197 if (remove_existing_files
199 /* Allow `ln -sf --backup k k' to succeed in creating the
200 self-referential symlink, but don't allow the hard-linking
201 equivalent: `ln -f k k' (with or without --backup) to get
202 beyond this point, because the error message you'd get is
204 && (backup_type
== none
|| !symlink
)
205 && (!symlink
|| stat (source
, &source_stats
) == 0)
206 && source_stats
.st_dev
== dest_stats
.st_dev
207 && source_stats
.st_ino
== dest_stats
.st_ino
208 /* The following detects whether removing DEST will also remove
209 SOURCE. If the file has only one link then both are surely
210 the same link. Otherwise check whether they point to the same
211 name in the same directory. */
212 && (source_stats
.st_nlink
== 1 || same_name (source
, dest
)))
214 error (0, 0, _("`%s' and `%s' are the same file"), source
, dest
);
218 if (lstat_status
== 0 || lstat (dest
, &dest_stats
) == 0)
220 if (S_ISDIR (dest_stats
.st_mode
))
222 error (0, 0, _("%s: cannot overwrite directory"), dest
);
227 fprintf (stderr
, _("%s: replace `%s'? "), program_name
, dest
);
231 else if (!remove_existing_files
&& backup_type
== none
)
233 error (0, 0, _("%s: File exists"), dest
);
237 if (backup_type
!= none
)
239 char *tmp_backup
= find_backup_file_name (dest
, backup_type
);
240 if (tmp_backup
== NULL
)
241 error (1, 0, _("virtual memory exhausted"));
242 dest_backup
= (char *) alloca (strlen (tmp_backup
) + 1);
243 strcpy (dest_backup
, tmp_backup
);
245 if (rename (dest
, dest_backup
))
249 error (0, errno
, _("cannot backup `%s'"), dest
);
257 /* Try to unlink DEST even if we may have renamed it. In some unusual
258 cases (when DEST and DEST_BACKUP are hard-links that refer to the
259 same file), rename succeeds and DEST remains. If we didn't remove
260 DEST in that case, the subsequent LINKFUNC call would fail. */
261 if (unlink (dest
) && errno
!= ENOENT
)
263 error (0, errno
, _("cannot remove `%s'"), dest
);
267 else if (errno
!= ENOENT
)
269 error (0, errno
, "%s", dest
);
274 printf (_("create %s %s to %s\n"), link_type_string
, dest
, source
);
276 if ((*linkfunc
) (source
, dest
) == 0)
281 error (0, errno
, _("cannot create %s `%s' to `%s'"), link_type_string
,
286 if (rename (dest_backup
, dest
))
287 error (0, errno
, _("cannot un-backup `%s'"), dest
);
296 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
301 Usage: %s [OPTION]... TARGET [LINK_NAME]\n\
302 or: %s [OPTION]... TARGET... DIRECTORY\n\
304 program_name
, program_name
);
306 Create a link to the specified TARGET with optional LINK_NAME. If there is\n\
307 more than one TARGET, the last argument must be a directory; create links\n\
308 in DIRECTORY to each TARGET. Create hard links by default, symbolic links\n\
309 with --symbolic. When creating hard links, each TARGET must exist.\n\
311 -b, --backup make a backup of each existing destination file\n\
312 -d, -F, --directory hard link directories (super-user only)\n\
313 -f, --force remove existing destination files\n\
314 -n, --no-dereference treat destination that is a symlink to a\n\
315 directory as if it were a normal file\n\
316 -i, --interactive prompt whether to remove destinations\n\
317 -s, --symbolic make symbolic links instead of hard links\n\
318 -S, --suffix=SUFFIX override the usual backup suffix\n\
319 -v, --verbose print name of each file before linking\n\
320 -V, --version-control=WORD override the usual version control\n\
321 --help display this help and exit\n\
322 --version output version information and exit\n\
326 The backup suffix is ~, unless set with SIMPLE_BACKUP_SUFFIX. The\n\
327 version control may be set with VERSION_CONTROL, values are:\n\
329 none, off never make backups (even if --backup is given)\n\
330 numbered, t make numbered backups\n\
331 existing, nil numbered if numbered backups exist, simple otherwise\n\
332 simple, never always make simple backups\n\
334 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
341 main (int argc
, char **argv
)
345 int make_backups
= 0;
348 program_name
= argv
[0];
349 setlocale (LC_ALL
, "");
350 bindtextdomain (PACKAGE
, LOCALEDIR
);
351 textdomain (PACKAGE
);
353 /* FIXME: consider not calling getenv for SIMPLE_BACKUP_SUFFIX unless
354 we'll actually use simple_backup_suffix. */
355 version
= getenv ("SIMPLE_BACKUP_SUFFIX");
357 simple_backup_suffix
= version
;
360 symbolic_link
= remove_existing_files
= interactive
= verbose
364 while ((c
= getopt_long (argc
, argv
, "bdfinsvFS:V:", long_options
, NULL
))
369 case 0: /* Long-named option. */
379 remove_existing_files
= 1;
383 remove_existing_files
= 0;
387 dereference_dest_dir_symlinks
= 0;
393 error (1, 0, _("symbolic links are not supported on this system"));
400 simple_backup_suffix
= optarg
;
405 case_GETOPT_HELP_CHAR
;
406 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
415 error (0, 0, _("missing file argument"));
419 backup_type
= (make_backups
420 ? xget_version (_("--version-control"), version
)
427 link_type_string
= _("symbolic link");
432 link_type_string
= _("hard link");
435 link_type_string
= _("link");
438 if (optind
== argc
- 1)
439 errors
= do_link (argv
[optind
], ".");
440 else if (optind
== argc
- 2)
442 struct stat source_stats
;
447 source
= argv
[optind
];
448 dest
= argv
[optind
+ 1];
450 /* When the destination is specified with a trailing slash and the
451 source exists but is not a directory, convert the user's command
452 `ln source dest/' to `ln source dest/basename(source)'. */
454 if (dest
[strlen (dest
) - 1] == '/'
455 && lstat (source
, &source_stats
) == 0
456 && !S_ISDIR (source_stats
.st_mode
))
458 PATH_BASENAME_CONCAT (new_dest
, dest
, source
);
465 errors
= do_link (source
, new_dest
);
473 error (1, 0, _("when making multiple links, last argument must be a directory"));
474 for (; optind
< argc
- 1; ++optind
)
475 errors
+= do_link (argv
[optind
], to
);