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>
56 int fflag; /* Unlink existing files. */
57 int Fflag; /* Remove empty directories also. */
58 int hflag; /* Check new name for symlink first. */
59 int iflag; /* Interactive mode. */
60 int Pflag; /* Create hard links to symlinks. */
61 int sflag; /* Symbolic, not hard, link. */
62 int vflag; /* Verbose output. */
63 int wflag; /* Warn if symlink target does not
64 * exist, and -f is not enabled. */
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)
93 exit(linkit(argv[0], argv[1], 0));
96 while ((ch = getopt(argc, argv, "FLPfhinsvw")) != -1)
137 linkch = sflag ? '-' : '=';
140 if (Fflag == 1 && iflag == 0) {
142 wflag = 0; /* Implied when fflag != 0 */
149 case 1: /* ln source */
150 exit(linkit(argv[0], ".", 1));
151 case 2: /* ln source target */
152 exit(linkit(argv[0], argv[1], 0));
156 /* ln source1 source2 directory */
157 targetdir = argv[argc - 1];
158 if (hflag && lstat(targetdir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
160 * We were asked not to follow symlinks, but found one at
161 * the target--simulate "not a directory" error
164 err(1, "%s", targetdir);
166 if (stat(targetdir, &sb))
167 err(1, "%s", targetdir);
168 if (!S_ISDIR(sb.st_mode))
170 for (exitval = 0; *argv != targetdir; ++argv)
171 exitval |= linkit(*argv, targetdir, 1);
176 * Two pathnames refer to the same directory entry if the directories match
177 * and the final components' names match.
180 samedirent(const char *path1, const char *path2)
182 const char *file1, *file2;
183 char pathbuf[PATH_MAX];
184 struct stat sb1, sb2;
186 if (strcmp(path1, path2) == 0)
188 file1 = strrchr(path1, '/');
193 file2 = strrchr(path2, '/');
198 if (strcmp(file1, file2) != 0)
200 if (file1 - path1 >= PATH_MAX || file2 - path2 >= PATH_MAX)
203 memcpy(pathbuf, ".", 2);
205 memcpy(pathbuf, path1, file1 - path1);
206 pathbuf[file1 - path1] = '\0';
208 if (stat(pathbuf, &sb1) != 0)
211 memcpy(pathbuf, ".", 2);
213 memcpy(pathbuf, path2, file2 - path2);
214 pathbuf[file2 - path2] = '\0';
216 if (stat(pathbuf, &sb2) != 0)
218 return sb1.st_dev == sb2.st_dev && sb1.st_ino == sb2.st_ino;
222 linkit(const char *source, const char *target, int isdir)
226 int ch, exists, first;
231 /* If source doesn't exist, quit now. */
232 if ((Pflag ? lstat : stat)(source, &sb)) {
236 /* Only symbolic links to directories. */
237 if (S_ISDIR(sb.st_mode)) {
245 * If the target is a directory (and not a symlink if hflag),
246 * append the source's name.
249 (lstat(target, &sb) == 0 && S_ISDIR(sb.st_mode)) ||
250 (!hflag && stat(target, &sb) == 0 && S_ISDIR(sb.st_mode))) {
251 if ((p = strrchr(source, '/')) == NULL)
255 if (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 p = strrchr(target, '/');
284 (void)snprintf(wbuf, sizeof(wbuf), "%.*s%s",
285 (int)(p - target), target, source);
286 if (stat(wbuf, &sb) != 0)
287 warn("warning: %s", source);
292 * If the file exists, first check it is not the same directory entry.
294 exists = !lstat(target, &sb);
296 if (!sflag && samedirent(source, target)) {
297 warnx("%s and %s are the same directory entry",
303 * Then unlink it forcibly if -f was specified
304 * and interactively if -i was specified.
306 if (fflag && exists) {
307 if (Fflag && S_ISDIR(sb.st_mode)) {
312 } else if (unlink(target)) {
316 } else if (iflag && exists) {
318 fprintf(stderr, "replace %s? ", target);
320 first = ch = getchar();
321 while(ch != '\n' && ch != EOF)
323 if (first != 'y' && first != 'Y') {
324 fprintf(stderr, "not replaced\n");
328 if (Fflag && S_ISDIR(sb.st_mode)) {
333 } else if (unlink(target)) {
339 /* Attempt the link. */
340 if (sflag ? symlink(source, target) :
341 linkat(AT_FDCWD, source, AT_FDCWD, target,
342 Pflag ? 0 : AT_SYMLINK_FOLLOW)) {
347 (void)printf("%s %c> %s\n", target, linkch, source);
354 (void)fprintf(stderr, "%s\n%s\n%s\n",
355 "usage: ln [-s [-F] | -L | -P] [-f | -i] [-hnv] source_file [target_file]",
356 " ln [-s [-F] | -L | -P] [-f | -i] [-hnv] source_file ... target_dir",
357 " link source_file target_file");