1 /* $NetBSD: ln.c,v 1.33 2008/05/26 14:21:08 christos 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. 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.
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
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)ln.c 8.2 (Berkeley) 3/31/94";
42 __RCSID("$NetBSD: ln.c,v 1.33 2008/05/26 14:21:08 christos Exp $");
46 #include <sys/param.h>
57 int fflag
; /* Unlink existing files. */
58 int hflag
; /* Check new name for symlink first. */
59 int iflag
; /* Interactive mode. */
60 int sflag
; /* Symbolic, not hard, link. */
61 int vflag
; /* Verbose output */
63 /* System link call. */
64 int (*linkf
)(const char *, const char *);
67 int linkit(const char *, const char *, int);
69 int main(int, char *[]);
72 main(int argc
, char *argv
[])
79 (void)setlocale(LC_ALL
, "");
81 while ((ch
= getopt(argc
, argv
, "fhinsv")) != -1)
122 case 1: /* ln target */
123 exit(linkit(argv
[0], ".", 1));
125 case 2: /* ln target source */
126 exit(linkit(argv
[0], argv
[1], 0));
130 /* ln target1 target2 directory */
131 sourcedir
= argv
[argc
- 1];
132 if (hflag
&& lstat(sourcedir
, &sb
) == 0 && S_ISLNK(sb
.st_mode
)) {
133 /* we were asked not to follow symlinks, but found one at
134 the target--simulate "not a directory" error */
136 err(EXIT_FAILURE
, "%s", sourcedir
);
139 if (stat(sourcedir
, &sb
)) {
140 err(EXIT_FAILURE
, "%s", sourcedir
);
143 if (!S_ISDIR(sb
.st_mode
)) {
147 for (exitval
= 0; *argv
!= sourcedir
; ++argv
)
148 exitval
|= linkit(*argv
, sourcedir
, 1);
154 linkit(const char *target
, const char *source
, int isdir
)
158 char path
[MAXPATHLEN
];
159 int ch
, exists
, first
;
162 /* If target doesn't exist, quit now. */
163 if (stat(target
, &sb
)) {
169 /* If the source is a directory (and not a symlink if hflag),
170 append the target's name. */
172 (!lstat(source
, &sb
) && S_ISDIR(sb
.st_mode
)) ||
173 (!hflag
&& !stat(source
, &sb
) && S_ISDIR(sb
.st_mode
))) {
174 if ((p
= strrchr(target
, '/')) == NULL
)
178 (void)snprintf(path
, sizeof(path
), "%s/%s", source
, p
);
182 exists
= !lstat(source
, &sb
);
185 * If the file exists, then unlink it forcibly if -f was specified
186 * and interactively if -i was specified.
188 if (fflag
&& exists
) {
189 if (unlink(source
)) {
193 } else if (iflag
&& exists
) {
195 (void)fprintf(stderr
, "replace %s? ", source
);
197 first
= ch
= getchar();
198 while (ch
!= '\n' && ch
!= EOF
)
200 if (first
!= 'y' && first
!= 'Y') {
201 (void)fprintf(stderr
, "not replaced\n");
205 if (unlink(source
)) {
211 /* Attempt the link. */
212 if ((*linkf
)(target
, source
)) {
217 (void)printf("%s %c> %s\n", source
, linkch
, target
);
226 (void)fprintf(stderr
,
227 "usage:\t%s [-fhinsv] file1 file2\n\t%s [-fhinsv] file ... directory\n",
228 getprogname(), getprogname());