Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / bin / ln / ln.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1987, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. 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.
20 *
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
31 * SUCH DAMAGE.
1de703da
MD
32 *
33 * @(#) Copyright (c) 1987, 1993, 1994 The Regents of the University of California. All rights reserved.
34 * @(#)ln.c 8.2 (Berkeley) 3/31/94
35 * $FreeBSD: src/bin/ln/ln.c,v 1.15.2.4 2002/07/12 07:34:38 tjr Exp $
36 * $DragonFly: src/bin/ln/ln.c,v 1.2 2003/06/17 04:22:50 dillon Exp $
984263bc
MD
37 */
38
984263bc
MD
39#include <sys/param.h>
40#include <sys/stat.h>
41
42#include <err.h>
43#include <errno.h>
44#include <limits.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <unistd.h>
49
50int fflag; /* Unlink existing files. */
51int hflag; /* Check new name for symlink first. */
52int iflag; /* Interactive mode. */
53int sflag; /* Symbolic, not hard, link. */
54int vflag; /* Verbose output. */
55 /* System link call. */
56int (*linkf)(const char *, const char *);
57char linkch;
58
59int linkit(const char *, const char *, int);
60void usage(void);
61
62int
63main(int argc, char *argv[])
64{
65 struct stat sb;
66 char *p, *sourcedir;
67 int ch, exitval;
68
69 /*
70 * Test for the special case where the utility is called as
71 * "link", for which the functionality provided is greatly
72 * simplified.
73 */
74 if ((p = rindex(argv[0], '/')) == NULL)
75 p = argv[0];
76 else
77 ++p;
78 if (strcmp(p, "link") == 0) {
79 while (getopt(argc, argv, "") != -1)
80 usage();
81 argc -= optind;
82 argv += optind;
83 if (argc != 2)
84 usage();
85 linkf = link;
86 exit(linkit(argv[0], argv[1], 0));
87 }
88
89 while ((ch = getopt(argc, argv, "fhinsv")) != -1)
90 switch (ch) {
91 case 'f':
92 fflag = 1;
93 iflag = 0;
94 break;
95 case 'h':
96 case 'n':
97 hflag = 1;
98 break;
99 case 'i':
100 iflag = 1;
101 fflag = 0;
102 break;
103 case 's':
104 sflag = 1;
105 break;
106 case 'v':
107 vflag = 1;
108 break;
109 case '?':
110 default:
111 usage();
112 }
113
114 argv += optind;
115 argc -= optind;
116
117 linkf = sflag ? symlink : link;
118 linkch = sflag ? '-' : '=';
119
120 switch(argc) {
121 case 0:
122 usage();
123 /* NOTREACHED */
124 case 1: /* ln target */
125 exit(linkit(argv[0], ".", 1));
126 case 2: /* ln target source */
127 exit(linkit(argv[0], argv[1], 0));
128 default:
129 ;
130 }
131 /* ln target1 target2 directory */
132 sourcedir = argv[argc - 1];
133 if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
134 /*
135 * We were asked not to follow symlinks, but found one at
136 * the target--simulate "not a directory" error
137 */
138 errno = ENOTDIR;
139 err(1, "%s", sourcedir);
140 }
141 if (stat(sourcedir, &sb))
142 err(1, "%s", sourcedir);
143 if (!S_ISDIR(sb.st_mode))
144 usage();
145 for (exitval = 0; *argv != sourcedir; ++argv)
146 exitval |= linkit(*argv, sourcedir, 1);
147 exit(exitval);
148}
149
150int
151linkit(const char *target, const char *source, int isdir)
152{
153 struct stat sb;
154 const char *p;
155 int ch, exists, first;
156 char path[PATH_MAX];
157
158 if (!sflag) {
159 /* If target doesn't exist, quit now. */
160 if (stat(target, &sb)) {
161 warn("%s", target);
162 return (1);
163 }
164 /* Only symbolic links to directories. */
165 if (S_ISDIR(sb.st_mode)) {
166 errno = EISDIR;
167 warn("%s", target);
168 return (1);
169 }
170 }
171
172 /*
173 * If the source is a directory (and not a symlink if hflag),
174 * append the target's name.
175 */
176 if (isdir ||
177 (lstat(source, &sb) == 0 && S_ISDIR(sb.st_mode)) ||
178 (!hflag && stat(source, &sb) == 0 && S_ISDIR(sb.st_mode))) {
179 if ((p = strrchr(target, '/')) == NULL)
180 p = target;
181 else
182 ++p;
183 if (snprintf(path, sizeof(path), "%s/%s", source, p) >=
184 sizeof(path)) {
185 errno = ENAMETOOLONG;
186 warn("%s", target);
187 return (1);
188 }
189 source = path;
190 }
191
192 exists = !lstat(source, &sb);
193 /*
194 * If the file exists, then unlink it forcibly if -f was specified
195 * and interactively if -i was specified.
196 */
197 if (fflag && exists) {
198 if (unlink(source)) {
199 warn("%s", source);
200 return (1);
201 }
202 } else if (iflag && exists) {
203 fflush(stdout);
204 fprintf(stderr, "replace %s? ", source);
205
206 first = ch = getchar();
207 while(ch != '\n' && ch != EOF)
208 ch = getchar();
209 if (first != 'y' && first != 'Y') {
210 fprintf(stderr, "not replaced\n");
211 return (1);
212 }
213
214 if (unlink(source)) {
215 warn("%s", source);
216 return (1);
217 }
218 }
219
220 /* Attempt the link. */
221 if ((*linkf)(target, source)) {
222 warn("%s", source);
223 return (1);
224 }
225 if (vflag)
226 (void)printf("%s %c> %s\n", source, linkch, target);
227 return (0);
228}
229
230void
231usage(void)
232{
233 (void)fprintf(stderr, "%s\n%s\n%s\n",
234 "usage: ln [-fhinsv] file1 file2",
235 " ln [-fhinsv] file ... directory",
236 " link file1 file2");
237 exit(1);
238}