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: src/bin/ln/ln.c,v 1.15.2.4 2002/07/12 07:34:38 tjr Exp $
32 * $DragonFly: src/bin/ln/ln.c,v 1.11 2006/09/25 09:27:21 corecode Exp $
35 #include <sys/param.h>
45 static int fflag; /* Unlink existing files. */
46 static int hflag; /* Check new name for symlink first. */
47 static int iflag; /* Interactive mode. */
48 static int sflag; /* Symbolic, not hard, link. */
49 static int vflag; /* Verbose output. */
50 /* System link call. */
51 static int (*linkf)(const char *, const char *);
54 static int linkit(const char *, const char *, int);
55 static void usage(void);
58 main(int argc, char *argv[])
65 * Test for the special case where the utility is called as
66 * "link", for which the functionality provided is greatly
69 if ((p = strrchr(argv[0], '/')) == NULL)
73 if (strcmp(p, "link") == 0) {
74 while (getopt(argc, argv, "") != -1)
81 exit(linkit(argv[0], argv[1], 0));
84 while ((ch = getopt(argc, argv, "fhinsv")) != -1) {
113 linkf = sflag ? symlink : link;
114 linkch = sflag ? '-' : '=';
120 case 1: /* ln target */
121 exit(linkit(argv[0], ".", 1));
122 case 2: /* ln target source */
123 exit(linkit(argv[0], argv[1], 0));
127 /* ln target1 target2 directory */
128 sourcedir = argv[argc - 1];
129 if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
131 * We were asked not to follow symlinks, but found one at
132 * the target--simulate "not a directory" error
134 errc(1, ENOTDIR, "%s", sourcedir);
136 if (stat(sourcedir, &sb) != 0)
137 err(1, "%s", sourcedir);
138 if (!S_ISDIR(sb.st_mode))
140 for (exitval = 0; *argv != sourcedir; ++argv)
141 exitval |= linkit(*argv, sourcedir, 1);
146 * Nomenclature warning!
148 * In this source "target" and "source" are used the opposite way they
149 * are used in the ln(1) manual. Here "target" is the existing file and
150 * "source" specifies the to-be-created link to "target".
153 linkit(const char *target, const char *source, int isdir)
157 int ch, exists, first;
161 /* If target doesn't exist, quit now. */
162 if (stat(target, &sb) != 0) {
166 /* Only symbolic links to directories. */
167 if (S_ISDIR(sb.st_mode)) {
175 * If the source is a directory (and not a symlink if hflag),
176 * append the target's name.
179 (lstat(source, &sb) == 0 && S_ISDIR(sb.st_mode)) ||
180 (!hflag && stat(source, &sb) == 0 && S_ISDIR(sb.st_mode))) {
181 if ((p = strrchr(target, '/')) == NULL)
185 if (snprintf(path, sizeof(path), "%s/%s", source, p) >=
187 errno = ENAMETOOLONG;
194 exists = (lstat(source, &sb) == 0);
196 * If doing hard links and the source (destination) exists and it
197 * actually is the same file like the target (existing file), we
198 * complain that the files are identical. If -f is specified, we
199 * accept the job as already done and return with success.
201 if (exists && !sflag) {
204 if (stat(target, &tsb) != 0) {
205 warn("%s: disappeared", target);
209 if (tsb.st_dev == sb.st_dev && tsb.st_ino == sb.st_ino) {
210 warnx("%s and %s are identical (not linked).", target, source);
218 * If the file exists, then unlink it forcibly if -f was specified
219 * and interactively if -i was specified.
221 if (fflag && exists) {
222 if (unlink(source) != 0) {
226 } else if (iflag && exists) {
228 fprintf(stderr, "replace %s? ", source);
230 first = ch = getchar();
231 while(ch != '\n' && ch != EOF)
233 if (first != 'y' && first != 'Y') {
234 fprintf(stderr, "not replaced\n");
238 if (unlink(source) != 0) {
244 /* Attempt the link. */
245 if ((*linkf)(target, source) != 0) {
250 printf("%s %c> %s\n", source, linkch, target);
257 fprintf(stderr, "%s\n%s\n%s\n",
258 "usage: ln [-fhinsv] source_file [target_file]",
259 " ln [-fhinsv] source_file ... target_dir",
260 " link source_file target_file");