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>
55 int fflag; /* Unlink existing files. */
56 int Fflag; /* Remove empty directories also. */
57 int hflag; /* Check new name for symlink first. */
58 int iflag; /* Interactive mode. */
59 int sflag; /* Symbolic, not hard, link. */
60 int vflag; /* Verbose output. */
61 int wflag; /* Warn if symlink target does not
62 * exist, and -f is not enabled. */
63 /* System link call. */
64 int (*linkf)(const char *, const char *);
67 int linkit(const char *, const char *, int);
71 main(int argc, char *argv[])
78 * Test for the special case where the utility is called as
79 * "link", for which the functionality provided is greatly
82 if ((p = rindex(argv[0], '/')) == NULL)
86 if (strcmp(p, "link") == 0) {
87 while (getopt(argc, argv, "") != -1)
94 exit(linkit(argv[0], argv[1], 0));
97 while ((ch = getopt(argc, argv, "Ffhinsvw")) != -1)
132 linkf = sflag ? symlink : link;
133 linkch = sflag ? '-' : '=';
136 if (Fflag == 1 && iflag == 0) {
138 wflag = 0; /* Implied when fflag != 0 */
145 case 1: /* ln source */
146 exit(linkit(argv[0], ".", 1));
147 case 2: /* ln source target */
148 exit(linkit(argv[0], argv[1], 0));
152 /* ln source1 source2 directory */
153 targetdir = argv[argc - 1];
154 if (hflag && lstat(targetdir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
156 * We were asked not to follow symlinks, but found one at
157 * the target--simulate "not a directory" error
160 err(1, "%s", targetdir);
162 if (stat(targetdir, &sb))
163 err(1, "%s", targetdir);
164 if (!S_ISDIR(sb.st_mode))
166 for (exitval = 0; *argv != targetdir; ++argv)
167 exitval |= linkit(*argv, targetdir, 1);
172 linkit(const char *source, const char *target, int isdir)
176 int ch, exists, first;
181 /* If source doesn't exist, quit now. */
182 if (stat(source, &sb)) {
186 /* Only symbolic links to directories. */
187 if (S_ISDIR(sb.st_mode)) {
195 * If the target is a directory (and not a symlink if hflag),
196 * append the source's name.
199 (lstat(target, &sb) == 0 && S_ISDIR(sb.st_mode)) ||
200 (!hflag && stat(target, &sb) == 0 && S_ISDIR(sb.st_mode))) {
201 if ((p = strrchr(source, '/')) == NULL)
205 if (snprintf(path, sizeof(path), "%s/%s", target, p) >=
206 (ssize_t)sizeof(path)) {
207 errno = ENAMETOOLONG;
214 exists = !lstat(target, &sb);
216 * If the link source doesn't exist, and a symbolic link was
217 * requested, and -w was specified, give a warning.
219 if (sflag && wflag) {
220 if (*source == '/') {
221 /* Absolute link source. */
222 if (stat(source, &sb) != 0)
223 warn("warning: %s inaccessible", source);
226 * Relative symlink source. Try to construct the
227 * absolute path of the source, by appending `source'
228 * to the parent directory of the target.
230 p = strrchr(target, '/');
235 (void)snprintf(wbuf, sizeof(wbuf), "%.*s%s",
236 (int)(p - target), target, source);
237 if (stat(wbuf, &sb) != 0)
238 warn("warning: %s", source);
242 * If the file exists, then unlink it forcibly if -f was specified
243 * and interactively if -i was specified.
245 if (fflag && exists) {
246 if (Fflag && S_ISDIR(sb.st_mode)) {
251 } else if (unlink(target)) {
255 } else if (iflag && exists) {
257 fprintf(stderr, "replace %s? ", target);
259 first = ch = getchar();
260 while(ch != '\n' && ch != EOF)
262 if (first != 'y' && first != 'Y') {
263 fprintf(stderr, "not replaced\n");
267 if (Fflag && S_ISDIR(sb.st_mode)) {
272 } else if (unlink(target)) {
278 /* Attempt the link. */
279 if ((*linkf)(source, target)) {
284 (void)printf("%s %c> %s\n", target, linkch, source);
291 (void)fprintf(stderr, "%s\n%s\n%s\n",
292 "usage: ln [-s [-F]] [-f | -i] [-hnv] source_file [target_file]",
293 " ln [-s [-F]] [-f | -i] [-hnv] source_file ... target_dir",
294 " link source_file target_file");