]> git.saurik.com Git - apple/file_cmds.git/blame - ln/ln.c
file_cmds-321.100.10.0.1.tar.gz
[apple/file_cmds.git] / ln / ln.c
CommitLineData
864a4b6e 1/*-
44a7a5ab
A
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.
44a7a5ab
A
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
864a4b6e 30#if 0
44a7a5ab 31#ifndef lint
6c780a1f
A
32static char const copyright[] =
33"@(#) Copyright (c) 1987, 1993, 1994\n\
34 The Regents of the University of California. All rights reserved.\n";
44a7a5ab
A
35#endif /* not lint */
36
37#ifndef lint
44a7a5ab 38static char sccsid[] = "@(#)ln.c 8.2 (Berkeley) 3/31/94";
44a7a5ab 39#endif /* not lint */
864a4b6e 40#endif
6c780a1f 41#include <sys/cdefs.h>
864a4b6e 42__FBSDID("$FreeBSD: src/bin/ln/ln.c,v 1.34 2006/02/14 11:08:05 glebius Exp $");
44a7a5ab
A
43
44#include <sys/param.h>
45#include <sys/stat.h>
46
47#include <err.h>
48#include <errno.h>
00337e45 49#include <libgen.h>
6c780a1f 50#include <limits.h>
44a7a5ab
A
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54#include <unistd.h>
55
56int fflag; /* Unlink existing files. */
864a4b6e 57int Fflag; /* Remove empty directories also. */
44a7a5ab 58int hflag; /* Check new name for symlink first. */
6c780a1f 59int iflag; /* Interactive mode. */
44a7a5ab 60int sflag; /* Symbolic, not hard, link. */
6c780a1f 61int vflag; /* Verbose output. */
44a7a5ab 62 /* System link call. */
6c780a1f
A
63int (*linkf)(const char *, const char *);
64char linkch;
44a7a5ab 65
6c780a1f
A
66int linkit(const char *, const char *, int);
67void usage(void);
44a7a5ab
A
68
69int
6c780a1f 70main(int argc, char *argv[])
44a7a5ab
A
71{
72 struct stat sb;
6c780a1f 73 char *p, *sourcedir;
44a7a5ab 74 int ch, exitval;
44a7a5ab 75
864a4b6e
A
76 if (argc < 1)
77 usage();
6c780a1f
A
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 while (getopt(argc, argv, "") != -1)
89 usage();
90 argc -= optind;
91 argv += optind;
92 if (argc != 2)
93 usage();
94 linkf = link;
74e6a095
A
95 /* UNIX conformance requires that both operands be NOT a dir */
96 for (int i = 0; i < 2; i++) {
97 if (stat(argv[i], &sb) == 0 && S_ISDIR(sb.st_mode)){
98 errno = EISDIR;
99 warn("%s", argv[0]);
100 return 1;
101 }
102 }
103
6c780a1f
A
104 exit(linkit(argv[0], argv[1], 0));
105 }
106
864a4b6e 107 while ((ch = getopt(argc, argv, "Ffhinsv")) != -1)
44a7a5ab 108 switch (ch) {
864a4b6e
A
109 case 'F':
110 Fflag = 1;
111 break;
44a7a5ab
A
112 case 'f':
113 fflag = 1;
6c780a1f 114 iflag = 0;
44a7a5ab
A
115 break;
116 case 'h':
117 case 'n':
118 hflag = 1;
119 break;
6c780a1f
A
120 case 'i':
121 iflag = 1;
122 fflag = 0;
123 break;
44a7a5ab
A
124 case 's':
125 sflag = 1;
126 break;
6c780a1f
A
127 case 'v':
128 vflag = 1;
129 break;
44a7a5ab
A
130 case '?':
131 default:
132 usage();
133 }
134
135 argv += optind;
136 argc -= optind;
137
138 linkf = sflag ? symlink : link;
6c780a1f 139 linkch = sflag ? '-' : '=';
864a4b6e
A
140 if (sflag == 0)
141 Fflag = 0;
142 if (Fflag == 1 && iflag == 0)
143 fflag = 1;
44a7a5ab
A
144
145 switch(argc) {
146 case 0:
147 usage();
148 /* NOTREACHED */
149 case 1: /* ln target */
150 exit(linkit(argv[0], ".", 1));
44a7a5ab
A
151 case 2: /* ln target source */
152 exit(linkit(argv[0], argv[1], 0));
6c780a1f
A
153 default:
154 ;
44a7a5ab
A
155 }
156 /* ln target1 target2 directory */
157 sourcedir = argv[argc - 1];
158 if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
6c780a1f
A
159 /*
160 * We were asked not to follow symlinks, but found one at
161 * the target--simulate "not a directory" error
162 */
44a7a5ab
A
163 errno = ENOTDIR;
164 err(1, "%s", sourcedir);
165 }
166 if (stat(sourcedir, &sb))
167 err(1, "%s", sourcedir);
168 if (!S_ISDIR(sb.st_mode))
169 usage();
170 for (exitval = 0; *argv != sourcedir; ++argv)
171 exitval |= linkit(*argv, sourcedir, 1);
172 exit(exitval);
44a7a5ab
A
173}
174
175int
6c780a1f 176linkit(const char *target, const char *source, int isdir)
44a7a5ab
A
177{
178 struct stat sb;
6c780a1f
A
179 const char *p;
180 int ch, exists, first;
181 char path[PATH_MAX];
00337e45 182 char bbuf[PATH_MAX];
44a7a5ab
A
183
184 if (!sflag) {
185 /* If target doesn't exist, quit now. */
186 if (stat(target, &sb)) {
187 warn("%s", target);
188 return (1);
189 }
6c780a1f
A
190 /* Only symbolic links to directories. */
191 if (S_ISDIR(sb.st_mode)) {
192 errno = EISDIR;
193 warn("%s", target);
194 return (1);
195 }
44a7a5ab
A
196 }
197
6c780a1f
A
198 /*
199 * If the source is a directory (and not a symlink if hflag),
200 * append the target's name.
201 */
44a7a5ab 202 if (isdir ||
6c780a1f
A
203 (lstat(source, &sb) == 0 && S_ISDIR(sb.st_mode)) ||
204 (!hflag && stat(source, &sb) == 0 && S_ISDIR(sb.st_mode))) {
00337e45
A
205 if (strlcpy(bbuf, target, sizeof(bbuf)) >= sizeof(bbuf) ||
206 (p = basename(bbuf)) == NULL ||
207 snprintf(path, sizeof(path), "%s/%s", source, p) >=
6c780a1f
A
208 (ssize_t)sizeof(path)) {
209 errno = ENAMETOOLONG;
210 warn("%s", target);
211 return (1);
212 }
44a7a5ab
A
213 source = path;
214 }
215
6c780a1f 216 exists = !lstat(source, &sb);
44a7a5ab 217 /*
6c780a1f
A
218 * If the file exists, then unlink it forcibly if -f was specified
219 * and interactively if -i was specified.
44a7a5ab 220 */
6c780a1f 221 if (fflag && exists) {
864a4b6e
A
222 if (Fflag && S_ISDIR(sb.st_mode)) {
223 if (rmdir(source)) {
224 warn("%s", source);
225 return (1);
226 }
227 } else if (unlink(source)) {
6c780a1f
A
228 warn("%s", source);
229 return (1);
230 }
231 } else if (iflag && exists) {
232 fflush(stdout);
233 fprintf(stderr, "replace %s? ", source);
234
235 first = ch = getchar();
236 while(ch != '\n' && ch != EOF)
237 ch = getchar();
238 if (first != 'y' && first != 'Y') {
239 fprintf(stderr, "not replaced\n");
240 return (1);
241 }
242
864a4b6e
A
243 if (Fflag && S_ISDIR(sb.st_mode)) {
244 if (rmdir(source)) {
245 warn("%s", source);
246 return (1);
247 }
248 } else if (unlink(source)) {
6c780a1f
A
249 warn("%s", source);
250 return (1);
251 }
252 }
253
254 /* Attempt the link. */
255 if ((*linkf)(target, source)) {
44a7a5ab
A
256 warn("%s", source);
257 return (1);
258 }
6c780a1f
A
259 if (vflag)
260 (void)printf("%s %c> %s\n", source, linkch, target);
44a7a5ab
A
261 return (0);
262}
263
264void
6c780a1f 265usage(void)
44a7a5ab 266{
6c780a1f 267 (void)fprintf(stderr, "%s\n%s\n%s\n",
f13ef9e9
A
268 "usage: ln [-Ffhinsv] source_file [link_name]",
269 " ln [-Ffhinsv] source_file ... linkname_dir",
270 " link source_file link_name");
44a7a5ab 271 exit(1);
44a7a5ab 272}