ln.c revision 1.21 1 /* $NetBSD: ln.c,v 1.21 2002/12/27 03:50:05 jrf 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.21 2002/12/27 03:50:05 jrf 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 int vflag; /* Verbose output */
64 /* System link call. */
65 int (*linkf)(const char *, const char *);
66 char linkch;
67
68 int linkit(char *, char *, int);
69 void usage(void);
70 int main(int, char *[]);
71
72 int
73 main(int argc, char *argv[])
74 {
75 struct stat sb;
76 int ch, exitval;
77 char *sourcedir;
78
79 setprogname(argv[0]);
80 while ((ch = getopt(argc, argv, "fhnsv")) != -1)
81 switch (ch) {
82 case 'f':
83 fflag = 1;
84 break;
85 case 'h':
86 case 'n':
87 hflag = 1;
88 break;
89 case 's':
90 sflag = 1;
91 break;
92 case 'v':
93 vflag = 1;
94 break;
95 case '?':
96 default:
97 usage();
98 }
99
100 argv += optind;
101 argc -= optind;
102
103 if (sflag) {
104 linkf = symlink;
105 linkch = '-';
106 } else {
107 linkf = link;
108 linkch = '=';
109 }
110
111 switch(argc) {
112 case 0:
113 usage();
114 /* NOTREACHED */
115 case 1: /* ln target */
116 exit(linkit(argv[0], ".", 1));
117 /* NOTREACHED */
118 case 2: /* ln target source */
119 exit(linkit(argv[0], argv[1], 0));
120 /* NOTREACHED */
121 }
122 /* ln target1 target2 directory */
123 sourcedir = argv[argc - 1];
124 if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
125 /* we were asked not to follow symlinks, but found one at
126 the target--simulate "not a directory" error */
127 errno = ENOTDIR;
128 err(1, "%s", sourcedir);
129 }
130 if (stat(sourcedir, &sb))
131 err(1, "%s", sourcedir);
132 if (!S_ISDIR(sb.st_mode))
133 usage();
134 for (exitval = 0; *argv != sourcedir; ++argv)
135 exitval |= linkit(*argv, sourcedir, 1);
136 exit(exitval);
137 /* NOTREACHED */
138 }
139
140 int
141 linkit(char *target, char *source, int isdir)
142 {
143 struct stat sb;
144 char *p, path[MAXPATHLEN];
145
146 if (!sflag) {
147 /* If target doesn't exist, quit now. */
148 if (stat(target, &sb)) {
149 warn("%s", target);
150 return (1);
151 }
152 }
153
154 /* If the source is a directory (and not a symlink if hflag),
155 append the target's name. */
156 if (isdir ||
157 (!lstat(source, &sb) && S_ISDIR(sb.st_mode)) ||
158 (!hflag && !stat(source, &sb) && S_ISDIR(sb.st_mode))) {
159 if ((p = strrchr(target, '/')) == NULL)
160 p = target;
161 else
162 ++p;
163 (void)snprintf(path, sizeof(path), "%s/%s", source, p);
164 source = path;
165 }
166
167 /*
168 * If the file exists, and -f was specified, unlink it.
169 * Attempt the link.
170 */
171 if ((fflag && unlink(source) < 0 && errno != ENOENT) ||
172 (*linkf)(target, source)) {
173 warn("%s", source);
174 return (1);
175 }
176 if (vflag)
177 (void)printf("%s %c> %s\n", source, linkch, target);
178
179 return (0);
180 }
181
182 void
183 usage(void)
184 {
185
186 (void)fprintf(stderr,
187 "Usage:\t%s [-fhns] file1 file2\n\t%s [-fhnsv] file ... directory\n",
188 getprogname(), getprogname());
189 exit(1);
190 /* NOTREACHED */
191 }
192