3
/* NetBSD: ln.c,v 1.34 2008/07/20 00:52:40 lukem Exp */
6
* Copyright (c) 1987, 1993, 1994
7
* The Regents of the University of California. All rights reserved.
9
* Redistribution and use in source and binary forms, with or without
10
* modification, are permitted provided that the following conditions
12
* 1. Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
14
* 2. Redistributions in binary form must reproduce the above copyright
15
* notice, this list of conditions and the following disclaimer in the
16
* documentation and/or other materials provided with the distribution.
17
* 3. 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
34
#include <sys/cdefs.h>
36
__COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\
37
The Regents of the University of California. All rights reserved.");
42
static char sccsid[] = "@(#)ln.c 8.2 (Berkeley) 3/31/94";
44
__RCSID("$NetBSD: ln.c,v 1.34 2008/07/20 00:52:40 lukem Exp $");
48
#include <sys/param.h>
60
#include <rump/ukfs.h>
62
#include <fsu_mount.h>
66
#define stat(a, b) ukfs_stat(ukfs, a, b)
67
#define lstat(a, b) ukfs_lstat(ukfs, a, b)
68
#define unlink(a) ukfs_remove(ukfs, a)
71
link(const char *name1, const char *name2)
74
return ukfs_link(ukfs, name1, name2);
78
symlink(const char *name1, const char *name2)
81
return ukfs_symlink(ukfs, name1, name2);
85
int fflag; /* Unlink existing files. */
86
int hflag; /* Check new name for symlink first. */
87
int iflag; /* Interactive mode. */
88
int sflag; /* Symbolic, not hard, link. */
89
int vflag; /* Verbose output */
91
/* System link call. */
92
int (*linkf)(const char *, const char *);
95
int linkit(const char *, const char *, int);
97
int main(int, char *[]);
100
main(int argc, char *argv[])
106
setprogname(argv[0]);
107
(void)setlocale(LC_ALL, "");
110
FSU_MOUNT(argc, argv, ukfs);
111
#endif /* USE_UKFS */
113
while ((ch = getopt(argc, argv, "fhinsv")) != -1)
154
case 1: /* ln target */
155
exit(linkit(argv[0], ".", 1));
157
case 2: /* ln target source */
158
exit(linkit(argv[0], argv[1], 0));
162
/* ln target1 target2 directory */
163
sourcedir = argv[argc - 1];
164
if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
165
/* we were asked not to follow symlinks, but found one at
166
the target--simulate "not a directory" error */
168
err(EXIT_FAILURE, "%s", sourcedir);
171
if (stat(sourcedir, &sb)) {
172
err(EXIT_FAILURE, "%s", sourcedir);
175
if (!S_ISDIR(sb.st_mode)) {
179
for (exitval = 0; *argv != sourcedir; ++argv)
180
exitval |= linkit(*argv, sourcedir, 1);
186
linkit(const char *target, const char *source, int isdir)
190
char path[MAXPATHLEN];
191
int ch, exists, first;
194
/* If target doesn't exist, quit now. */
195
if (stat(target, &sb)) {
201
/* If the source is a directory (and not a symlink if hflag),
202
append the target's name. */
204
(!lstat(source, &sb) && S_ISDIR(sb.st_mode)) ||
205
(!hflag && !stat(source, &sb) && S_ISDIR(sb.st_mode))) {
206
if ((p = strrchr(target, '/')) == NULL)
210
(void)snprintf(path, sizeof(path), "%s/%s", source, p);
214
exists = !lstat(source, &sb);
217
* If the file exists, then unlink it forcibly if -f was specified
218
* and interactively if -i was specified.
220
if (fflag && exists) {
221
if (unlink(source)) {
225
} else if (iflag && exists) {
227
(void)fprintf(stderr, "replace %s? ", source);
229
first = ch = getchar();
230
while (ch != '\n' && ch != EOF)
232
if (first != 'y' && first != 'Y') {
233
(void)fprintf(stderr, "not replaced\n");
237
if (unlink(source)) {
243
/* Attempt the link. */
244
if ((*linkf)(target, source)) {
249
(void)printf("%s %c> %s\n", source, linkch, target);
258
(void)fprintf(stderr,
259
"usage:\t%s [-fhinsv] file1 file2\n\t%s [-fhinsv] file ... directory\n",
260
getprogname(), getprogname());
263
"usage:\t%s %s [-fhinsv] file1 file2\n"
264
"usage:\t%s %s [-fhinsv] file ... directory\n",
265
getprogname(), fsu_mount_usage(),
266
getprogname(), fsu_mount_usage());