VirtualBox

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

Last change on this file since 1223 was 1183, checked in by bird, 17 years ago

Added --version and --help to all builtins.

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