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