xref: /freebsd/bin/ln/ln.c (revision 7bd6fde3)
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 					/* System link call. */
62 int (*linkf)(const char *, const char *);
63 char	linkch;
64 
65 int	linkit(const char *, const char *, int);
66 void	usage(void);
67 
68 int
69 main(int argc, char *argv[])
70 {
71 	struct stat sb;
72 	char *p, *sourcedir;
73 	int ch, exitval;
74 
75 	/*
76 	 * Test for the special case where the utility is called as
77 	 * "link", for which the functionality provided is greatly
78 	 * simplified.
79 	 */
80 	if ((p = rindex(argv[0], '/')) == NULL)
81 		p = argv[0];
82 	else
83 		++p;
84 	if (strcmp(p, "link") == 0) {
85 		while (getopt(argc, argv, "") != -1)
86 			usage();
87 		argc -= optind;
88 		argv += optind;
89 		if (argc != 2)
90 			usage();
91 		linkf = link;
92 		exit(linkit(argv[0], argv[1], 0));
93 	}
94 
95 	while ((ch = getopt(argc, argv, "Ffhinsv")) != -1)
96 		switch (ch) {
97 		case 'F':
98 			Fflag = 1;
99 			break;
100 		case 'f':
101 			fflag = 1;
102 			iflag = 0;
103 			break;
104 		case 'h':
105 		case 'n':
106 			hflag = 1;
107 			break;
108 		case 'i':
109 			iflag = 1;
110 			fflag = 0;
111 			break;
112 		case 's':
113 			sflag = 1;
114 			break;
115 		case 'v':
116 			vflag = 1;
117 			break;
118 		case '?':
119 		default:
120 			usage();
121 		}
122 
123 	argv += optind;
124 	argc -= optind;
125 
126 	linkf = sflag ? symlink : link;
127 	linkch = sflag ? '-' : '=';
128 	if (sflag == 0)
129 		Fflag = 0;
130 	if (Fflag == 1 && iflag == 0)
131 		fflag = 1;
132 
133 	switch(argc) {
134 	case 0:
135 		usage();
136 		/* NOTREACHED */
137 	case 1:				/* ln target */
138 		exit(linkit(argv[0], ".", 1));
139 	case 2:				/* ln target source */
140 		exit(linkit(argv[0], argv[1], 0));
141 	default:
142 		;
143 	}
144 					/* ln target1 target2 directory */
145 	sourcedir = argv[argc - 1];
146 	if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
147 		/*
148 		 * We were asked not to follow symlinks, but found one at
149 		 * the target--simulate "not a directory" error
150 		 */
151 		errno = ENOTDIR;
152 		err(1, "%s", sourcedir);
153 	}
154 	if (stat(sourcedir, &sb))
155 		err(1, "%s", sourcedir);
156 	if (!S_ISDIR(sb.st_mode))
157 		usage();
158 	for (exitval = 0; *argv != sourcedir; ++argv)
159 		exitval |= linkit(*argv, sourcedir, 1);
160 	exit(exitval);
161 }
162 
163 int
164 linkit(const char *target, const char *source, int isdir)
165 {
166 	struct stat sb;
167 	const char *p;
168 	int ch, exists, first;
169 	char path[PATH_MAX];
170 
171 	if (!sflag) {
172 		/* If target doesn't exist, quit now. */
173 		if (stat(target, &sb)) {
174 			warn("%s", target);
175 			return (1);
176 		}
177 		/* Only symbolic links to directories. */
178 		if (S_ISDIR(sb.st_mode)) {
179 			errno = EISDIR;
180 			warn("%s", target);
181 			return (1);
182 		}
183 	}
184 
185 	/*
186 	 * If the source is a directory (and not a symlink if hflag),
187 	 * append the target's name.
188 	 */
189 	if (isdir ||
190 	    (lstat(source, &sb) == 0 && S_ISDIR(sb.st_mode)) ||
191 	    (!hflag && stat(source, &sb) == 0 && S_ISDIR(sb.st_mode))) {
192 		if ((p = strrchr(target, '/')) == NULL)
193 			p = target;
194 		else
195 			++p;
196 		if (snprintf(path, sizeof(path), "%s/%s", source, p) >=
197 		    (ssize_t)sizeof(path)) {
198 			errno = ENAMETOOLONG;
199 			warn("%s", target);
200 			return (1);
201 		}
202 		source = path;
203 	}
204 
205 	exists = !lstat(source, &sb);
206 	/*
207 	 * If the file exists, then unlink it forcibly if -f was specified
208 	 * and interactively if -i was specified.
209 	 */
210 	if (fflag && exists) {
211 		if (Fflag && S_ISDIR(sb.st_mode)) {
212 			if (rmdir(source)) {
213 				warn("%s", source);
214 				return (1);
215 			}
216 		} else if (unlink(source)) {
217 			warn("%s", source);
218 			return (1);
219 		}
220 	} else if (iflag && exists) {
221 		fflush(stdout);
222 		fprintf(stderr, "replace %s? ", source);
223 
224 		first = ch = getchar();
225 		while(ch != '\n' && ch != EOF)
226 			ch = getchar();
227 		if (first != 'y' && first != 'Y') {
228 			fprintf(stderr, "not replaced\n");
229 			return (1);
230 		}
231 
232 		if (Fflag && S_ISDIR(sb.st_mode)) {
233 			if (rmdir(source)) {
234 				warn("%s", source);
235 				return (1);
236 			}
237 		} else if (unlink(source)) {
238 			warn("%s", source);
239 			return (1);
240 		}
241 	}
242 
243 	/* Attempt the link. */
244 	if ((*linkf)(target, source)) {
245 		warn("%s", source);
246 		return (1);
247 	}
248 	if (vflag)
249 		(void)printf("%s %c> %s\n", source, linkch, target);
250 	return (0);
251 }
252 
253 void
254 usage(void)
255 {
256 	(void)fprintf(stderr, "%s\n%s\n%s\n",
257 	    "usage: ln [-Ffhinsv] source_file [target_file]",
258 	    "       ln [-Ffhinsv] source_file ... target_dir",
259 	    "       link source_file target_file");
260 	exit(1);
261 }
262