ln.c revision 1.17 1 /* $NetBSD: ln.c,v 1.17 2001/02/04 19:32:26 christos Exp $ */
2
3 /*
4 * Copyright (c) 1987, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
22 *
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
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)ln.c 8.2 (Berkeley) 3/31/94";
45 #else
46 __RCSID("$NetBSD: ln.c,v 1.17 2001/02/04 19:32:26 christos Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/stat.h>
52
53 #include <err.h>
54 #include <errno.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59
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 *));
65
66 extern char *__progname;
67
68 int linkit __P((char *, char *, int));
69 void usage __P((void));
70 int main __P((int, char *[]));
71
72 int
73 main(argc, argv)
74 int argc;
75 char *argv[];
76 {
77 struct stat sb;
78 int ch, exitval;
79 char *sourcedir;
80
81 while ((ch = getopt(argc, argv, "fhns")) != -1)
82 switch (ch) {
83 case 'f':
84 fflag = 1;
85 break;
86 case 'h':
87 case 'n':
88 hflag = 1;
89 break;
90 case 's':
91 sflag = 1;
92 break;
93 case '?':
94 default:
95 usage();
96 }
97
98 argv += optind;
99 argc -= optind;
100
101 linkf = sflag ? symlink : link;
102
103 switch(argc) {
104 case 0:
105 usage();
106 /* NOTREACHED */
107 case 1: /* ln target */
108 exit(linkit(argv[0], ".", 1));
109 /* NOTREACHED */
110 case 2: /* ln target source */
111 exit(linkit(argv[0], argv[1], 0));
112 /* NOTREACHED */
113 }
114 /* ln target1 target2 directory */
115 sourcedir = argv[argc - 1];
116 if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
117 /* we were asked not to follow symlinks, but found one at
118 the target--simulate "not a directory" error */
119 errno = ENOTDIR;
120 err(1, "%s", sourcedir);
121 }
122 if (stat(sourcedir, &sb))
123 err(1, "%s", sourcedir);
124 if (!S_ISDIR(sb.st_mode))
125 usage();
126 for (exitval = 0; *argv != sourcedir; ++argv)
127 exitval |= linkit(*argv, sourcedir, 1);
128 exit(exitval);
129 /* NOTREACHED */
130 }
131
132 int
133 linkit(target, source, isdir)
134 char *target, *source;
135 int isdir;
136 {
137 struct stat sb;
138 char *p, path[MAXPATHLEN];
139
140 if (!sflag) {
141 /* If target doesn't exist, quit now. */
142 if (lstat(target, &sb)) {
143 warn("%s", target);
144 return (1);
145 }
146 }
147
148 /* If the source is a directory (and not a symlink if hflag),
149 append the target's name. */
150 if (isdir ||
151 (!lstat(source, &sb) && S_ISDIR(sb.st_mode)) ||
152 (!hflag && !stat(source, &sb) && S_ISDIR(sb.st_mode))) {
153 if ((p = strrchr(target, '/')) == NULL)
154 p = target;
155 else
156 ++p;
157 (void)snprintf(path, sizeof(path), "%s/%s", source, p);
158 source = path;
159 }
160
161 /*
162 * If the file exists, and -f was specified, unlink it.
163 * Attempt the link.
164 */
165 if ((fflag && unlink(source) < 0 && errno != ENOENT) ||
166 (*linkf)(target, source)) {
167 warn("%s", source);
168 return (1);
169 }
170
171 return (0);
172 }
173
174 void
175 usage()
176 {
177
178 (void)fprintf(stderr,
179 "Usage:\t%s [-fhns] file1 file2\n\t%s [-fhns] file ... directory\n",
180 __progname, __progname);
181 exit(1);
182 /* NOTREACHED */
183 }
184