VirtualBox

source: kBuild/trunk/src/gmake/kmkbuiltin/ln.c@ 809

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

Solaris + cleanup.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 6.7 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#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#if defined(__FreeBSD__) || defined(__EMX__) || defined(__APPLE__)
95 optreset = 1;
96 optind = 1;
97#else
98 optind = 0; /* init */
99#endif
100
101#if 0 /* kmk: we don't need this. */
102 /*
103 * Test for the special case where the utility is called as
104 * "link", for which the functionality provided is greatly
105 * simplified.
106 */
107 if ((p = rindex(argv[0], '/')) == NULL)
108 p = argv[0];
109 else
110 ++p;
111 if (strcmp(p, "link") == 0) {
112 while (getopt(argc, argv, "") != -1)
113 return usage();
114 argc -= optind;
115 argv += optind;
116 if (argc != 2)
117 return usage();
118 linkf = link;
119 return linkit(argv[0], argv[1], 0);
120 }
121#else
122 (void)p;
123#endif
124
125
126 while ((ch = getopt(argc, argv, "fhinsv")) != -1)
127 switch (ch) {
128 case 'f':
129 fflag = 1;
130 iflag = 0;
131 break;
132 case 'h':
133 case 'n':
134 hflag = 1;
135 break;
136 case 'i':
137 iflag = 1;
138 fflag = 0;
139 break;
140 case 's':
141 sflag = 1;
142 break;
143 case 'v':
144 vflag = 1;
145 break;
146 case '?':
147 default:
148 return usage();
149 }
150
151 argv += optind;
152 argc -= optind;
153
154 linkf = sflag ? symlink : link;
155 linkch = sflag ? '-' : '=';
156
157 switch(argc) {
158 case 0:
159 return usage();
160 /* NOTREACHED */
161 case 1: /* ln target */
162 return linkit(argv[0], ".", 1);
163 case 2: /* ln target source */
164 return linkit(argv[0], argv[1], 0);
165 default:
166 ;
167 }
168 /* ln target1 target2 directory */
169 sourcedir = argv[argc - 1];
170 if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
171 /*
172 * We were asked not to follow symlinks, but found one at
173 * the target--simulate "not a directory" error
174 */
175 errno = ENOTDIR;
176 return err(1, "%s", sourcedir);
177 }
178 if (stat(sourcedir, &sb))
179 return err(1, "%s", sourcedir);
180 if (!S_ISDIR(sb.st_mode))
181 return usage();
182 for (exitval = 0; *argv != sourcedir; ++argv)
183 exitval |= linkit(*argv, sourcedir, 1);
184 return exitval;
185}
186
187static int
188linkit(const char *target, const char *source, int isdir)
189{
190 struct stat sb;
191 const char *p;
192 int ch, exists, first;
193 char path[PATH_MAX];
194
195 if (!sflag) {
196 /* If target doesn't exist, quit now. */
197 if (stat(target, &sb)) {
198 warn("%s", target);
199 return (1);
200 }
201 /* Only symbolic links to directories. */
202 if (S_ISDIR(sb.st_mode)) {
203 errno = EISDIR;
204 warn("%s", target);
205 return (1);
206 }
207 }
208
209 /*
210 * If the source is a directory (and not a symlink if hflag),
211 * append the target's name.
212 */
213 if (isdir ||
214 (lstat(source, &sb) == 0 && S_ISDIR(sb.st_mode)) ||
215 (!hflag && stat(source, &sb) == 0 && S_ISDIR(sb.st_mode))) {
216 if ((p = strrchr(target, '/')) == NULL)
217 p = target;
218 else
219 ++p;
220 if (snprintf(path, sizeof(path), "%s/%s", source, p) >=
221 (ssize_t)sizeof(path)) {
222 errno = ENAMETOOLONG;
223 warn("%s", target);
224 return (1);
225 }
226 source = path;
227 }
228
229 exists = !lstat(source, &sb);
230 /*
231 * If the file exists, then unlink it forcibly if -f was specified
232 * and interactively if -i was specified.
233 */
234 if (fflag && exists) {
235 if (unlink(source)) {
236 warn("%s", source);
237 return (1);
238 }
239 } else if (iflag && exists) {
240 fflush(stdout);
241 fprintf(stderr, "replace %s? ", source);
242
243 first = ch = getchar();
244 while(ch != '\n' && ch != EOF)
245 ch = getchar();
246 if (first != 'y' && first != 'Y') {
247 fprintf(stderr, "not replaced\n");
248 return (1);
249 }
250
251 if (unlink(source)) {
252 warn("%s", source);
253 return (1);
254 }
255 }
256
257 /* Attempt the link. */
258 if ((*linkf)(target, source)) {
259 warn("%s", source);
260 return (1);
261 }
262 if (vflag)
263 (void)printf("%s %c> %s\n", source, linkch, target);
264 return (0);
265}
266
267static int
268usage(void)
269{
270 (void)fprintf(stderr, "%s\n%s\n%s\n",
271 "usage: ln [-fhinsv] source_file [target_file]",
272 " ln [-fhinsv] source_file ... target_dir",
273 " link source_file target_file");
274 return 1;
275}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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