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