]>
git.saurik.com Git - apple/file_cmds.git/blob - ln/ln.c
1 /* $NetBSD: ln.c,v 1.15 1998/07/28 05:31:25 mycroft Exp $ */
4 * Copyright (c) 1987, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
38 __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n");
44 static char sccsid
[] = "@(#)ln.c 8.2 (Berkeley) 3/31/94";
46 __RCSID("$NetBSD: ln.c,v 1.15 1998/07/28 05:31:25 mycroft Exp $");
50 #include <sys/param.h>
60 int fflag
; /* Unlink existing files. */
61 int hflag
; /* Check new name for symlink first. */
62 int sflag
; /* Symbolic, not hard, link. */
63 /* System link call. */
64 int (*linkf
) __P((const char *, const char *));
66 int linkit
__P((char *, char *, int));
67 void usage
__P((void));
68 int main
__P((int, char *[]));
79 while ((ch
= getopt(argc
, argv
, "fhns")) != -1)
99 linkf
= sflag
? symlink
: link
;
105 case 1: /* ln target */
106 exit(linkit(argv
[0], ".", 1));
108 case 2: /* ln target source */
109 exit(linkit(argv
[0], argv
[1], 0));
112 /* ln target1 target2 directory */
113 sourcedir
= argv
[argc
- 1];
114 if (hflag
&& lstat(sourcedir
, &sb
) == 0 && S_ISLNK(sb
.st_mode
)) {
115 /* we were asked not to follow symlinks, but found one at
116 the target--simulate "not a directory" error */
118 err(1, "%s", sourcedir
);
120 if (stat(sourcedir
, &sb
))
121 err(1, "%s", sourcedir
);
122 if (!S_ISDIR(sb
.st_mode
))
124 for (exitval
= 0; *argv
!= sourcedir
; ++argv
)
125 exitval
|= linkit(*argv
, sourcedir
, 1);
131 linkit(target
, source
, isdir
)
132 char *target
, *source
;
136 char *p
, path
[MAXPATHLEN
];
139 /* If target doesn't exist, quit now. */
140 if (stat(target
, &sb
)) {
146 /* If the source is a directory (and not a symlink if hflag),
147 append the target's name. */
149 (!lstat(source
, &sb
) && S_ISDIR(sb
.st_mode
)) ||
150 (!hflag
&& !stat(source
, &sb
) && S_ISDIR(sb
.st_mode
))) {
151 if ((p
= strrchr(target
, '/')) == NULL
)
155 (void)snprintf(path
, sizeof(path
), "%s/%s", source
, p
);
160 * If the file exists, and -f was specified, unlink it.
163 if ((fflag
&& unlink(source
) < 0 && errno
!= ENOENT
) ||
164 (*linkf
)(target
, source
)) {
176 extern char *__progname
;
177 (void)fprintf(stderr
,
178 "Usage:\t%s [-fhns] file1 file2\n\t%s [-fhns] file ... directory\n",
179 __progname
, __progname
);