ln.c revision 1.32 1 /* $NetBSD: ln.c,v 1.32 2006/10/13 20:37:10 wiz Exp $ */
2
3 /*
4 * Copyright (c) 1987, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\n\
35 The Regents of the University of California. All rights reserved.\n");
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)ln.c 8.2 (Berkeley) 3/31/94";
41 #else
42 __RCSID("$NetBSD: ln.c,v 1.32 2006/10/13 20:37:10 wiz Exp $");
43 #endif
44 #endif /* not lint */
45
46 #include <sys/param.h>
47 #include <sys/stat.h>
48
49 #include <err.h>
50 #include <errno.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55
56 int fflag; /* Unlink existing files. */
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
62 /* System link call. */
63 int (*linkf)(const char *, const char *);
64 char linkch;
65
66 int linkit(const char *, const char *, int);
67 void usage(void);
68 int main(int, char *[]);
69
70 int
71 main(int argc, char *argv[])
72 {
73 struct stat sb;
74 int ch, exitval;
75 char *sourcedir;
76
77 setprogname(argv[0]);
78 while ((ch = getopt(argc, argv, "fhinsv")) != -1)
79 switch (ch) {
80 case 'f':
81 fflag = 1;
82 iflag = 0;
83 break;
84 case 'h':
85 case 'n':
86 hflag = 1;
87 break;
88 case 'i':
89 iflag = 1;
90 fflag = 0;
91 break;
92 case 's':
93 sflag = 1;
94 break;
95 case 'v':
96 vflag = 1;
97 break;
98 case '?':
99 default:
100 usage();
101 /* NOTREACHED */
102 }
103
104 argv += optind;
105 argc -= optind;
106
107 if (sflag) {
108 linkf = symlink;
109 linkch = '-';
110 } else {
111 linkf = link;
112 linkch = '=';
113 }
114
115 switch(argc) {
116 case 0:
117 usage();
118 /* NOTREACHED */
119 case 1: /* ln target */
120 exit(linkit(argv[0], ".", 1));
121 /* NOTREACHED */
122 case 2: /* ln target source */
123 exit(linkit(argv[0], argv[1], 0));
124 /* NOTREACHED */
125 }
126
127 /* ln target1 target2 directory */
128 sourcedir = argv[argc - 1];
129 if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
130 /* we were asked not to follow symlinks, but found one at
131 the target--simulate "not a directory" error */
132 errno = ENOTDIR;
133 err(EXIT_FAILURE, "%s", sourcedir);
134 /* NOTREACHED */
135 }
136 if (stat(sourcedir, &sb)) {
137 err(EXIT_FAILURE, "%s", sourcedir);
138 /* NOTREACHED */
139 }
140 if (!S_ISDIR(sb.st_mode)) {
141 usage();
142 /* NOTREACHED */
143 }
144 for (exitval = 0; *argv != sourcedir; ++argv)
145 exitval |= linkit(*argv, sourcedir, 1);
146 exit(exitval);
147 /* NOTREACHED */
148 }
149
150 int
151 linkit(const char *target, const char *source, int isdir)
152 {
153 struct stat sb;
154 const char *p;
155 char path[MAXPATHLEN];
156 int ch, exists, first;
157
158 if (!sflag) {
159 /* If target doesn't exist, quit now. */
160 if (stat(target, &sb)) {
161 warn("%s", target);
162 return (1);
163 }
164 }
165
166 /* If the source is a directory (and not a symlink if hflag),
167 append the target's name. */
168 if (isdir ||
169 (!lstat(source, &sb) && S_ISDIR(sb.st_mode)) ||
170 (!hflag && !stat(source, &sb) && S_ISDIR(sb.st_mode))) {
171 if ((p = strrchr(target, '/')) == NULL)
172 p = target;
173 else
174 ++p;
175 (void)snprintf(path, sizeof(path), "%s/%s", source, p);
176 source = path;
177 }
178
179 exists = !lstat(source, &sb);
180
181 /*
182 * If the file exists, then unlink it forcibly if -f was specified
183 * and interactively if -i was specified.
184 */
185 if (fflag && exists) {
186 if (unlink(source)) {
187 warn("%s", source);
188 return (1);
189 }
190 } else if (iflag && exists) {
191 fflush(stdout);
192 (void)fprintf(stderr, "replace %s? ", source);
193
194 first = ch = getchar();
195 while (ch != '\n' && ch != EOF)
196 ch = getchar();
197 if (first != 'y' && first != 'Y') {
198 (void)fprintf(stderr, "not replaced\n");
199 return (1);
200 }
201
202 if (unlink(source)) {
203 warn("%s", source);
204 return (1);
205 }
206 }
207
208 /* Attempt the link. */
209 if ((*linkf)(target, source)) {
210 warn("%s", source);
211 return (1);
212 }
213 if (vflag)
214 (void)printf("%s %c> %s\n", source, linkch, target);
215
216 return (0);
217 }
218
219 void
220 usage(void)
221 {
222
223 (void)fprintf(stderr,
224 "usage:\t%s [-fhinsv] file1 file2\n\t%s [-fhinsv] file ... directory\n",
225 getprogname(), getprogname());
226 exit(1);
227 /* NOTREACHED */
228 }
229