xref: /freebsd/bin/ln/ln.c (revision 941e286383714ef25f1ffe9ba6ae5040afdd7060)
1 /*-
2  * Copyright (c) 1987, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
16  *
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
27  * SUCH DAMAGE.
28  */
29 
30 #if 0
31 #ifndef lint
32 static char const copyright[] =
33 "@(#) Copyright (c) 1987, 1993, 1994\n\
34 	The Regents of the University of California.  All rights reserved.\n";
35 #endif /* not lint */
36 
37 #ifndef lint
38 static char sccsid[] = "@(#)ln.c	8.2 (Berkeley) 3/31/94";
39 #endif /* not lint */
40 #endif
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include <sys/param.h>
45 #include <sys/stat.h>
46 
47 #include <err.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <limits.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 
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. */
65 char	linkch;
66 
67 int	linkit(const char *, const char *, int);
68 void	usage(void);
69 
70 int
71 main(int argc, char *argv[])
72 {
73 	struct stat sb;
74 	char *p, *targetdir;
75 	int ch, exitval;
76 
77 	/*
78 	 * Test for the special case where the utility is called as
79 	 * "link", for which the functionality provided is greatly
80 	 * simplified.
81 	 */
82 	if ((p = rindex(argv[0], '/')) == NULL)
83 		p = argv[0];
84 	else
85 		++p;
86 	if (strcmp(p, "link") == 0) {
87 		while (getopt(argc, argv, "") != -1)
88 			usage();
89 		argc -= optind;
90 		argv += optind;
91 		if (argc != 2)
92 			usage();
93 		exit(linkit(argv[0], argv[1], 0));
94 	}
95 
96 	while ((ch = getopt(argc, argv, "FLPfhinsvw")) != -1)
97 		switch (ch) {
98 		case 'F':
99 			Fflag = 1;
100 			break;
101 		case 'L':
102 			Pflag = 0;
103 			break;
104 		case 'P':
105 			Pflag = 1;
106 			break;
107 		case 'f':
108 			fflag = 1;
109 			iflag = 0;
110 			wflag = 0;
111 			break;
112 		case 'h':
113 		case 'n':
114 			hflag = 1;
115 			break;
116 		case 'i':
117 			iflag = 1;
118 			fflag = 0;
119 			break;
120 		case 's':
121 			sflag = 1;
122 			break;
123 		case 'v':
124 			vflag = 1;
125 			break;
126 		case 'w':
127 			wflag = 1;
128 			break;
129 		case '?':
130 		default:
131 			usage();
132 		}
133 
134 	argv += optind;
135 	argc -= optind;
136 
137 	linkch = sflag ? '-' : '=';
138 	if (sflag == 0)
139 		Fflag = 0;
140 	if (Fflag == 1 && iflag == 0) {
141 		fflag = 1;
142 		wflag = 0;		/* Implied when fflag != 0 */
143 	}
144 
145 	switch(argc) {
146 	case 0:
147 		usage();
148 		/* NOTREACHED */
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));
153 	default:
154 		;
155 	}
156 					/* ln source1 source2 directory */
157 	targetdir = argv[argc - 1];
158 	if (hflag && lstat(targetdir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
159 		/*
160 		 * We were asked not to follow symlinks, but found one at
161 		 * the target--simulate "not a directory" error
162 		 */
163 		errno = ENOTDIR;
164 		err(1, "%s", targetdir);
165 	}
166 	if (stat(targetdir, &sb))
167 		err(1, "%s", targetdir);
168 	if (!S_ISDIR(sb.st_mode))
169 		usage();
170 	for (exitval = 0; *argv != targetdir; ++argv)
171 		exitval |= linkit(*argv, targetdir, 1);
172 	exit(exitval);
173 }
174 
175 int
176 linkit(const char *source, const char *target, int isdir)
177 {
178 	struct stat sb;
179 	const char *p;
180 	int ch, exists, first;
181 	char path[PATH_MAX];
182 	char wbuf[PATH_MAX];
183 
184 	if (!sflag) {
185 		/* If source doesn't exist, quit now. */
186 		if ((Pflag ? lstat : stat)(source, &sb)) {
187 			warn("%s", source);
188 			return (1);
189 		}
190 		/* Only symbolic links to directories. */
191 		if (S_ISDIR(sb.st_mode)) {
192 			errno = EISDIR;
193 			warn("%s", source);
194 			return (1);
195 		}
196 	}
197 
198 	/*
199 	 * If the target is a directory (and not a symlink if hflag),
200 	 * append the source's name.
201 	 */
202 	if (isdir ||
203 	    (lstat(target, &sb) == 0 && S_ISDIR(sb.st_mode)) ||
204 	    (!hflag && stat(target, &sb) == 0 && S_ISDIR(sb.st_mode))) {
205 		if ((p = strrchr(source, '/')) == NULL)
206 			p = source;
207 		else
208 			++p;
209 		if (snprintf(path, sizeof(path), "%s/%s", target, p) >=
210 		    (ssize_t)sizeof(path)) {
211 			errno = ENAMETOOLONG;
212 			warn("%s", source);
213 			return (1);
214 		}
215 		target = path;
216 	}
217 
218 	exists = !lstat(target, &sb);
219 	/*
220 	 * If the link source doesn't exist, and a symbolic link was
221 	 * requested, and -w was specified, give a warning.
222 	 */
223 	if (sflag && wflag) {
224 		if (*source == '/') {
225 			/* Absolute link source. */
226 			if (stat(source, &sb) != 0)
227 				 warn("warning: %s inaccessible", source);
228 		} else {
229 			/*
230 			 * Relative symlink source.  Try to construct the
231 			 * absolute path of the source, by appending `source'
232 			 * to the parent directory of the target.
233 			 */
234 			p = strrchr(target, '/');
235 			if (p != NULL)
236 				p++;
237 			else
238 				p = target;
239 			(void)snprintf(wbuf, sizeof(wbuf), "%.*s%s",
240 			    (int)(p - target), target, source);
241 			if (stat(wbuf, &sb) != 0)
242 				warn("warning: %s", source);
243 		}
244 	}
245 	/*
246 	 * If the file exists, then unlink it forcibly if -f was specified
247 	 * and interactively if -i was specified.
248 	 */
249 	if (fflag && exists) {
250 		if (Fflag && S_ISDIR(sb.st_mode)) {
251 			if (rmdir(target)) {
252 				warn("%s", target);
253 				return (1);
254 			}
255 		} else if (unlink(target)) {
256 			warn("%s", target);
257 			return (1);
258 		}
259 	} else if (iflag && exists) {
260 		fflush(stdout);
261 		fprintf(stderr, "replace %s? ", target);
262 
263 		first = ch = getchar();
264 		while(ch != '\n' && ch != EOF)
265 			ch = getchar();
266 		if (first != 'y' && first != 'Y') {
267 			fprintf(stderr, "not replaced\n");
268 			return (1);
269 		}
270 
271 		if (Fflag && S_ISDIR(sb.st_mode)) {
272 			if (rmdir(target)) {
273 				warn("%s", target);
274 				return (1);
275 			}
276 		} else if (unlink(target)) {
277 			warn("%s", target);
278 			return (1);
279 		}
280 	}
281 
282 	/* Attempt the link. */
283 	if (sflag ? symlink(source, target) :
284 	    linkat(AT_FDCWD, source, AT_FDCWD, target,
285 	    Pflag ? 0 : AT_SYMLINK_FOLLOW)) {
286 		warn("%s", target);
287 		return (1);
288 	}
289 	if (vflag)
290 		(void)printf("%s %c> %s\n", target, linkch, source);
291 	return (0);
292 }
293 
294 void
295 usage(void)
296 {
297 	(void)fprintf(stderr, "%s\n%s\n%s\n",
298 	    "usage: ln [-s [-F] | -L | -P] [-f | -i] [-hnv] source_file [target_file]",
299 	    "       ln [-s [-F] | -L | -P] [-f | -i] [-hnv] source_file ... target_dir",
300 	    "       link source_file target_file");
301 	exit(1);
302 }
303