VirtualBox

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

Last change on this file since 1422 was 1328, checked in by bird, 17 years ago

include our getopt.h

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