Don't include version.h.
[coreutils.git] / src / ln.c
blobd60e369d98acb2b43d7f161a74a339958c384b27
1 /* `ln' program to create links between files.
2 Copyright (C) 1986, 1989, 1990, 1991, 1995 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
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, 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 (char *source, char *dest)
130 struct stat dest_stats;
131 char *dest_backup = NULL;
132 int lstat_status;
134 /* Use stat here instead of lstat.
135 On SVR4, link does not follow symlinks, so this check disallows
136 making hard links to symlinks that point to directories. Big deal.
137 On other systems, link follows symlinks, so this check is right. */
138 if (!symbolic_link)
140 struct stat source_stats;
142 if (stat (source, &source_stats) != 0)
144 error (0, errno, "%s", source);
145 return 1;
147 if (!hard_dir_link && S_ISDIR (source_stats.st_mode))
149 error (0, 0, _("%s: hard link not allowed for directory"), source);
150 return 1;
154 if (lstat (dest, &dest_stats) != 0 && errno != ENOENT)
156 error (0, errno, "%s", dest);
157 return 1;
160 /* If the destination is a directory or (it is a symlink to a directory
161 and the user has not specified --no-dereference), then form the
162 actual destination name by appending basename (source) to the
163 specified destination directory. */
164 lstat_status = lstat (dest, &dest_stats);
166 if (lstat_status != 0 && errno != ENOENT)
168 error (0, errno, "%s", dest);
169 return 1;
172 if ((lstat_status == 0
173 && S_ISDIR (dest_stats.st_mode))
174 #ifdef S_ISLNK
175 || (dereference_dest_dir_symlinks
176 && (S_ISLNK (dest_stats.st_mode)
177 && isdir (dest)))
178 #endif
181 /* Target is a directory; build the full filename. */
182 char *new_dest;
183 PATH_BASENAME_CONCAT (new_dest, dest, source);
184 dest = new_dest;
185 /* Set this to nonzero to force another call to lstat
186 with the new destination. */
187 lstat_status = 1;
190 if (lstat_status == 0 || lstat (dest, &dest_stats) == 0)
192 if (S_ISDIR (dest_stats.st_mode))
194 error (0, 0, _("%s: cannot overwrite directory"), dest);
195 return 1;
197 if (interactive)
199 fprintf (stderr, _("%s: replace `%s'? "), program_name, dest);
200 if (!yesno ())
201 return 0;
203 else if (!remove_existing_files)
205 error (0, 0, _("%s: File exists"), dest);
206 return 1;
209 if (backup_type != none)
211 char *tmp_backup = find_backup_file_name (dest);
212 if (tmp_backup == NULL)
213 error (1, 0, _("virtual memory exhausted"));
214 dest_backup = (char *) alloca (strlen (tmp_backup) + 1);
215 strcpy (dest_backup, tmp_backup);
216 free (tmp_backup);
217 if (rename (dest, dest_backup))
219 if (errno != ENOENT)
221 error (0, errno, _("cannot backup `%s'"), dest);
222 return 1;
224 else
225 dest_backup = NULL;
228 else if (unlink (dest) && errno != ENOENT)
230 error (0, errno, _("cannot remove `%s'"), dest);
231 return 1;
234 else if (errno != ENOENT)
236 error (0, errno, "%s", dest);
237 return 1;
240 if (verbose)
241 printf (_("create %slink %s to %s\n"), SYMBOLIC_SPACE_STRING,
242 dest, source);
244 if ((*linkfunc) (source, dest) == 0)
246 return 0;
249 error (0, errno, _("cannot %slink `%s' to `%s'"), SYMBOLIC_SPACE_STRING,
250 source, dest);
252 if (dest_backup)
254 if (rename (dest_backup, dest))
255 error (0, errno, _("cannot un-backup `%s'"), dest);
257 return 1;
260 static void
261 usage (int status)
263 if (status != 0)
264 fprintf (stderr, _("Try `%s --help' for more information.\n"),
265 program_name);
266 else
268 printf (_("\
269 Usage: %s [OPTION]... SOURCE [DEST]\n\
270 or: %s [OPTION]... SOURCE... DIRECTORY\n\
272 program_name, program_name);
273 printf (_("\
274 Link SOURCE to DEST (. by default), or multiple SOURCE(s) to DIRECTORY.\n\
275 Makes hard links by default, symbolic links with -s.\n\
277 -b, --backup make backups for removed files\n\
278 -d, -F, --directory hard link directories (super-user only)\n\
279 -f, --force remove existing destinations\n\
280 -n, --no-dereference treat destination that is a symlink to a\n\
281 directory as if it were a normal file\n\
282 -i, --interactive prompt whether to remove destinations\n\
283 -s, --symbolic make symbolic links instead of hard links\n\
284 -v, --verbose print name of each file before linking\n\
285 -S, --suffix=SUFFIX override the usual backup suffix\n\
286 -V, --version-control=WORD override the usual version control\n\
287 --help display this help and exit\n\
288 --version output version information and exit\n\
290 The backup suffix is ~, unless set with SIMPLE_BACKUP_SUFFIX. The\n\
291 version control may be set with VERSION_CONTROL, values are:\n\
293 t, numbered make numbered backups\n\
294 nil, existing numbered if numbered backups exist, simple otherwise\n\
295 never, simple always make simple backups\n"));
297 exit (status);
300 void
301 main (int argc, char **argv)
303 int c;
304 int errors;
305 int make_backups = 0;
306 char *version;
308 program_name = argv[0];
309 setlocale (LC_ALL, "");
310 bindtextdomain (PACKAGE, LOCALEDIR);
311 textdomain (PACKAGE);
313 version = getenv ("SIMPLE_BACKUP_SUFFIX");
314 if (version)
315 simple_backup_suffix = version;
316 version = getenv ("VERSION_CONTROL");
318 linkfunc = link;
319 symbolic_link = remove_existing_files = interactive = verbose
320 = hard_dir_link = 0;
321 errors = 0;
323 while ((c = getopt_long (argc, argv,
324 "bdfinsvFS:V:", long_options, (int *) 0))
325 != EOF)
327 switch (c)
329 case 0: /* Long-named option. */
330 break;
331 case 'b':
332 make_backups = 1;
333 break;
334 case 'd':
335 case 'F':
336 hard_dir_link = 1;
337 break;
338 case 'f':
339 remove_existing_files = 1;
340 interactive = 0;
341 break;
342 case 'i':
343 remove_existing_files = 0;
344 interactive = 1;
345 break;
346 case 'n':
347 dereference_dest_dir_symlinks = 0;
348 break;
349 case 's':
350 #ifdef S_ISLNK
351 symbolic_link = 1;
352 #else
353 error (1, 0, _("symbolic links are not supported on this system"));
354 #endif
355 break;
356 case 'v':
357 verbose = 1;
358 break;
359 case 'S':
360 simple_backup_suffix = optarg;
361 break;
362 case 'V':
363 version = optarg;
364 break;
365 default:
366 usage (1);
367 break;
371 if (show_version)
373 printf ("ln - %s\n", PACKAGE_VERSION);
374 exit (0);
377 if (show_help)
378 usage (0);
380 if (optind == argc)
382 error (0, 0, _("missing file argument"));
383 usage (1);
386 if (make_backups)
387 backup_type = get_version (version);
389 #ifdef S_ISLNK
390 if (symbolic_link)
391 linkfunc = symlink;
392 #endif
394 if (optind == argc - 1)
395 errors = do_link (argv[optind], ".");
396 else if (optind == argc - 2)
398 struct stat source_stats;
399 char *source;
400 char *dest;
401 char *new_dest;
403 source = argv[optind];
404 dest = argv[optind + 1];
406 /* When the destination is specified with a trailing slash and the
407 source exists but is not a directory, convert the user's command
408 `ln source dest/' to `ln source dest/basename(source)'. */
410 if (dest[strlen (dest) - 1] == '/'
411 && lstat (source, &source_stats) == 0
412 && !S_ISDIR (source_stats.st_mode))
414 PATH_BASENAME_CONCAT (new_dest, dest, source);
416 else
418 new_dest = dest;
421 errors = do_link (source, new_dest);
423 else
425 char *to;
427 to = argv[argc - 1];
428 if (!isdir (to))
429 error (1, 0, _("when making multiple links, last argument must be a directory"));
430 for (; optind < argc - 1; ++optind)
431 errors += do_link (argv[optind], to);
434 exit (errors != 0);