xref: /freebsd/bin/ln/ln.c (revision 13014ca04aad1931d41958b56f71a2c65b9a7a2c)
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 <limits.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 
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 *);
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 		linkf = link;
94 		exit(linkit(argv[0], argv[1], 0));
95 	}
96 
97 	while ((ch = getopt(argc, argv, "Ffhinsvw")) != -1)
98 		switch (ch) {
99 		case 'F':
100 			Fflag = 1;
101 			break;
102 		case 'f':
103 			fflag = 1;
104 			iflag = 0;
105 			wflag = 0;
106 			break;
107 		case 'h':
108 		case 'n':
109 			hflag = 1;
110 			break;
111 		case 'i':
112 			iflag = 1;
113 			fflag = 0;
114 			break;
115 		case 's':
116 			sflag = 1;
117 			break;
118 		case 'v':
119 			vflag = 1;
120 			break;
121 		case 'w':
122 			wflag = 1;
123 			break;
124 		case '?':
125 		default:
126 			usage();
127 		}
128 
129 	argv += optind;
130 	argc -= optind;
131 
132 	linkf = sflag ? symlink : link;
133 	linkch = sflag ? '-' : '=';
134 	if (sflag == 0)
135 		Fflag = 0;
136 	if (Fflag == 1 && iflag == 0) {
137 		fflag = 1;
138 		wflag = 0;		/* Implied when fflag != 0 */
139 	}
140 
141 	switch(argc) {
142 	case 0:
143 		usage();
144 		/* NOTREACHED */
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));
149 	default:
150 		;
151 	}
152 					/* ln source1 source2 directory */
153 	targetdir = argv[argc - 1];
154 	if (hflag && lstat(targetdir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
155 		/*
156 		 * We were asked not to follow symlinks, but found one at
157 		 * the target--simulate "not a directory" error
158 		 */
159 		errno = ENOTDIR;
160 		err(1, "%s", targetdir);
161 	}
162 	if (stat(targetdir, &sb))
163 		err(1, "%s", targetdir);
164 	if (!S_ISDIR(sb.st_mode))
165 		usage();
166 	for (exitval = 0; *argv != targetdir; ++argv)
167 		exitval |= linkit(*argv, targetdir, 1);
168 	exit(exitval);
169 }
170 
171 int
172 linkit(const char *source, const char *target, int isdir)
173 {
174 	struct stat sb;
175 	const char *p;
176 	int ch, exists, first;
177 	char path[PATH_MAX];
178 	char wbuf[PATH_MAX];
179 
180 	if (!sflag) {
181 		/* If source doesn't exist, quit now. */
182 		if (stat(source, &sb)) {
183 			warn("%s", source);
184 			return (1);
185 		}
186 		/* Only symbolic links to directories. */
187 		if (S_ISDIR(sb.st_mode)) {
188 			errno = EISDIR;
189 			warn("%s", source);
190 			return (1);
191 		}
192 	}
193 
194 	/*
195 	 * If the target is a directory (and not a symlink if hflag),
196 	 * append the source's name.
197 	 */
198 	if (isdir ||
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)
202 			p = source;
203 		else
204 			++p;
205 		if (snprintf(path, sizeof(path), "%s/%s", target, p) >=
206 		    (ssize_t)sizeof(path)) {
207 			errno = ENAMETOOLONG;
208 			warn("%s", source);
209 			return (1);
210 		}
211 		target = path;
212 	}
213 
214 	exists = !lstat(target, &sb);
215 	/*
216 	 * If the link source doesn't exist, and a symbolic link was
217 	 * requested, and -w was specified, give a warning.
218 	 */
219 	if (sflag && wflag) {
220 		if (*source == '/') {
221 			/* Absolute link source. */
222 			if (stat(source, &sb) != 0)
223 				 warn("warning: %s inaccessible", source);
224 		} else {
225 			/*
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.
229 			 */
230 			p = strrchr(target, '/');
231 			if (p != NULL)
232 				p++;
233 			else
234 				p = 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);
239 		}
240 	}
241 	/*
242 	 * If the file exists, then unlink it forcibly if -f was specified
243 	 * and interactively if -i was specified.
244 	 */
245 	if (fflag && exists) {
246 		if (Fflag && S_ISDIR(sb.st_mode)) {
247 			if (rmdir(target)) {
248 				warn("%s", target);
249 				return (1);
250 			}
251 		} else if (unlink(target)) {
252 			warn("%s", target);
253 			return (1);
254 		}
255 	} else if (iflag && exists) {
256 		fflush(stdout);
257 		fprintf(stderr, "replace %s? ", target);
258 
259 		first = ch = getchar();
260 		while(ch != '\n' && ch != EOF)
261 			ch = getchar();
262 		if (first != 'y' && first != 'Y') {
263 			fprintf(stderr, "not replaced\n");
264 			return (1);
265 		}
266 
267 		if (Fflag && S_ISDIR(sb.st_mode)) {
268 			if (rmdir(target)) {
269 				warn("%s", target);
270 				return (1);
271 			}
272 		} else if (unlink(target)) {
273 			warn("%s", target);
274 			return (1);
275 		}
276 	}
277 
278 	/* Attempt the link. */
279 	if ((*linkf)(source, target)) {
280 		warn("%s", target);
281 		return (1);
282 	}
283 	if (vflag)
284 		(void)printf("%s %c> %s\n", target, linkch, source);
285 	return (0);
286 }
287 
288 void
289 usage(void)
290 {
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");
295 	exit(1);
296 }
297