2 * Copyright (c) 1987, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char const copyright[] =
33 "@(#) Copyright (c) 1987, 1993, 1994\n\
34 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)ln.c 8.2 (Berkeley) 3/31/94";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
44 #include <sys/param.h>
57 int fflag; /* Unlink existing files. */
58 int Fflag; /* Remove empty directories also. */
59 int hflag; /* Check new name for symlink first. */
60 int iflag; /* Interactive mode. */
61 int Pflag; /* Create hard links to symlinks. */
62 int sflag; /* Symbolic, not hard, link. */
63 int vflag; /* Verbose output. */
64 int wflag; /* Warn if symlink target does not
65 * exist, and -f is not enabled. */
68 int linkit(const char *, const char *, int);
72 main(int argc, char *argv[])
79 * Test for the special case where the utility is called as
80 * "link", for which the functionality provided is greatly
83 if ((p = strrchr(argv[0], '/')) == NULL)
87 if (strcmp(p, "link") == 0) {
88 while (getopt(argc, argv, "") != -1)
94 exit(linkit(argv[0], argv[1], 0));
97 while ((ch = getopt(argc, argv, "FLPfhinsvw")) != -1)
138 linkch = sflag ? '-' : '=';
141 if (Fflag == 1 && iflag == 0) {
143 wflag = 0; /* Implied when fflag != 0 */
150 case 1: /* ln source */
151 exit(linkit(argv[0], ".", 1));
152 case 2: /* ln source target */
153 exit(linkit(argv[0], argv[1], 0));
157 /* ln source1 source2 directory */
158 targetdir = argv[argc - 1];
159 if (hflag && lstat(targetdir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
161 * We were asked not to follow symlinks, but found one at
162 * the target--simulate "not a directory" error
165 err(1, "%s", targetdir);
167 if (stat(targetdir, &sb))
168 err(1, "%s", targetdir);
169 if (!S_ISDIR(sb.st_mode))
171 for (exitval = 0; *argv != targetdir; ++argv)
172 exitval |= linkit(*argv, targetdir, 1);
177 * Two pathnames refer to the same directory entry if the directories match
178 * and the final components' names match.
181 samedirent(const char *path1, const char *path2)
183 const char *file1, *file2;
184 char pathbuf[PATH_MAX];
185 struct stat sb1, sb2;
187 if (strcmp(path1, path2) == 0)
189 file1 = strrchr(path1, '/');
194 file2 = strrchr(path2, '/');
199 if (strcmp(file1, file2) != 0)
201 if (file1 - path1 >= PATH_MAX || file2 - path2 >= PATH_MAX)
204 memcpy(pathbuf, ".", 2);
206 memcpy(pathbuf, path1, file1 - path1);
207 pathbuf[file1 - path1] = '\0';
209 if (stat(pathbuf, &sb1) != 0)
212 memcpy(pathbuf, ".", 2);
214 memcpy(pathbuf, path2, file2 - path2);
215 pathbuf[file2 - path2] = '\0';
217 if (stat(pathbuf, &sb2) != 0)
219 return sb1.st_dev == sb2.st_dev && sb1.st_ino == sb2.st_ino;
223 linkit(const char *source, const char *target, int isdir)
227 int ch, exists, first;
233 /* If source doesn't exist, quit now. */
234 if ((Pflag ? lstat : stat)(source, &sb)) {
238 /* Only symbolic links to directories. */
239 if (S_ISDIR(sb.st_mode)) {
247 * If the target is a directory (and not a symlink if hflag),
248 * append the source's name.
251 (lstat(target, &sb) == 0 && S_ISDIR(sb.st_mode)) ||
252 (!hflag && stat(target, &sb) == 0 && S_ISDIR(sb.st_mode))) {
253 if (strlcpy(bbuf, source, sizeof(bbuf)) >= sizeof(bbuf) ||
254 (p = basename(bbuf)) == NULL ||
255 snprintf(path, sizeof(path), "%s/%s", target, p) >=
256 (ssize_t)sizeof(path)) {
257 errno = ENAMETOOLONG;
265 * If the link source doesn't exist, and a symbolic link was
266 * requested, and -w was specified, give a warning.
268 if (sflag && wflag) {
269 if (*source == '/') {
270 /* Absolute link source. */
271 if (stat(source, &sb) != 0)
272 warn("warning: %s inaccessible", source);
275 * Relative symlink source. Try to construct the
276 * absolute path of the source, by appending `source'
277 * to the parent directory of the target.
279 strlcpy(bbuf, target, sizeof(bbuf));
282 (void)snprintf(wbuf, sizeof(wbuf), "%s/%s",
284 if (stat(wbuf, &sb) != 0)
285 warn("warning: %s", source);
291 * If the file exists, first check it is not the same directory entry.
293 exists = !lstat(target, &sb);
295 if (!sflag && samedirent(source, target)) {
296 warnx("%s and %s are the same directory entry",
302 * Then unlink it forcibly if -f was specified
303 * and interactively if -i was specified.
305 if (fflag && exists) {
306 if (Fflag && S_ISDIR(sb.st_mode)) {
311 } else if (unlink(target)) {
315 } else if (iflag && exists) {
317 fprintf(stderr, "replace %s? ", target);
319 first = ch = getchar();
320 while(ch != '\n' && ch != EOF)
322 if (first != 'y' && first != 'Y') {
323 fprintf(stderr, "not replaced\n");
327 if (Fflag && S_ISDIR(sb.st_mode)) {
332 } else if (unlink(target)) {
338 /* Attempt the link. */
339 if (sflag ? symlink(source, target) :
340 linkat(AT_FDCWD, source, AT_FDCWD, target,
341 Pflag ? 0 : AT_SYMLINK_FOLLOW)) {
346 (void)printf("%s %c> %s\n", target, linkch, source);
353 (void)fprintf(stderr, "%s\n%s\n%s\n",
354 "usage: ln [-s [-F] | -L | -P] [-f | -i] [-hnv] source_file [target_file]",
355 " ln [-s [-F] | -L | -P] [-f | -i] [-hnv] source_file ... target_dir",
356 " link source_file target_file");