2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1987, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static char const copyright[] =
35 "@(#) Copyright (c) 1987, 1993, 1994\n\
36 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)ln.c 8.2 (Berkeley) 3/31/94";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
59 static int fflag; /* Unlink existing files. */
60 static int Fflag; /* Remove empty directories also. */
61 static int hflag; /* Check new name for symlink first. */
62 static int iflag; /* Interactive mode. */
63 static int Pflag; /* Create hard links to symlinks. */
64 static int sflag; /* Symbolic, not hard, link. */
65 static int vflag; /* Verbose output. */
66 static int wflag; /* Warn if symlink target does not
67 * exist, and -f is not enabled. */
70 static int linkit(const char *, const char *, int);
71 static void usage(void);
74 main(int argc, char *argv[])
81 * Test for the special case where the utility is called as
82 * "link", for which the functionality provided is greatly
85 if ((p = strrchr(argv[0], '/')) == NULL)
89 if (strcmp(p, "link") == 0) {
90 while (getopt(argc, argv, "") != -1)
96 exit(linkit(argv[0], argv[1], 0));
99 while ((ch = getopt(argc, argv, "FLPfhinsvw")) != -1)
140 linkch = sflag ? '-' : '=';
143 if (Fflag == 1 && iflag == 0) {
145 wflag = 0; /* Implied when fflag != 0 */
152 case 1: /* ln source */
153 exit(linkit(argv[0], ".", 1));
154 case 2: /* ln source target */
155 exit(linkit(argv[0], argv[1], 0));
159 /* ln source1 source2 directory */
160 targetdir = argv[argc - 1];
161 if (hflag && lstat(targetdir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
163 * We were asked not to follow symlinks, but found one at
164 * the target--simulate "not a directory" error
167 err(1, "%s", targetdir);
169 if (stat(targetdir, &sb))
170 err(1, "%s", targetdir);
171 if (!S_ISDIR(sb.st_mode))
173 for (exitval = 0; *argv != targetdir; ++argv)
174 exitval |= linkit(*argv, targetdir, 1);
179 * Two pathnames refer to the same directory entry if the directories match
180 * and the final components' names match.
183 samedirent(const char *path1, const char *path2)
185 const char *file1, *file2;
186 char pathbuf[PATH_MAX];
187 struct stat sb1, sb2;
189 if (strcmp(path1, path2) == 0)
191 file1 = strrchr(path1, '/');
196 file2 = strrchr(path2, '/');
201 if (strcmp(file1, file2) != 0)
203 if (file1 - path1 >= PATH_MAX || file2 - path2 >= PATH_MAX)
206 memcpy(pathbuf, ".", 2);
208 memcpy(pathbuf, path1, file1 - path1);
209 pathbuf[file1 - path1] = '\0';
211 if (stat(pathbuf, &sb1) != 0)
214 memcpy(pathbuf, ".", 2);
216 memcpy(pathbuf, path2, file2 - path2);
217 pathbuf[file2 - path2] = '\0';
219 if (stat(pathbuf, &sb2) != 0)
221 return sb1.st_dev == sb2.st_dev && sb1.st_ino == sb2.st_ino;
225 linkit(const char *source, const char *target, int isdir)
229 int ch, exists, first;
235 /* If source doesn't exist, quit now. */
236 if ((Pflag ? lstat : stat)(source, &sb)) {
240 /* Only symbolic links to directories. */
241 if (S_ISDIR(sb.st_mode)) {
249 * If the target is a directory (and not a symlink if hflag),
250 * append the source's name, unless Fflag is set.
252 if (!Fflag && (isdir ||
253 (lstat(target, &sb) == 0 && S_ISDIR(sb.st_mode)) ||
254 (!hflag && stat(target, &sb) == 0 && S_ISDIR(sb.st_mode)))) {
255 if (strlcpy(bbuf, source, sizeof(bbuf)) >= sizeof(bbuf) ||
256 (p = basename(bbuf)) == NULL ||
257 snprintf(path, sizeof(path), "%s/%s", target, p) >=
258 (ssize_t)sizeof(path)) {
259 errno = ENAMETOOLONG;
267 * If the link source doesn't exist, and a symbolic link was
268 * requested, and -w was specified, give a warning.
270 if (sflag && wflag) {
271 if (*source == '/') {
272 /* Absolute link source. */
273 if (stat(source, &sb) != 0)
274 warn("warning: %s inaccessible", source);
277 * Relative symlink source. Try to construct the
278 * absolute path of the source, by appending `source'
279 * to the parent directory of the target.
281 strlcpy(bbuf, target, sizeof(bbuf));
284 (void)snprintf(wbuf, sizeof(wbuf), "%s/%s",
286 if (stat(wbuf, &sb) != 0)
287 warn("warning: %s", source);
293 * If the file exists, first check it is not the same directory entry.
295 exists = !lstat(target, &sb);
297 if (!sflag && samedirent(source, target)) {
298 warnx("%s and %s are the same directory entry",
304 * Then unlink it forcibly if -f was specified
305 * and interactively if -i was specified.
307 if (fflag && exists) {
308 if (Fflag && S_ISDIR(sb.st_mode)) {
313 } else if (unlink(target)) {
317 } else if (iflag && exists) {
319 fprintf(stderr, "replace %s? ", target);
321 first = ch = getchar();
322 while(ch != '\n' && ch != EOF)
324 if (first != 'y' && first != 'Y') {
325 fprintf(stderr, "not replaced\n");
329 if (Fflag && S_ISDIR(sb.st_mode)) {
334 } else if (unlink(target)) {
340 /* Attempt the link. */
341 if (sflag ? symlink(source, target) :
342 linkat(AT_FDCWD, source, AT_FDCWD, target,
343 Pflag ? 0 : AT_SYMLINK_FOLLOW)) {
348 (void)printf("%s %c> %s\n", target, linkch, source);
355 (void)fprintf(stderr, "%s\n%s\n%s\n",
356 "usage: ln [-s [-F] | -L | -P] [-f | -i] [-hnv] source_file [target_file]",
357 " ln [-s [-F] | -L | -P] [-f | -i] [-hnv] source_file ... target_dir",
358 " link source_file target_file");