VirtualBox

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

Last change on this file since 3387 was 3215, checked in by bird, 6 years ago

kmk_ln, kmk_mkdir, kmk_mv, kmk_printf: changed to use getopt_r and got rid of remaining static buffers.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use