*** empty log message ***
[coreutils.git] / src / ln.c
blob7943c88416dbede4a4eb329608259d814bc4ed80
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)
7 any later version.
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. */
20 #ifdef _AIX
21 #pragma alloca
22 #endif
24 #include <config.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <getopt.h>
29 #include "system.h"
30 #include "same.h"
31 #include "backupfile.h"
32 #include "error.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. */
41 #ifdef S_ISLNK
42 int symlink ();
43 #endif
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) \
49 do \
50 { \
51 const char *source_base; \
52 char *tmp_source; \
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);\
62 } \
63 while (0)
65 int isdir ();
66 int yesno ();
67 void strip_trailing_slashes ();
69 /* The name by which the program was run, for error messages. */
70 char *program_name;
72 /* FIXME: document */
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. */
93 static int verbose;
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},
117 {NULL, 0, NULL, 0}
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. */
125 static int
126 do_link (const char *source, const char *dest)
128 struct stat source_stats;
129 struct stat dest_stats;
130 char *dest_backup = NULL;
131 int lstat_status;
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. */
137 if (!symbolic_link)
139 if (stat (source, &source_stats) != 0)
141 /* This still could be a legitimate request:
142 if SOURCE is a dangling symlink. */
143 if (errno != ENOENT
144 || lstat (source, &source_stats) != 0)
146 error (0, errno, "%s", source);
147 return 1;
150 if (!hard_dir_link && S_ISDIR (source_stats.st_mode))
152 error (0, 0, _("%s: hard link not allowed for directory"), source);
153 return 1;
157 lstat_status = lstat (dest, &dest_stats);
158 if (lstat_status != 0 && errno != ENOENT)
160 error (0, errno, "%s", dest);
161 return 1;
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))
170 #ifdef S_ISLNK
171 || (dereference_dest_dir_symlinks
172 && (lstat_status == 0
173 && S_ISLNK (dest_stats.st_mode)
174 && isdir (dest)))
175 #endif
178 /* Target is a directory; build the full filename. */
179 char *new_dest;
180 PATH_BASENAME_CONCAT (new_dest, dest, source);
181 dest = new_dest;
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);
188 return 1;
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
198 && lstat_status == 0
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
203 misleading. */
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);
215 return 1;
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);
223 return 1;
225 if (interactive)
227 fprintf (stderr, _("%s: replace `%s'? "), program_name, dest);
228 if (!yesno ())
229 return 0;
231 else if (!remove_existing_files && backup_type == none)
233 error (0, 0, _("%s: File exists"), dest);
234 return 1;
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);
244 free (tmp_backup);
245 if (rename (dest, dest_backup))
247 if (errno != ENOENT)
249 error (0, errno, _("cannot backup `%s'"), dest);
250 return 1;
252 else
253 dest_backup = NULL;
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);
264 return 1;
267 else if (errno != ENOENT)
269 error (0, errno, "%s", dest);
270 return 1;
273 if (verbose)
274 printf (_("create %s %s to %s\n"), link_type_string, dest, source);
276 if ((*linkfunc) (source, dest) == 0)
278 return 0;
281 error (0, errno, _("cannot create %s `%s' to `%s'"), link_type_string,
282 dest, source);
284 if (dest_backup)
286 if (rename (dest_backup, dest))
287 error (0, errno, _("cannot un-backup `%s'"), dest);
289 return 1;
292 void
293 usage (int status)
295 if (status != 0)
296 fprintf (stderr, _("Try `%s --help' for more information.\n"),
297 program_name);
298 else
300 printf (_("\
301 Usage: %s [OPTION]... TARGET [LINK_NAME]\n\
302 or: %s [OPTION]... TARGET... DIRECTORY\n\
304 program_name, program_name);
305 printf (_("\
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\
324 "));
325 printf (_("\
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\
333 "));
334 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
335 close_stdout ();
337 exit (status);
341 main (int argc, char **argv)
343 int c;
344 int errors;
345 int make_backups = 0;
346 const char *version;
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");
356 if (version)
357 simple_backup_suffix = version;
358 version = NULL;
360 symbolic_link = remove_existing_files = interactive = verbose
361 = hard_dir_link = 0;
362 errors = 0;
364 while ((c = getopt_long (argc, argv, "bdfinsvFS:V:", long_options, NULL))
365 != -1)
367 switch (c)
369 case 0: /* Long-named option. */
370 break;
371 case 'b':
372 make_backups = 1;
373 break;
374 case 'd':
375 case 'F':
376 hard_dir_link = 1;
377 break;
378 case 'f':
379 remove_existing_files = 1;
380 interactive = 0;
381 break;
382 case 'i':
383 remove_existing_files = 0;
384 interactive = 1;
385 break;
386 case 'n':
387 dereference_dest_dir_symlinks = 0;
388 break;
389 case 's':
390 #ifdef S_ISLNK
391 symbolic_link = 1;
392 #else
393 error (1, 0, _("symbolic links are not supported on this system"));
394 #endif
395 break;
396 case 'v':
397 verbose = 1;
398 break;
399 case 'S':
400 simple_backup_suffix = optarg;
401 break;
402 case 'V':
403 version = optarg;
404 break;
405 case_GETOPT_HELP_CHAR;
406 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
407 default:
408 usage (1);
409 break;
413 if (optind == argc)
415 error (0, 0, _("missing file argument"));
416 usage (1);
419 backup_type = (make_backups
420 ? xget_version (_("--version-control"), version)
421 : none);
423 #ifdef S_ISLNK
424 if (symbolic_link)
426 linkfunc = symlink;
427 link_type_string = _("symbolic link");
429 else
431 linkfunc = link;
432 link_type_string = _("hard link");
434 #else
435 link_type_string = _("link");
436 #endif
438 if (optind == argc - 1)
439 errors = do_link (argv[optind], ".");
440 else if (optind == argc - 2)
442 struct stat source_stats;
443 const char *source;
444 char *dest;
445 char *new_dest;
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);
460 else
462 new_dest = dest;
465 errors = do_link (source, new_dest);
467 else
469 char *to;
471 to = argv[argc - 1];
472 if (!isdir (to))
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);
478 if (verbose)
479 close_stdout ();
480 exit (errors != 0);