source: trunk/src/gmake/kmkbuiltin/ln.c@ 370

Last change on this file since 370 was 370, checked in by bird, 20 years ago

o Ported all kmk builtins to win32.
o Fixed serveral bugs in kmk builtins.
o Probably broke both linux, bsd and OS/2. :-)

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