merge with 3.8.3b
[coreutils.git] / src / ln.c
blob8e96eb848716c4268547fe8111ba44ab26a033d0
1 /* `ln' program to create links between files.
2 Copyright (C) 1986, 1989, 1990, 1991 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 #ifdef HAVE_CONFIG_H
25 #if defined (CONFIG_BROKETS)
26 /* We use <config.h> instead of "config.h" so that a compilation
27 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
28 (which it would do because it found this file in $srcdir). */
29 #include <config.h>
30 #else
31 #include "config.h"
32 #endif
33 #endif
35 #include <stdio.h>
36 #include <sys/types.h>
37 #include <getopt.h>
38 #include "system.h"
39 #include "backupfile.h"
40 #include "version.h"
42 int link (); /* Some systems don't declare this anywhere. */
44 #ifdef S_ISLNK
45 int symlink ();
46 #endif
48 /* Construct a string NEW_DEST by concatenating DEST, a slash, and
49 basename(SOURCE) in alloca'd memory. Don't modify DEST or SOURCE. */
51 #define PATH_BASENAME_CONCAT(new_dest, dest, source) \
52 do \
53 { \
54 char *source_base; \
55 char *tmp_source; \
57 tmp_source = (char *) alloca (strlen ((source)) + 1); \
58 strcpy (tmp_source, (source)); \
59 strip_trailing_slashes (tmp_source); \
60 source_base = basename (tmp_source); \
62 (new_dest) = (char *) alloca (strlen ((dest)) + 1 \
63 + strlen (source_base) + 1); \
64 stpcpy (stpcpy (stpcpy ((new_dest), (dest)), "/"), source_base);\
65 } \
66 while (0)
68 char *basename ();
69 enum backup_type get_version ();
70 int isdir ();
71 int yesno ();
72 void error ();
73 void strip_trailing_slashes ();
74 char *stpcpy ();
76 static void usage ();
77 static int do_link ();
79 /* The name by which the program was run, for error messages. */
80 char *program_name;
82 /* A pointer to the function used to make links. This will point to either
83 `link' or `symlink'. */
84 static int (*linkfunc) ();
86 /* If nonzero, make symbolic links; otherwise, make hard links. */
87 static int symbolic_link;
89 /* If nonzero, ask the user before removing existing files. */
90 static int interactive;
92 /* If nonzero, remove existing files unconditionally. */
93 static int remove_existing_files;
95 /* If nonzero, list each file as it is moved. */
96 static int verbose;
98 /* If nonzero, allow the superuser to make hard links to directories. */
99 static int hard_dir_link;
101 /* If non-zero, display usage information and exit. */
102 static int show_help;
104 /* If non-zero, print the version on standard output and exit. */
105 static int show_version;
107 static struct option const long_options[] =
109 {"backup", no_argument, NULL, 'b'},
110 {"directory", no_argument, &hard_dir_link, 1},
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 void
123 main (argc, argv)
124 int argc;
125 char **argv;
127 int c;
128 int errors;
129 int make_backups = 0;
130 char *version;
132 version = getenv ("SIMPLE_BACKUP_SUFFIX");
133 if (version)
134 simple_backup_suffix = version;
135 version = getenv ("VERSION_CONTROL");
136 program_name = argv[0];
137 linkfunc = link;
138 symbolic_link = remove_existing_files = interactive = verbose
139 = hard_dir_link = 0;
140 errors = 0;
142 while ((c = getopt_long (argc, argv, "bdfisvFS:V:", long_options, (int *) 0))
143 != EOF)
145 switch (c)
147 case 0: /* Long-named option. */
148 break;
149 case 'b':
150 make_backups = 1;
151 break;
152 case 'd':
153 case 'F':
154 hard_dir_link = 1;
155 break;
156 case 'f':
157 remove_existing_files = 1;
158 interactive = 0;
159 break;
160 case 'i':
161 remove_existing_files = 0;
162 interactive = 1;
163 break;
164 case 's':
165 #ifdef S_ISLNK
166 symbolic_link = 1;
167 #else
168 error (1, 0, "symbolic links are not supported on this system");
169 #endif
170 break;
171 case 'v':
172 verbose = 1;
173 break;
174 case 'S':
175 simple_backup_suffix = optarg;
176 break;
177 case 'V':
178 version = optarg;
179 break;
180 default:
181 usage ();
182 break;
186 if (show_version)
188 printf ("%s\n", version_string);
189 exit (0);
192 if (show_help)
193 usage ();
195 if (optind == argc)
196 usage ();
198 if (make_backups)
199 backup_type = get_version (version);
201 #ifdef S_ISLNK
202 if (symbolic_link)
203 linkfunc = symlink;
204 #endif
206 if (optind == argc - 1)
207 errors = do_link (argv[optind], ".");
208 else if (optind == argc - 2)
210 struct stat source_stats;
211 char *source;
212 char *dest;
213 char *new_dest;
215 source = argv[optind];
216 dest = argv[optind + 1];
218 /* When the destination is specified with a trailing slash and the
219 source exists but is not a directory, convert the user's command
220 `ln source dest/' to `ln source dest/basename(source)'. */
222 if (dest[strlen (dest) - 1] == '/'
223 && lstat (source, &source_stats) == 0
224 && !S_ISDIR (source_stats.st_mode))
226 PATH_BASENAME_CONCAT (new_dest, dest, source);
228 else
230 new_dest = dest;
233 errors = do_link (source, new_dest);
235 else
237 char *to;
239 to = argv[argc - 1];
240 if (!isdir (to))
241 error (1, 0, "when making multiple links, last argument must be a directory");
242 for (; optind < argc - 1; ++optind)
243 errors += do_link (argv[optind], to);
246 exit (errors != 0);
249 /* Make a link DEST to existing file SOURCE.
250 If DEST is a directory, put the link to SOURCE in that directory.
251 Return 1 if there is an error, otherwise 0. */
253 static int
254 do_link (source, dest)
255 char *source;
256 char *dest;
258 struct stat dest_stats;
259 char *dest_backup = NULL;
261 /* isdir uses stat instead of lstat.
262 On SVR4, link does not follow symlinks, so this check disallows
263 making hard links to symlinks that point to directories. Big deal.
264 On other systems, link follows symlinks, so this check is right. */
265 if (!symbolic_link && !hard_dir_link && isdir (source))
267 error (0, 0, "%s: hard link not allowed for directory", source);
268 return 1;
270 if (isdir (dest))
272 /* Target is a directory; build the full filename. */
273 char *new_dest;
274 PATH_BASENAME_CONCAT (new_dest, dest, source);
275 dest = new_dest;
278 if (lstat (dest, &dest_stats) == 0)
280 if (S_ISDIR (dest_stats.st_mode))
282 error (0, 0, "%s: cannot overwrite directory", dest);
283 return 1;
285 if (interactive)
287 fprintf (stderr, "%s: replace `%s'? ", program_name, dest);
288 if (!yesno ())
289 return 0;
291 else if (!remove_existing_files)
293 error (0, 0, "%s: File exists", dest);
294 return 1;
297 if (backup_type != none)
299 char *tmp_backup = find_backup_file_name (dest);
300 if (tmp_backup == NULL)
301 error (1, 0, "virtual memory exhausted");
302 dest_backup = (char *) alloca (strlen (tmp_backup) + 1);
303 strcpy (dest_backup, tmp_backup);
304 free (tmp_backup);
305 if (rename (dest, dest_backup))
307 if (errno != ENOENT)
309 error (0, errno, "cannot backup `%s'", dest);
310 return 1;
312 else
313 dest_backup = NULL;
316 else if (unlink (dest) && errno != ENOENT)
318 error (0, errno, "cannot remove old link to `%s'", dest);
319 return 1;
322 else if (errno != ENOENT)
324 error (0, errno, "%s", dest);
325 return 1;
328 if (verbose)
329 printf ("%s -> %s\n", source, dest);
331 if ((*linkfunc) (source, dest) == 0)
333 return 0;
336 error (0, errno, "cannot %slink `%s' to `%s'",
337 #ifdef S_ISLNK
338 linkfunc == symlink ? "symbolic " : "",
339 #else
341 #endif
342 source, dest);
344 if (dest_backup)
346 if (rename (dest_backup, dest))
347 error (0, errno, "cannot un-backup `%s'", dest);
349 return 1;
352 static void
353 usage ()
355 fprintf (stderr, "\
356 Usage: %s [options] source [dest]\n\
357 %s [options] source... directory\n\
358 Options:\n\
359 [-bdfisvF] [-S backup-suffix] [-V {numbered,existing,simple}]\n\
360 [--version-control={numbered,existing,simple}] [--backup] [--directory]\n\
361 [--force] [--interactive] [--symbolic] [--verbose]\n\
362 [--suffix=backup-suffix] [--help] [--version]\n",
363 program_name, program_name);
364 exit (1);