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