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
RevLine 
[367]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 $");
[370]42#endif /* no $id */
[367]43
[3215]44#define FAKES_NO_GETOPT_H /* bird */
[2129]45#include "config.h"
[370]46#ifndef _MSC_VER
[809]47# include <sys/param.h>
[375]48#endif
[367]49#include <sys/stat.h>
50
[370]51#include "err.h"
[367]52#include <errno.h>
53#include <limits.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include <unistd.h>
[3215]58#include "getopt_r.h"
[809]59#ifdef _MSC_VER
60# include "mscfakes.h"
61#endif
[1183]62#include "kmkbuiltin.h"
63
[3192]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
[1183]80static struct option long_options[] =
81{
82 { "help", no_argument, 0, 261 },
83 { "version", no_argument, 0, 262 },
84 { 0, 0, 0, 0 },
85};
[367]86
[1183]87
[3192]88static int linkit(PLNINSTANCE,const char *, const char *, int);
89static int usage(PKMKBUILTINCTX, int);
[367]90
[370]91
[367]92int
[3192]93kmk_builtin_ln(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx)
[367]94{
[3192]95 LNINSTANCE This;
[3215]96 struct getopt_state_r gos;
[367]97 struct stat sb;
[1183]98 char *sourcedir;
[367]99 int ch, exitval;
100
[1183]101 /* initialize globals. */
[3192]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;
[375]110
[3215]111 getopt_initialize_r(&gos, argc, argv, "fhinsv", long_options, envp, pCtx);
112 while ((ch = getopt_long_r(&gos, NULL)) != -1)
[367]113 switch (ch) {
114 case 'f':
[3192]115 This.fflag = 1;
116 This.iflag = 0;
[367]117 break;
118 case 'h':
119 case 'n':
[3192]120 This.hflag = 1;
[367]121 break;
122 case 'i':
[3192]123 This.iflag = 1;
124 This.fflag = 0;
[367]125 break;
126 case 's':
[3192]127 This.sflag = 1;
[367]128 break;
129 case 'v':
[3192]130 This.vflag = 1;
[367]131 break;
[1183]132 case 261:
[3192]133 usage(pCtx, 0);
[1183]134 return 0;
135 case 262:
136 return kbuild_version(argv[0]);
[367]137 case '?':
138 default:
[3192]139 return usage(pCtx, 1);
[367]140 }
141
[3215]142 argv += gos.optind;
143 argc -= gos.optind;
[367]144
[3192]145 This.linkf = This.sflag ? symlink : link;
146 This.linkch = This.sflag ? '-' : '=';
[367]147
148 switch(argc) {
149 case 0:
[3192]150 return usage(pCtx, 1);
[367]151 /* NOTREACHED */
152 case 1: /* ln target */
[3192]153 return linkit(&This, argv[0], ".", 1);
[367]154 case 2: /* ln target source */
[3192]155 return linkit(&This, argv[0], argv[1], 0);
[367]156 default:
157 ;
158 }
159 /* ln target1 target2 directory */
160 sourcedir = argv[argc - 1];
[3192]161 if (This.hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
[367]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;
[3192]167 return err(pCtx, 1, "st_mode: %s", sourcedir);
[367]168 }
169 if (stat(sourcedir, &sb))
[3192]170 return err(pCtx, 1, "stat: %s", sourcedir);
[367]171 if (!S_ISDIR(sb.st_mode))
[3192]172 return usage(pCtx, 1);
[367]173 for (exitval = 0; *argv != sourcedir; ++argv)
[3192]174 exitval |= linkit(&This, *argv, sourcedir, 1);
[370]175 return exitval;
[367]176}
177
[370]178static int
[3192]179linkit(PLNINSTANCE pThis, const char *target, const char *source, int isdir)
[367]180{
181 struct stat sb;
182 const char *p;
183 int ch, exists, first;
184 char path[PATH_MAX];
185
[3192]186 if (!pThis->sflag) {
[367]187 /* If target doesn't exist, quit now. */
188 if (stat(target, &sb)) {
[3192]189 warn(pThis->pCtx, "stat: %s", target);
[367]190 return (1);
191 }
192 /* Only symbolic links to directories. */
193 if (S_ISDIR(sb.st_mode)) {
194 errno = EISDIR;
[3192]195 warn(pThis->pCtx, "st_mode: %s", target);
[367]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)) ||
[3192]206 (!pThis->hflag && stat(source, &sb) == 0 && S_ISDIR(sb.st_mode))) {
[2128]207#if defined(_MSC_VER) || defined(__OS2__)
[1718]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
[367]214 if ((p = strrchr(target, '/')) == NULL)
[1718]215#endif
[367]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;
[3192]222 warn(pThis->pCtx, "snprintf: %s", target);
[367]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 */
[3192]233 if (pThis->fflag && exists) {
[367]234 if (unlink(source)) {
[3192]235 warn(pThis->pCtx, "unlink: %s", source);
[367]236 return (1);
237 }
[3192]238 } else if (pThis->iflag && exists) {
[367]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') {
[3192]246 kmk_builtin_ctx_printf(pThis->pCtx, 1, "not replaced\n");
[367]247 return (1);
248 }
249
250 if (unlink(source)) {
[3192]251 warn(pThis->pCtx, "unlink: %s", source);
[367]252 return (1);
253 }
254 }
255
256 /* Attempt the link. */
[3192]257 if ((*pThis->linkf)(target, source)) {
258 warn(pThis->pCtx, "%s: %s", pThis->linkf == link ? "link" : "symlink", source);
[367]259 return (1);
260 }
[3192]261 if (pThis->vflag)
262 kmk_builtin_ctx_printf(pThis->pCtx, 0, "%s %c> %s\n", source, pThis->linkch, target);
[367]263 return (0);
264}
265
[370]266static int
[3192]267usage(PKMKBUILTINCTX pCtx, int fIsErr)
[367]268{
[3192]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);
[370]277 return 1;
[367]278}
[3192]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