VirtualBox

source: kBuild/trunk/src/kmk/kmkbuiltin/ln.c@ 3192

最後變更 在這個檔案從3192是 3192,由 bird 提交於 7 年 前

kmkbuiltin: funnel output thru output.c (usually via err.c).

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 7.8 KB
 
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
63/*********************************************************************************************************************************
64* Structures and Typedefs *
65*********************************************************************************************************************************/
66typedef struct LNINSTANCE
67{
68 PKMKBUILTINCTX pCtx;
69 int fflag; /* Unlink existing files. */
70 int hflag; /* Check new name for symlink first. */
71 int iflag; /* Interactive mode. */
72 int sflag; /* Symbolic, not hard, link. */
73 int vflag; /* Verbose output. */
74 int (*linkf)(const char *, const char *); /* System link call. */
75 char linkch;
76} LNINSTANCE;
77typedef LNINSTANCE *PLNINSTANCE;
78
79static struct option long_options[] =
80{
81 { "help", no_argument, 0, 261 },
82 { "version", no_argument, 0, 262 },
83 { 0, 0, 0, 0 },
84};
85
86
87static int linkit(PLNINSTANCE,const char *, const char *, int);
88static int usage(PKMKBUILTINCTX, int);
89
90
91int
92kmk_builtin_ln(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
93{
94 LNINSTANCE This;
95 struct stat sb;
96 char *sourcedir;
97 int ch, exitval;
98
99 /* initialize globals. */
100 This.pCtx = pCtx;
101 This.fflag = 0;
102 This.hflag = 0;
103 This.iflag = 0;
104 This.sflag = 0;
105 This.vflag = 0;
106 This.linkch = 0;
107 This.linkf = NULL;
108
109 /* kmk: reset getopt() and set program name. */
110 opterr = 1;
111 optarg = NULL;
112 optopt = 0;
113 optind = 0; /* init */
114
115 while ((ch = getopt_long(argc, argv, "fhinsv", long_options, NULL)) != -1)
116 switch (ch) {
117 case 'f':
118 This.fflag = 1;
119 This.iflag = 0;
120 break;
121 case 'h':
122 case 'n':
123 This.hflag = 1;
124 break;
125 case 'i':
126 This.iflag = 1;
127 This.fflag = 0;
128 break;
129 case 's':
130 This.sflag = 1;
131 break;
132 case 'v':
133 This.vflag = 1;
134 break;
135 case 261:
136 usage(pCtx, 0);
137 return 0;
138 case 262:
139 return kbuild_version(argv[0]);
140 case '?':
141 default:
142 return usage(pCtx, 1);
143 }
144
145 argv += optind;
146 argc -= optind;
147
148 This.linkf = This.sflag ? symlink : link;
149 This.linkch = This.sflag ? '-' : '=';
150
151 switch(argc) {
152 case 0:
153 return usage(pCtx, 1);
154 /* NOTREACHED */
155 case 1: /* ln target */
156 return linkit(&This, argv[0], ".", 1);
157 case 2: /* ln target source */
158 return linkit(&This, argv[0], argv[1], 0);
159 default:
160 ;
161 }
162 /* ln target1 target2 directory */
163 sourcedir = argv[argc - 1];
164 if (This.hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
165 /*
166 * We were asked not to follow symlinks, but found one at
167 * the target--simulate "not a directory" error
168 */
169 errno = ENOTDIR;
170 return err(pCtx, 1, "st_mode: %s", sourcedir);
171 }
172 if (stat(sourcedir, &sb))
173 return err(pCtx, 1, "stat: %s", sourcedir);
174 if (!S_ISDIR(sb.st_mode))
175 return usage(pCtx, 1);
176 for (exitval = 0; *argv != sourcedir; ++argv)
177 exitval |= linkit(&This, *argv, sourcedir, 1);
178 return exitval;
179}
180
181static int
182linkit(PLNINSTANCE pThis, const char *target, const char *source, int isdir)
183{
184 struct stat sb;
185 const char *p;
186 int ch, exists, first;
187 char path[PATH_MAX];
188
189 if (!pThis->sflag) {
190 /* If target doesn't exist, quit now. */
191 if (stat(target, &sb)) {
192 warn(pThis->pCtx, "stat: %s", target);
193 return (1);
194 }
195 /* Only symbolic links to directories. */
196 if (S_ISDIR(sb.st_mode)) {
197 errno = EISDIR;
198 warn(pThis->pCtx, "st_mode: %s", target);
199 return (1);
200 }
201 }
202
203 /*
204 * If the source is a directory (and not a symlink if hflag),
205 * append the target's name.
206 */
207 if (isdir ||
208 (lstat(source, &sb) == 0 && S_ISDIR(sb.st_mode)) ||
209 (!pThis->hflag && stat(source, &sb) == 0 && S_ISDIR(sb.st_mode))) {
210#if defined(_MSC_VER) || defined(__OS2__)
211 char *p2 = strrchr(target, '\\');
212 p = strrchr(target, '/');
213 if (p2 != NULL && (p == NULL || p2 > p))
214 p = p2;
215 if (p == NULL)
216#else
217 if ((p = strrchr(target, '/')) == NULL)
218#endif
219 p = target;
220 else
221 ++p;
222 if (snprintf(path, sizeof(path), "%s/%s", source, p) >=
223 (ssize_t)sizeof(path)) {
224 errno = ENAMETOOLONG;
225 warn(pThis->pCtx, "snprintf: %s", target);
226 return (1);
227 }
228 source = path;
229 }
230
231 exists = !lstat(source, &sb);
232 /*
233 * If the file exists, then unlink it forcibly if -f was specified
234 * and interactively if -i was specified.
235 */
236 if (pThis->fflag && exists) {
237 if (unlink(source)) {
238 warn(pThis->pCtx, "unlink: %s", source);
239 return (1);
240 }
241 } else if (pThis->iflag && exists) {
242 fflush(stdout);
243 fprintf(stderr, "replace %s? ", source);
244
245 first = ch = getchar();
246 while(ch != '\n' && ch != EOF)
247 ch = getchar();
248 if (first != 'y' && first != 'Y') {
249 kmk_builtin_ctx_printf(pThis->pCtx, 1, "not replaced\n");
250 return (1);
251 }
252
253 if (unlink(source)) {
254 warn(pThis->pCtx, "unlink: %s", source);
255 return (1);
256 }
257 }
258
259 /* Attempt the link. */
260 if ((*pThis->linkf)(target, source)) {
261 warn(pThis->pCtx, "%s: %s", pThis->linkf == link ? "link" : "symlink", source);
262 return (1);
263 }
264 if (pThis->vflag)
265 kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s %c> %s\n", source, pThis->linkch, target);
266 return (0);
267}
268
269static int
270usage(PKMKBUILTINCTX pCtx, int fIsErr)
271{
272 kmk_builtin_ctx_printf(pCtx,fIsErr,
273 "usage: %s [-fhinsv] source_file [target_file]\n"
274 " or: %s [-fhinsv] source_file ... target_dir\n"
275 " or: %s source_file target_file\n"
276 " or: %s --help\n"
277 " or: %s --version\n",
278 pCtx->pszProgName, pCtx->pszProgName, pCtx->pszProgName,
279 pCtx->pszProgName, pCtx->pszProgName);
280 return 1;
281}
282
283#ifdef KMK_BUILTIN_STANDALONE
284int main(int argc, char **argv, char **envp)
285{
286 KMKBUILTINCTX Ctx = { "kmk_ln", NULL };
287 return kmk_builtin_ln(argc, argv, envp, &Ctx);
288}
289#endif
290
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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