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)
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. */
26 #include <sys/types.h>
30 #include "backupfile.h"
33 int link (); /* Some systems don't declare this anywhere. */
40 # define SYMBOLIC_SPACE_STRING symbolic_link ? _("symbolic ") : ""
42 # define SYMBOLIC_SPACE_STRING ""
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) \
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);\
66 enum backup_type
get_version ();
69 void strip_trailing_slashes ();
72 /* The name by which the program was run, for error messages. */
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. */
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},
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. */
128 do_link (char *source
, char *dest
)
130 struct stat dest_stats
;
131 char *dest_backup
= NULL
;
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. */
140 struct stat source_stats
;
142 if (stat (source
, &source_stats
) != 0)
144 error (0, errno
, "%s", source
);
147 if (!hard_dir_link
&& S_ISDIR (source_stats
.st_mode
))
149 error (0, 0, _("%s: hard link not allowed for directory"), source
);
154 if (lstat (dest
, &dest_stats
) != 0 && errno
!= ENOENT
)
156 error (0, errno
, "%s", dest
);
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
);
172 if ((lstat_status
== 0
173 && S_ISDIR (dest_stats
.st_mode
))
175 || (dereference_dest_dir_symlinks
176 && (S_ISLNK (dest_stats
.st_mode
)
181 /* Target is a directory; build the full filename. */
183 PATH_BASENAME_CONCAT (new_dest
, dest
, source
);
185 /* Set this to nonzero to force another call to lstat
186 with the new destination. */
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
);
199 fprintf (stderr
, _("%s: replace `%s'? "), program_name
, dest
);
203 else if (!remove_existing_files
)
205 error (0, 0, _("%s: File exists"), dest
);
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
);
217 if (rename (dest
, dest_backup
))
221 error (0, errno
, _("cannot backup `%s'"), dest
);
228 else if (unlink (dest
) && errno
!= ENOENT
)
230 error (0, errno
, _("cannot remove `%s'"), dest
);
234 else if (errno
!= ENOENT
)
236 error (0, errno
, "%s", dest
);
241 printf (_("create %slink %s to %s\n"), SYMBOLIC_SPACE_STRING
,
244 if ((*linkfunc
) (source
, dest
) == 0)
249 error (0, errno
, _("cannot %slink `%s' to `%s'"), SYMBOLIC_SPACE_STRING
,
254 if (rename (dest_backup
, dest
))
255 error (0, errno
, _("cannot un-backup `%s'"), dest
);
264 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
269 Usage: %s [OPTION]... SOURCE [DEST]\n\
270 or: %s [OPTION]... SOURCE... DIRECTORY\n\
272 program_name
, program_name
);
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"));
301 main (int argc
, char **argv
)
305 int make_backups
= 0;
308 program_name
= argv
[0];
309 setlocale (LC_ALL
, "");
310 bindtextdomain (PACKAGE
, LOCALEDIR
);
311 textdomain (PACKAGE
);
313 version
= getenv ("SIMPLE_BACKUP_SUFFIX");
315 simple_backup_suffix
= version
;
316 version
= getenv ("VERSION_CONTROL");
319 symbolic_link
= remove_existing_files
= interactive
= verbose
323 while ((c
= getopt_long (argc
, argv
,
324 "bdfinsvFS:V:", long_options
, (int *) 0))
329 case 0: /* Long-named option. */
339 remove_existing_files
= 1;
343 remove_existing_files
= 0;
347 dereference_dest_dir_symlinks
= 0;
353 error (1, 0, _("symbolic links are not supported on this system"));
360 simple_backup_suffix
= optarg
;
373 printf ("ln - %s\n", PACKAGE_VERSION
);
382 error (0, 0, _("missing file argument"));
387 backup_type
= get_version (version
);
394 if (optind
== argc
- 1)
395 errors
= do_link (argv
[optind
], ".");
396 else if (optind
== argc
- 2)
398 struct stat source_stats
;
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
);
421 errors
= do_link (source
, new_dest
);
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
);