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 * 3. 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
29 * @(#) Copyright (c) 1987, 1993, 1994 The Regents of the University of California. All rights reserved.
30 * @(#)ln.c 8.2 (Berkeley) 3/31/94
31 * $FreeBSD: head/bin/ln/ln.c 251261 2013-06-02 17:55:00Z eadler $
34 #include <sys/param.h>
46 static int fflag; /* Unlink existing files. */
47 static int Fflag; /* Remove empty directories also. */
48 static int hflag; /* Check new name for symlink first. */
49 static int iflag; /* Interactive mode. */
50 static int Pflag; /* Create hard links to symlinks. */
51 static int sflag; /* Symbolic, not hard, link. */
52 static int vflag; /* Verbose output. */
53 static int wflag; /* Warn if symlink target does not
54 * exist, and -f is not enabled. */
57 static int linkit(const char *, const char *, int);
58 static void usage(void) __dead2;
61 main(int argc, char *argv[])
68 * Test for the special case where the utility is called as
69 * "link", for which the functionality provided is greatly
72 if ((p = strrchr(argv[0], '/')) == NULL)
76 if (strcmp(p, "link") == 0) {
77 while (getopt(argc, argv, "") != -1)
83 exit(linkit(argv[0], argv[1], 0));
86 while ((ch = getopt(argc, argv, "FLPfhinsvw")) != -1)
127 linkch = sflag ? '-' : '=';
130 if (Fflag == 1 && iflag == 0) {
132 wflag = 0; /* Implied when fflag != 0 */
139 case 1: /* ln source */
140 exit(linkit(argv[0], ".", 1));
141 case 2: /* ln source target */
142 exit(linkit(argv[0], argv[1], 0));
146 /* ln source1 source2 directory */
147 targetdir = argv[argc - 1];
148 if (hflag && lstat(targetdir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
150 * We were asked not to follow symlinks, but found one at
151 * the target--simulate "not a directory" error
154 err(1, "%s", targetdir);
156 if (stat(targetdir, &sb))
157 err(1, "%s", targetdir);
158 if (!S_ISDIR(sb.st_mode))
160 for (exitval = 0; *argv != targetdir; ++argv)
161 exitval |= linkit(*argv, targetdir, 1);
166 * Two pathnames refer to the same directory entry if the directories match
167 * and the final components' names match.
170 samedirent(const char *path1, const char *path2)
172 const char *file1, *file2;
173 char pathbuf[PATH_MAX];
174 struct stat sb1, sb2;
176 if (strcmp(path1, path2) == 0)
178 file1 = strrchr(path1, '/');
183 file2 = strrchr(path2, '/');
188 if (strcmp(file1, file2) != 0)
190 if (file1 - path1 >= PATH_MAX || file2 - path2 >= PATH_MAX)
193 memcpy(pathbuf, ".", 2);
195 memcpy(pathbuf, path1, file1 - path1);
196 pathbuf[file1 - path1] = '\0';
198 if (stat(pathbuf, &sb1) != 0)
201 memcpy(pathbuf, ".", 2);
203 memcpy(pathbuf, path2, file2 - path2);
204 pathbuf[file2 - path2] = '\0';
206 if (stat(pathbuf, &sb2) != 0)
208 return sb1.st_dev == sb2.st_dev && sb1.st_ino == sb2.st_ino;
212 linkit(const char *source, const char *target, int isdir)
216 int ch, exists, first;
222 /* If source doesn't exist, quit now. */
223 if ((Pflag ? lstat : stat)(source, &sb)) {
227 /* Only symbolic links to directories. */
228 if (S_ISDIR(sb.st_mode)) {
236 * If the target is a directory (and not a symlink if hflag),
237 * append the source's name.
240 (lstat(target, &sb) == 0 && S_ISDIR(sb.st_mode)) ||
241 (!hflag && stat(target, &sb) == 0 && S_ISDIR(sb.st_mode))) {
242 if (strlcpy(bbuf, source, sizeof(bbuf)) >= sizeof(bbuf) ||
243 (p = basename(bbuf)) == NULL ||
244 snprintf(path, sizeof(path), "%s/%s", target, p) >=
245 (ssize_t)sizeof(path)) {
246 errno = ENAMETOOLONG;
254 * If the link source doesn't exist, and a symbolic link was
255 * requested, and -w was specified, give a warning.
257 if (sflag && wflag) {
258 if (*source == '/') {
259 /* Absolute link source. */
260 if (stat(source, &sb) != 0)
261 warn("warning: %s inaccessible", source);
264 * Relative symlink source. Try to construct the
265 * absolute path of the source, by appending `source'
266 * to the parent directory of the target.
268 strlcpy(bbuf, target, sizeof(bbuf));
271 snprintf(wbuf, sizeof(wbuf), "%s/%s",
273 if (stat(wbuf, &sb) != 0)
274 warn("warning: %s", source);
280 * If the file exists, first check it is not the same directory entry.
282 exists = !lstat(target, &sb);
284 if (!sflag && samedirent(source, target)) {
285 warnx("%s and %s are the same directory entry",
291 * Then unlink it forcibly if -f was specified
292 * and interactively if -i was specified.
294 if (fflag && exists) {
295 if (Fflag && S_ISDIR(sb.st_mode)) {
300 } else if (unlink(target)) {
304 } else if (iflag && exists) {
306 fprintf(stderr, "replace %s? ", target);
308 first = ch = getchar();
309 while(ch != '\n' && ch != EOF)
311 if (first != 'y' && first != 'Y') {
312 fprintf(stderr, "not replaced\n");
316 if (Fflag && S_ISDIR(sb.st_mode)) {
321 } else if (unlink(target)) {
327 /* Attempt the link. */
328 if (sflag ? symlink(source, target) :
329 linkat(AT_FDCWD, source, AT_FDCWD, target,
330 Pflag ? 0 : AT_SYMLINK_FOLLOW)) {
335 printf("%s %c> %s\n", target, linkch, source);
342 fprintf(stderr, "%s\n%s\n%s\n",
343 "usage: ln [-s [-F] | -L | -P] [-f | -i] [-hnv] source_file [target_file]",
344 " ln [-s [-F] | -L | -P] [-f | -i] [-hnv] source_file ... target_dir",
345 " link source_file target_file");