]>
git.saurik.com Git - apple/file_cmds.git/blob - ln/ln.c
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 * 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.
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
35 static char const copyright
[] =
36 "@(#) Copyright (c) 1987, 1993, 1994\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid
[] = "@(#)ln.c 8.2 (Berkeley) 3/31/94";
45 #include <sys/cdefs.h>
46 __RCSID("$FreeBSD: src/bin/ln/ln.c,v 1.29 2002/07/31 16:53:59 markm Exp $");
48 #include <sys/param.h>
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 *);
68 int linkit(const char *, const char *, int);
72 main(int argc
, char *argv
[])
79 * Test for the special case where the utility is called as
80 * "link", for which the functionality provided is greatly
83 if ((p
= rindex(argv
[0], '/')) == NULL
)
87 if (strcmp(p
, "link") == 0) {
88 while (getopt(argc
, argv
, "") != -1)
95 exit(linkit(argv
[0], argv
[1], 0));
98 while ((ch
= getopt(argc
, argv
, "fhinsv")) != -1)
126 linkf
= sflag
? symlink
: link
;
127 linkch
= sflag
? '-' : '=';
133 case 1: /* ln target */
134 exit(linkit(argv
[0], ".", 1));
135 case 2: /* ln target source */
136 exit(linkit(argv
[0], argv
[1], 0));
140 /* ln target1 target2 directory */
141 sourcedir
= argv
[argc
- 1];
142 if (hflag
&& lstat(sourcedir
, &sb
) == 0 && S_ISLNK(sb
.st_mode
)) {
144 * We were asked not to follow symlinks, but found one at
145 * the target--simulate "not a directory" error
148 err(1, "%s", sourcedir
);
150 if (stat(sourcedir
, &sb
))
151 err(1, "%s", sourcedir
);
152 if (!S_ISDIR(sb
.st_mode
))
154 for (exitval
= 0; *argv
!= sourcedir
; ++argv
)
155 exitval
|= linkit(*argv
, sourcedir
, 1);
160 linkit(const char *target
, const char *source
, int isdir
)
164 int ch
, exists
, first
;
168 /* If target doesn't exist, quit now. */
169 if (stat(target
, &sb
)) {
173 /* Only symbolic links to directories. */
174 if (S_ISDIR(sb
.st_mode
)) {
182 * If the source is a directory (and not a symlink if hflag),
183 * append the target's name.
186 (lstat(source
, &sb
) == 0 && S_ISDIR(sb
.st_mode
)) ||
187 (!hflag
&& stat(source
, &sb
) == 0 && S_ISDIR(sb
.st_mode
))) {
188 if ((p
= strrchr(target
, '/')) == NULL
)
192 if (snprintf(path
, sizeof(path
), "%s/%s", source
, p
) >=
193 (ssize_t
)sizeof(path
)) {
194 errno
= ENAMETOOLONG
;
201 exists
= !lstat(source
, &sb
);
203 * If the file exists, then unlink it forcibly if -f was specified
204 * and interactively if -i was specified.
206 if (fflag
&& exists
) {
207 if (unlink(source
)) {
211 } else if (iflag
&& exists
) {
213 fprintf(stderr
, "replace %s? ", source
);
215 first
= ch
= getchar();
216 while(ch
!= '\n' && ch
!= EOF
)
218 if (first
!= 'y' && first
!= 'Y') {
219 fprintf(stderr
, "not replaced\n");
223 if (unlink(source
)) {
229 /* Attempt the link. */
230 if ((*linkf
)(target
, source
)) {
235 (void)printf("%s %c> %s\n", source
, linkch
, target
);
242 (void)fprintf(stderr
, "%s\n%s\n%s\n",
243 "usage: ln [-fhinsv] file1 file2",
244 " ln [-fhinsv] file ... directory",
245 " link file1 file2");