VirtualBox

source: kBuild/trunk/src/gmake/kmkbuiltin/ln.c@ 775

Last change on this file since 775 was 557, checked in by bird, 18 years ago

Initial Mac OS X / Darwin bootstrapping.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 KB
Line 
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#if 0
31#ifndef lint
32static char const copyright[] =
33"@(#) Copyright (c) 1987, 1993, 1994\n\
34 The Regents of the University of California. All rights reserved.\n";
35#endif /* not lint */
36
37#ifndef lint
38static char sccsid[] = "@(#)ln.c 8.2 (Berkeley) 3/31/94";
39#endif /* not lint */
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD: src/bin/ln/ln.c,v 1.33 2005/02/09 17:37:37 ru Exp $");
42#endif /* no $id */
43
44#ifndef _MSC_VER
45#include <sys/param.h>
46#endif
47#include <sys/stat.h>
48
49#include "err.h"
50#include <errno.h>
51#include <limits.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#ifndef _MSC_VER
56#include <unistd.h>
57#else
58#include "mscfakes.h"
59#endif
60
61static int fflag; /* Unlink existing files. */
62static int hflag; /* Check new name for symlink first. */
63static int iflag; /* Interactive mode. */
64static int sflag; /* Symbolic, not hard, link. */
65static int vflag; /* Verbose output. */
66 /* System link call. */
67static int (*linkf)(const char *, const char *);
68static char linkch;
69
70static int linkit(const char *, const char *, int);
71static int usage(void);
72
73
74int
75kmk_builtin_ln(int argc, char *argv[])
76{
77 struct stat sb;
78 char *p, *sourcedir;
79 int ch, exitval;
80
81 /* initialize globals. */
82 fflag = hflag = iflag = sflag = vflag = 0;
83 linkch = 0;
84 linkf = NULL;
85
86 /* kmk: reset getopt() and set program name. */
87 g_progname = argv[0];
88 opterr = 1;
89 optarg = NULL;
90 optopt = 0;
91#if defined(__FreeBSD__) || defined(__EMX__) || defined(__APPLE__)
92 optreset = 1;
93 optind = 1;
94#else
95 optind = 0; /* init */
96#endif
97
98#if 0 /* kmk: we don't need this. */
99 /*
100 * Test for the special case where the utility is called as
101 * "link", for which the functionality provided is greatly
102 * simplified.
103 */
104 if ((p = rindex(argv[0], '/')) == NULL)
105 p = argv[0];
106 else
107 ++p;
108 if (strcmp(p, "link") == 0) {
109 while (getopt(argc, argv, "") != -1)
110 return usage();
111 argc -= optind;
112 argv += optind;
113 if (argc != 2)
114 return usage();
115 linkf = link;
116 return linkit(argv[0], argv[1], 0);
117 }
118#else
119 (void)p;
120#endif
121
122
123 while ((ch = getopt(argc, argv, "fhinsv")) != -1)
124 switch (ch) {
125 case 'f':
126 fflag = 1;
127 iflag = 0;
128 break;
129 case 'h':
130 case 'n':
131 hflag = 1;
132 break;
133 case 'i':
134 iflag = 1;
135 fflag = 0;
136 break;
137 case 's':
138 sflag = 1;
139 break;
140 case 'v':
141 vflag = 1;
142 break;
143 case '?':
144 default:
145 return usage();
146 }
147
148 argv += optind;
149 argc -= optind;
150
151 linkf = sflag ? symlink : link;
152 linkch = sflag ? '-' : '=';
153
154 switch(argc) {
155 case 0:
156 return usage();
157 /* NOTREACHED */
158 case 1: /* ln target */
159 return linkit(argv[0], ".", 1);
160 case 2: /* ln target source */
161 return linkit(argv[0], argv[1], 0);
162 default:
163 ;
164 }
165 /* ln target1 target2 directory */
166 sourcedir = argv[argc - 1];
167 if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
168 /*
169 * We were asked not to follow symlinks, but found one at
170 * the target--simulate "not a directory" error
171 */
172 errno = ENOTDIR;
173 return err(1, "%s", sourcedir);
174 }
175 if (stat(sourcedir, &sb))
176 return err(1, "%s", sourcedir);
177 if (!S_ISDIR(sb.st_mode))
178 return usage();
179 for (exitval = 0; *argv != sourcedir; ++argv)
180 exitval |= linkit(*argv, sourcedir, 1);
181 return exitval;
182}
183
184static int
185linkit(const char *target, const char *source, int isdir)
186{
187 struct stat sb;
188 const char *p;
189 int ch, exists, first;
190 char path[PATH_MAX];
191
192 if (!sflag) {
193 /* If target doesn't exist, quit now. */
194 if (stat(target, &sb)) {
195 warn("%s", target);
196 return (1);
197 }
198 /* Only symbolic links to directories. */
199 if (S_ISDIR(sb.st_mode)) {
200 errno = EISDIR;
201 warn("%s", target);
202 return (1);
203 }
204 }
205
206 /*
207 * If the source is a directory (and not a symlink if hflag),
208 * append the target's name.
209 */
210 if (isdir ||
211 (lstat(source, &sb) == 0 && S_ISDIR(sb.st_mode)) ||
212 (!hflag && stat(source, &sb) == 0 && S_ISDIR(sb.st_mode))) {
213 if ((p = strrchr(target, '/')) == NULL)
214 p = target;
215 else
216 ++p;
217 if (snprintf(path, sizeof(path), "%s/%s", source, p) >=
218 (ssize_t)sizeof(path)) {
219 errno = ENAMETOOLONG;
220 warn("%s", target);
221 return (1);
222 }
223 source = path;
224 }
225
226 exists = !lstat(source, &sb);
227 /*
228 * If the file exists, then unlink it forcibly if -f was specified
229 * and interactively if -i was specified.
230 */
231 if (fflag && exists) {
232 if (unlink(source)) {
233 warn("%s", source);
234 return (1);
235 }
236 } else if (iflag && exists) {
237 fflush(stdout);
238 fprintf(stderr, "replace %s? ", source);
239
240 first = ch = getchar();
241 while(ch != '\n' && ch != EOF)
242 ch = getchar();
243 if (first != 'y' && first != 'Y') {
244 fprintf(stderr, "not replaced\n");
245 return (1);
246 }
247
248 if (unlink(source)) {
249 warn("%s", source);
250 return (1);
251 }
252 }
253
254 /* Attempt the link. */
255 if ((*linkf)(target, source)) {
256 warn("%s", source);
257 return (1);
258 }
259 if (vflag)
260 (void)printf("%s %c> %s\n", source, linkch, target);
261 return (0);
262}
263
264static int
265usage(void)
266{
267 (void)fprintf(stderr, "%s\n%s\n%s\n",
268 "usage: ln [-fhinsv] source_file [target_file]",
269 " ln [-fhinsv] source_file ... target_dir",
270 " link source_file target_file");
271 return 1;
272}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette