1 /* $NetBSD: ln.c,v 1.35 2011/08/29 14:38:30 joerg 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.35 2011/08/29 14:38:30 joerg Exp $");
46 #include <sys/param.h>
57 static int fflag
; /* Unlink existing files. */
58 static int hflag
; /* Check new name for symlink first. */
59 static int iflag
; /* Interactive mode. */
60 static int sflag
; /* Symbolic, not hard, link. */
61 static int vflag
; /* Verbose output */
63 /* System link call. */
64 static int (*linkf
)(const char *, const char *);
67 static int linkit(const char *, const char *, int);
68 __dead
static void usage(void);
71 main(int argc
, char *argv
[])
78 (void)setlocale(LC_ALL
, "");
80 while ((ch
= getopt(argc
, argv
, "fhinsv")) != -1)
121 case 1: /* ln target */
122 exit(linkit(argv
[0], ".", 1));
124 case 2: /* ln target source */
125 exit(linkit(argv
[0], argv
[1], 0));
129 /* ln target1 target2 directory */
130 sourcedir
= argv
[argc
- 1];
131 if (hflag
&& lstat(sourcedir
, &sb
) == 0 && S_ISLNK(sb
.st_mode
)) {
132 /* we were asked not to follow symlinks, but found one at
133 the target--simulate "not a directory" error */
135 err(EXIT_FAILURE
, "%s", sourcedir
);
138 if (stat(sourcedir
, &sb
)) {
139 err(EXIT_FAILURE
, "%s", sourcedir
);
142 if (!S_ISDIR(sb
.st_mode
)) {
146 for (exitval
= 0; *argv
!= sourcedir
; ++argv
)
147 exitval
|= linkit(*argv
, sourcedir
, 1);
153 linkit(const char *target
, const char *source
, int isdir
)
157 char path
[MAXPATHLEN
];
158 int ch
, exists
, first
;
161 /* If target doesn't exist, quit now. */
162 if (stat(target
, &sb
)) {
168 /* If the source is a directory (and not a symlink if hflag),
169 append the target's name. */
171 (!lstat(source
, &sb
) && S_ISDIR(sb
.st_mode
)) ||
172 (!hflag
&& !stat(source
, &sb
) && S_ISDIR(sb
.st_mode
))) {
173 if ((p
= strrchr(target
, '/')) == NULL
)
177 (void)snprintf(path
, sizeof(path
), "%s/%s", source
, p
);
181 exists
= !lstat(source
, &sb
);
184 * If the file exists, then unlink it forcibly if -f was specified
185 * and interactively if -i was specified.
187 if (fflag
&& exists
) {
188 if (unlink(source
)) {
192 } else if (iflag
&& exists
) {
194 (void)fprintf(stderr
, "replace %s? ", source
);
196 first
= ch
= getchar();
197 while (ch
!= '\n' && ch
!= EOF
)
199 if (first
!= 'y' && first
!= 'Y') {
200 (void)fprintf(stderr
, "not replaced\n");
204 if (unlink(source
)) {
210 /* Attempt the link. */
211 if ((*linkf
)(target
, source
)) {
216 (void)printf("%s %c> %s\n", source
, linkch
, target
);
225 (void)fprintf(stderr
,
226 "usage:\t%s [-fhinsv] file1 file2\n\t%s [-fhinsv] file ... directory\n",
227 getprogname(), getprogname());