xref: /freebsd/bin/ln/ln.c (revision 6990ffd8a95caaba6858ad44ff1b3157d1efba8f)
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char const copyright[] =
36 "@(#) Copyright (c) 1987, 1993, 1994\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)ln.c	8.2 (Berkeley) 3/31/94";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47 
48 #include <sys/param.h>
49 #include <sys/stat.h>
50 
51 #include <err.h>
52 #include <errno.h>
53 #include <limits.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 
59 int	fflag;				/* Unlink existing files. */
60 int	hflag;				/* Check new name for symlink first. */
61 int	iflag;				/* Interactive mode. */
62 int	sflag;				/* Symbolic, not hard, link. */
63 int	vflag;				/* Verbose output. */
64 					/* System link call. */
65 int (*linkf) __P((const char *, const char *));
66 char	linkch;
67 
68 int	linkit __P((const char *, const char *, int));
69 int	main __P((int, char *[]));
70 void	usage __P((void));
71 
72 int
73 main(argc, argv)
74 	int argc;
75 	char *argv[];
76 {
77 	struct stat sb;
78 	char *p, *sourcedir;
79 	int ch, exitval;
80 
81 	/*
82 	 * Test for the special case where the utility is called as
83 	 * "link", for which the functionality provided is greatly
84 	 * simplified.
85 	 */
86 	if ((p = rindex(argv[0], '/')) == NULL)
87 		p = argv[0];
88 	else
89 		++p;
90 	if (strcmp(p, "link") == 0) {
91 		if (argc == 3) {
92 			linkf = link;
93 			exit(linkit(argv[1], argv[2], 0));
94 		} else
95 			usage();
96 	}
97 
98 	while ((ch = getopt(argc, argv, "fhinsv")) != -1)
99 		switch (ch) {
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 
129 	switch(argc) {
130 	case 0:
131 		usage();
132 		/* NOTREACHED */
133 	case 1:				/* ln target */
134 		exit(linkit(argv[0], ".", 1));
135 	case 2:				/* ln target source */
136 		exit(linkit(argv[0], argv[1], 0));
137 	}
138 					/* ln target1 target2 directory */
139 	sourcedir = argv[argc - 1];
140 	if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
141 		/*
142 		 * We were asked not to follow symlinks, but found one at
143 		 * the target--simulate "not a directory" error
144 		 */
145 		errno = ENOTDIR;
146 		err(1, "%s", sourcedir);
147 	}
148 	if (stat(sourcedir, &sb))
149 		err(1, "%s", sourcedir);
150 	if (!S_ISDIR(sb.st_mode))
151 		usage();
152 	for (exitval = 0; *argv != sourcedir; ++argv)
153 		exitval |= linkit(*argv, sourcedir, 1);
154 	exit(exitval);
155 }
156 
157 int
158 linkit(target, source, isdir)
159 	const char *target, *source;
160 	int isdir;
161 {
162 	struct stat sb;
163 	const char *p;
164 	int ch, exists, first;
165 	char path[PATH_MAX];
166 
167 	if (!sflag) {
168 		/* If target doesn't exist, quit now. */
169 		if (stat(target, &sb)) {
170 			warn("%s", target);
171 			return (1);
172 		}
173 		/* Only symbolic links to directories. */
174 		if (S_ISDIR(sb.st_mode)) {
175 			errno = EISDIR;
176 			warn("%s", target);
177 			return (1);
178 		}
179 	}
180 
181 	/*
182 	 * If the source is a directory (and not a symlink if hflag),
183 	 * append the target's name.
184 	 */
185 	if (isdir ||
186 	    (lstat(source, &sb) == 0 && S_ISDIR(sb.st_mode)) ||
187 	    (!hflag && stat(source, &sb) == 0 && S_ISDIR(sb.st_mode))) {
188 		if ((p = strrchr(target, '/')) == NULL)
189 			p = target;
190 		else
191 			++p;
192 		(void)snprintf(path, sizeof(path), "%s/%s", source, p);
193 		source = path;
194 	}
195 
196 	exists = !lstat(source, &sb);
197 	/*
198 	 * If the file exists, then unlink it forcibly if -f was specified
199 	 * and interactively if -i was specified.
200 	 */
201 	if (fflag && exists) {
202 		if (unlink(source)) {
203 			warn("%s", source);
204 			return (1);
205 		}
206 	} else if (iflag && exists) {
207 		fflush(stdout);
208 		fprintf(stderr, "replace %s? ", source);
209 
210 		first = ch = getchar();
211 		while(ch != '\n' && ch != EOF)
212 			ch = getchar();
213 		if (first != 'y' && first != 'Y') {
214 			fprintf(stderr, "not replaced\n");
215 			return (1);
216 		}
217 
218 		if (unlink(source)) {
219 			warn("%s", source);
220 			return (1);
221 		}
222 	}
223 
224 	/* Attempt the link. */
225 	if ((*linkf)(target, source)) {
226 		warn("%s", source);
227 		return (1);
228 	}
229 	if (vflag)
230 		(void)printf("%s %c> %s\n", source, linkch, target);
231 	return (0);
232 }
233 
234 void
235 usage()
236 {
237 	(void)fprintf(stderr, "%s\n%s\n%s\n",
238 	    "usage: ln [-fhinsv] file1 file2",
239 	    "       ln [-fhinsv] file ... directory",
240 	    "       link file1 file2");
241 	exit(1);
242 }
243