.
[coreutils.git] / src / ln.c
blobb24501e36c0d78f4a5e24bb88fc1662bbb71b0e3
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)
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 "backupfile.h"
31 #include "error.h"
33 int link (); /* Some systems don't declare this anywhere. */
35 #ifdef S_ISLNK
36 int symlink ();
37 #endif
39 #ifdef S_ISLNK
40 # define SYMBOLIC_SPACE_STRING symbolic_link ? _("symbolic ") : ""
41 #else
42 # define SYMBOLIC_SPACE_STRING ""
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 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 = basename (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 char *basename ();
66 enum backup_type get_version ();
67 int isdir ();
68 int yesno ();
69 void strip_trailing_slashes ();
70 char *stpcpy ();
72 /* The name by which the program was run, for error messages. */
73 char *program_name;
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 /* 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. */
89 static int verbose;
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, &hard_dir_link, 1},
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, &symbolic_link, 1},
115 {"verbose", no_argument, &verbose, 1},
116 {"version-control", required_argument, NULL, 'V'},
117 {"help", no_argument, &show_help, 1},
118 {"version", no_argument, &show_version, 1},
119 {NULL, 0, NULL, 0}
122 /* Make a link DEST to the (usually) existing file SOURCE.
123 Symbolic links to nonexistent files are allowed.
124 If DEST is a directory, put the link to SOURCE in that directory.
125 Return 1 if there is an error, otherwise 0. */
127 static int
128 do_link (const char *source, const char *dest)
130 struct stat source_stats;
131 struct stat dest_stats;
132 char *dest_backup = NULL;
133 int lstat_status;
135 /* Use stat here instead of lstat.
136 On SVR4, link does not follow symlinks, so this check disallows
137 making hard links to symlinks that point to directories. Big deal.
138 On other systems, link follows symlinks, so this check is right. */
139 if (!symbolic_link)
141 if (stat (source, &source_stats) != 0)
143 error (0, errno, "%s", source);
144 return 1;
146 if (!hard_dir_link && S_ISDIR (source_stats.st_mode))
148 error (0, 0, _("%s: hard link not allowed for directory"), source);
149 return 1;
153 lstat_status = lstat (dest, &dest_stats);
155 if (lstat_status != 0 && errno != ENOENT)
157 error (0, errno, "%s", dest);
158 return 1;
161 /* If --force (-f) has been specified, before making a link ln must
162 remove the destination file if it exists. But if the source and
163 destination are the same, don't remove anything and fail right here. */
164 if (remove_existing_files
165 && lstat_status == 0
166 && (!symlink || stat (source, &source_stats) == 0)
167 && source_stats.st_dev == dest_stats.st_dev
168 && source_stats.st_ino == dest_stats.st_ino)
170 error (0, 0, _("`%s' and `%s' are the same file"), source, dest);
171 return 1;
174 /* If the destination is a directory or (it is a symlink to a directory
175 and the user has not specified --no-dereference), then form the
176 actual destination name by appending basename (source) to the
177 specified destination directory. */
178 if ((lstat_status == 0
179 && S_ISDIR (dest_stats.st_mode))
180 #ifdef S_ISLNK
181 || (dereference_dest_dir_symlinks
182 && (S_ISLNK (dest_stats.st_mode)
183 && isdir (dest)))
184 #endif
187 /* Target is a directory; build the full filename. */
188 char *new_dest;
189 PATH_BASENAME_CONCAT (new_dest, dest, source);
190 dest = new_dest;
191 /* Set this to nonzero to force another call to lstat
192 with the new destination. */
193 lstat_status = 1;
196 if (lstat_status == 0 || lstat (dest, &dest_stats) == 0)
198 if (S_ISDIR (dest_stats.st_mode))
200 error (0, 0, _("%s: cannot overwrite directory"), dest);
201 return 1;
203 if (interactive)
205 fprintf (stderr, _("%s: replace `%s'? "), program_name, dest);
206 if (!yesno ())
207 return 0;
209 else if (!remove_existing_files)
211 error (0, 0, _("%s: File exists"), dest);
212 return 1;
215 if (backup_type != none)
217 char *tmp_backup = find_backup_file_name (dest);
218 if (tmp_backup == NULL)
219 error (1, 0, _("virtual memory exhausted"));
220 dest_backup = (char *) alloca (strlen (tmp_backup) + 1);
221 strcpy (dest_backup, tmp_backup);
222 free (tmp_backup);
223 if (rename (dest, dest_backup))
225 if (errno != ENOENT)
227 error (0, errno, _("cannot backup `%s'"), dest);
228 return 1;
230 else
231 dest_backup = NULL;
234 else if (unlink (dest) && errno != ENOENT)
236 error (0, errno, _("cannot remove `%s'"), dest);
237 return 1;
240 else if (errno != ENOENT)
242 error (0, errno, "%s", dest);
243 return 1;
246 if (verbose)
247 printf (_("create %slink %s to %s\n"), SYMBOLIC_SPACE_STRING,
248 dest, source);
250 if ((*linkfunc) (source, dest) == 0)
252 return 0;
255 error (0, errno, _("cannot %slink `%s' to `%s'"), SYMBOLIC_SPACE_STRING,
256 source, dest);
258 if (dest_backup)
260 if (rename (dest_backup, dest))
261 error (0, errno, _("cannot un-backup `%s'"), dest);
263 return 1;
266 static void
267 usage (int status)
269 if (status != 0)
270 fprintf (stderr, _("Try `%s --help' for more information.\n"),
271 program_name);
272 else
274 printf (_("\
275 Usage: %s [OPTION]... SOURCE [DEST]\n\
276 or: %s [OPTION]... SOURCE... DIRECTORY\n\
278 program_name, program_name);
279 printf (_("\
280 Link SOURCE to DEST (. by default), or multiple SOURCE(s) to DIRECTORY.\n\
281 Makes hard links by default, symbolic links with -s.\n\
283 -b, --backup make backups for removed files\n\
284 -d, -F, --directory hard link directories (super-user only)\n\
285 -f, --force remove existing destinations\n\
286 -n, --no-dereference treat destination that is a symlink to a\n\
287 directory as if it were a normal file\n\
288 -i, --interactive prompt whether to remove destinations\n\
289 -s, --symbolic make symbolic links instead of hard links\n\
290 -v, --verbose print name of each file before linking\n\
291 -S, --suffix=SUFFIX override the usual backup suffix\n\
292 -V, --version-control=WORD override the usual version control\n\
293 --help display this help and exit\n\
294 --version output version information and exit\n\
296 The backup suffix is ~, unless set with SIMPLE_BACKUP_SUFFIX. The\n\
297 version control may be set with VERSION_CONTROL, values are:\n\
299 t, numbered make numbered backups\n\
300 nil, existing numbered if numbered backups exist, simple otherwise\n\
301 never, simple always make simple backups\n"));
303 exit (status);
307 main (int argc, char **argv)
309 int c;
310 int errors;
311 int make_backups = 0;
312 char *version;
314 program_name = argv[0];
315 setlocale (LC_ALL, "");
316 bindtextdomain (PACKAGE, LOCALEDIR);
317 textdomain (PACKAGE);
319 version = getenv ("SIMPLE_BACKUP_SUFFIX");
320 if (version)
321 simple_backup_suffix = version;
322 version = getenv ("VERSION_CONTROL");
324 linkfunc = link;
325 symbolic_link = remove_existing_files = interactive = verbose
326 = hard_dir_link = 0;
327 errors = 0;
329 while ((c = getopt_long (argc, argv,
330 "bdfinsvFS:V:", long_options, (int *) 0))
331 != EOF)
333 switch (c)
335 case 0: /* Long-named option. */
336 break;
337 case 'b':
338 make_backups = 1;
339 break;
340 case 'd':
341 case 'F':
342 hard_dir_link = 1;
343 break;
344 case 'f':
345 remove_existing_files = 1;
346 interactive = 0;
347 break;
348 case 'i':
349 remove_existing_files = 0;
350 interactive = 1;
351 break;
352 case 'n':
353 dereference_dest_dir_symlinks = 0;
354 break;
355 case 's':
356 #ifdef S_ISLNK
357 symbolic_link = 1;
358 #else
359 error (1, 0, _("symbolic links are not supported on this system"));
360 #endif
361 break;
362 case 'v':
363 verbose = 1;
364 break;
365 case 'S':
366 simple_backup_suffix = optarg;
367 break;
368 case 'V':
369 version = optarg;
370 break;
371 default:
372 usage (1);
373 break;
377 if (show_version)
379 printf ("ln - %s\n", PACKAGE_VERSION);
380 exit (0);
383 if (show_help)
384 usage (0);
386 if (optind == argc)
388 error (0, 0, _("missing file argument"));
389 usage (1);
392 if (make_backups)
393 backup_type = get_version (version);
395 #ifdef S_ISLNK
396 if (symbolic_link)
397 linkfunc = symlink;
398 #endif
400 if (optind == argc - 1)
401 errors = do_link (argv[optind], ".");
402 else if (optind == argc - 2)
404 struct stat source_stats;
405 char *source;
406 char *dest;
407 char *new_dest;
409 source = argv[optind];
410 dest = argv[optind + 1];
412 /* When the destination is specified with a trailing slash and the
413 source exists but is not a directory, convert the user's command
414 `ln source dest/' to `ln source dest/basename(source)'. */
416 if (dest[strlen (dest) - 1] == '/'
417 && lstat (source, &source_stats) == 0
418 && !S_ISDIR (source_stats.st_mode))
420 PATH_BASENAME_CONCAT (new_dest, dest, source);
422 else
424 new_dest = dest;
427 errors = do_link (source, new_dest);
429 else
431 char *to;
433 to = argv[argc - 1];
434 if (!isdir (to))
435 error (1, 0, _("when making multiple links, last argument must be a directory"));
436 for (; optind < argc - 1; ++optind)
437 errors += do_link (argv[optind], to);
440 exit (errors != 0);