]>
git.saurik.com Git - apple/file_cmds.git/blob - ln/ln.c
f7bdc7ad08927a3b6f66803e6aeadd667b847c53
2 * Copyright (c) 1987, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 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.
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
32 static char const copyright
[] =
33 "@(#) Copyright (c) 1987, 1993, 1994\n\
34 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid
[] = "@(#)ln.c 8.2 (Berkeley) 3/31/94";
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD: src/bin/ln/ln.c,v 1.34 2006/02/14 11:08:05 glebius Exp $");
44 #include <sys/param.h>
55 int fflag
; /* Unlink existing files. */
56 int Fflag
; /* Remove empty directories also. */
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 /* System link call. */
62 int (*linkf
)(const char *, const char *);
65 int linkit(const char *, const char *, int);
69 main(int argc
, char *argv
[])
78 * Test for the special case where the utility is called as
79 * "link", for which the functionality provided is greatly
82 if ((p
= rindex(argv
[0], '/')) == NULL
)
86 if (strcmp(p
, "link") == 0) {
87 while (getopt(argc
, argv
, "") != -1)
94 exit(linkit(argv
[0], argv
[1], 0));
97 while ((ch
= getopt(argc
, argv
, "Ffhinsv")) != -1)
128 linkf
= sflag
? symlink
: link
;
129 linkch
= sflag
? '-' : '=';
132 if (Fflag
== 1 && iflag
== 0)
139 case 1: /* ln target */
140 exit(linkit(argv
[0], ".", 1));
141 case 2: /* ln target source */
142 exit(linkit(argv
[0], argv
[1], 0));
146 /* ln target1 target2 directory */
147 sourcedir
= argv
[argc
- 1];
148 if (hflag
&& lstat(sourcedir
, &sb
) == 0 && S_ISLNK(sb
.st_mode
)) {
150 * We were asked not to follow symlinks, but found one at
151 * the target--simulate "not a directory" error
154 err(1, "%s", sourcedir
);
156 if (stat(sourcedir
, &sb
))
157 err(1, "%s", sourcedir
);
158 if (!S_ISDIR(sb
.st_mode
))
160 for (exitval
= 0; *argv
!= sourcedir
; ++argv
)
161 exitval
|= linkit(*argv
, sourcedir
, 1);
166 linkit(const char *target
, const char *source
, int isdir
)
170 int ch
, exists
, first
;
174 /* If target doesn't exist, quit now. */
175 if (stat(target
, &sb
)) {
179 /* Only symbolic links to directories. */
180 if (S_ISDIR(sb
.st_mode
)) {
188 * If the source is a directory (and not a symlink if hflag),
189 * append the target's name.
192 (lstat(source
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) ||
193 (!hflag
&& stat(source
, &sb
) == 0 && S_ISDIR(sb
.st_mode
))) {
194 if ((p
= strrchr(target
, '/')) == NULL
)
198 if (snprintf(path
, sizeof(path
), "%s/%s", source
, p
) >=
199 (ssize_t
)sizeof(path
)) {
200 errno
= ENAMETOOLONG
;
207 exists
= !lstat(source
, &sb
);
209 * If the file exists, then unlink it forcibly if -f was specified
210 * and interactively if -i was specified.
212 if (fflag
&& exists
) {
213 if (Fflag
&& S_ISDIR(sb
.st_mode
)) {
218 } else if (unlink(source
)) {
222 } else if (iflag
&& exists
) {
224 fprintf(stderr
, "replace %s? ", source
);
226 first
= ch
= getchar();
227 while(ch
!= '\n' && ch
!= EOF
)
229 if (first
!= 'y' && first
!= 'Y') {
230 fprintf(stderr
, "not replaced\n");
234 if (Fflag
&& S_ISDIR(sb
.st_mode
)) {
239 } else if (unlink(source
)) {
245 /* Attempt the link. */
246 if ((*linkf
)(target
, source
)) {
251 (void)printf("%s %c> %s\n", source
, linkch
, target
);
258 (void)fprintf(stderr
, "%s\n%s\n%s\n",
259 "usage: ln [-Ffhinsv] source_file [target_file]",
260 " ln [-Ffhinsv] source_file ... target_dir",
261 " link source_file target_file");