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