VirtualBox

source: kBuild/branches/kBuild-0.1.5/src/kmk/kmkbuiltin/ln.c@ 2321

Last change on this file since 2321 was 2129, checked in by bird, 16 years ago

ln.s: include config.h first.

  • 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#include "config.h"
45#ifndef _MSC_VER
46# include <sys/param.h>
47#endif
48#include <sys/stat.h>
49
50#include "err.h"
51#include <errno.h>
52#include <limits.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
56#include <unistd.h>
57#include "getopt.h"
58#ifdef _MSC_VER
59# include "mscfakes.h"
60#endif
61#include "kmkbuiltin.h"
62
63static int fflag; /* Unlink existing files. */
64static int hflag; /* Check new name for symlink first. */
65static int iflag; /* Interactive mode. */
66static int sflag; /* Symbolic, not hard, link. */
67static int vflag; /* Verbose output. */
68 /* System link call. */
69static int (*linkf)(const char *, const char *);
70static char linkch;
71static struct option long_options[] =
72{
73 { "help", no_argument, 0, 261 },
74 { "version", no_argument, 0, 262 },
75 { 0, 0, 0, 0 },
76};
77
78
79static int linkit(const char *, const char *, int);
80static int usage(FILE *);
81
82
83int
84kmk_builtin_ln(int argc, char *argv[], char **envp)
85{
86 struct stat sb;
87 char *sourcedir;
88 int ch, exitval;
89
90 /* initialize globals. */
91 fflag = hflag = iflag = sflag = vflag = 0;
92 linkch = 0;
93 linkf = NULL;
94
95 /* kmk: reset getopt() and set program name. */
96 g_progname = argv[0];
97 opterr = 1;
98 optarg = NULL;
99 optopt = 0;
100 optind = 0; /* init */
101
102 while ((ch = getopt_long(argc, argv, "fhinsv", long_options, NULL)) != -1)
103 switch (ch) {
104 case 'f':
105 fflag = 1;
106 iflag = 0;
107 break;
108 case 'h':
109 case 'n':
110 hflag = 1;
111 break;
112 case 'i':
113 iflag = 1;
114 fflag = 0;
115 break;
116 case 's':
117 sflag = 1;
118 break;
119 case 'v':
120 vflag = 1;
121 break;
122 case 261:
123 usage(stdout);
124 return 0;
125 case 262:
126 return kbuild_version(argv[0]);
127 case '?':
128 default:
129 return usage(stderr);
130 }
131
132 argv += optind;
133 argc -= optind;
134
135 linkf = sflag ? symlink : link;
136 linkch = sflag ? '-' : '=';
137
138 switch(argc) {
139 case 0:
140 return usage(stderr);
141 /* NOTREACHED */
142 case 1: /* ln target */
143 return linkit(argv[0], ".", 1);
144 case 2: /* ln target source */
145 return linkit(argv[0], argv[1], 0);
146 default:
147 ;
148 }
149 /* ln target1 target2 directory */
150 sourcedir = argv[argc - 1];
151 if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
152 /*
153 * We were asked not to follow symlinks, but found one at
154 * the target--simulate "not a directory" error
155 */
156 errno = ENOTDIR;
157 return err(1, "%s", sourcedir);
158 }
159 if (stat(sourcedir, &sb))
160 return err(1, "%s", sourcedir);
161 if (!S_ISDIR(sb.st_mode))
162 return usage(stderr);
163 for (exitval = 0; *argv != sourcedir; ++argv)
164 exitval |= linkit(*argv, sourcedir, 1);
165 return exitval;
166}
167
168static int
169linkit(const char *target, const char *source, int isdir)
170{
171 struct stat sb;
172 const char *p;
173 int ch, exists, first;
174 char path[PATH_MAX];
175
176 if (!sflag) {
177 /* If target doesn't exist, quit now. */
178 if (stat(target, &sb)) {
179 warn("%s", target);
180 return (1);
181 }
182 /* Only symbolic links to directories. */
183 if (S_ISDIR(sb.st_mode)) {
184 errno = EISDIR;
185 warn("%s", target);
186 return (1);
187 }
188 }
189
190 /*
191 * If the source is a directory (and not a symlink if hflag),
192 * append the target's name.
193 */
194 if (isdir ||
195 (lstat(source, &sb) == 0 && S_ISDIR(sb.st_mode)) ||
196 (!hflag && stat(source, &sb) == 0 && S_ISDIR(sb.st_mode))) {
197#if defined(_MSC_VER) || defined(__OS2__)
198 char *p2 = strrchr(target, '\\');
199 p = strrchr(target, '/');
200 if (p2 != NULL && (p == NULL || p2 > p))
201 p = p2;
202 if (p == NULL)
203#else
204 if ((p = strrchr(target, '/')) == NULL)
205#endif
206 p = target;
207 else
208 ++p;
209 if (snprintf(path, sizeof(path), "%s/%s", source, p) >=
210 (ssize_t)sizeof(path)) {
211 errno = ENAMETOOLONG;
212 warn("%s", target);
213 return (1);
214 }
215 source = path;
216 }
217
218 exists = !lstat(source, &sb);
219 /*
220 * If the file exists, then unlink it forcibly if -f was specified
221 * and interactively if -i was specified.
222 */
223 if (fflag && exists) {
224 if (unlink(source)) {
225 warn("%s", source);
226 return (1);
227 }
228 } else if (iflag && exists) {
229 fflush(stdout);
230 fprintf(stderr, "replace %s? ", source);
231
232 first = ch = getchar();
233 while(ch != '\n' && ch != EOF)
234 ch = getchar();
235 if (first != 'y' && first != 'Y') {
236 fprintf(stderr, "not replaced\n");
237 return (1);
238 }
239
240 if (unlink(source)) {
241 warn("%s", source);
242 return (1);
243 }
244 }
245
246 /* Attempt the link. */
247 if ((*linkf)(target, source)) {
248 warn("%s", source);
249 return (1);
250 }
251 if (vflag)
252 (void)printf("%s %c> %s\n", source, linkch, target);
253 return (0);
254}
255
256static int
257usage(FILE *pf)
258{
259 fprintf(pf, "usage: %s [-fhinsv] source_file [target_file]\n"
260 " or: %s [-fhinsv] source_file ... target_dir\n"
261 " or: %s source_file target_file\n"
262 " or: %s --help\n"
263 " or: %s --version\n",
264 g_progname, g_progname, g_progname, g_progname, g_progname);
265 return 1;
266}
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