VirtualBox

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

Last change on this file since 1162 was 942, checked in by bird, 18 years ago

Always use the GNU make getopt stuff.

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