this repo has no description
at fixPythonPipStalling 265 lines 6.6 kB view raw
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#endif 41#include <sys/cdefs.h> 42//__FBSDID("$FreeBSD: src/bin/ln/ln.c,v 1.34 2006/02/14 11:08:05 glebius Exp $"); 43 44#include <bsd/string.h> 45 46#include <sys/param.h> 47#include <sys/stat.h> 48 49#include <err.h> 50#include <errno.h> 51#include <libgen.h> 52#include <limits.h> 53#include <stdio.h> 54#include <stdlib.h> 55#include <string.h> 56#include <unistd.h> 57 58int fflag; /* Unlink existing files. */ 59int Fflag; /* Remove empty directories also. */ 60int hflag; /* Check new name for symlink first. */ 61int iflag; /* Interactive mode. */ 62int sflag; /* Symbolic, not hard, link. */ 63int vflag; /* Verbose output. */ 64 /* System link call. */ 65int (*linkf)(const char *, const char *); 66char linkch; 67 68int linkit(const char *, const char *, int); 69void usage(void); 70 71int 72main(int argc, char *argv[]) 73{ 74 struct stat sb; 75 char *p, *sourcedir; 76 int ch, exitval; 77 78 if (argc < 1) 79 usage(); 80 /* 81 * Test for the special case where the utility is called as 82 * "link", for which the functionality provided is greatly 83 * simplified. 84 */ 85 if ((p = rindex(argv[0], '/')) == NULL) 86 p = argv[0]; 87 else 88 ++p; 89 if (strcmp(p, "link") == 0) { 90 while (getopt(argc, argv, "") != -1) 91 usage(); 92 argc -= optind; 93 argv += optind; 94 if (argc != 2) 95 usage(); 96 linkf = link; 97 exit(linkit(argv[0], argv[1], 0)); 98 } 99 100 while ((ch = getopt(argc, argv, "Ffhinsv")) != -1) 101 switch (ch) { 102 case 'F': 103 Fflag = 1; 104 break; 105 case 'f': 106 fflag = 1; 107 iflag = 0; 108 break; 109 case 'h': 110 case 'n': 111 hflag = 1; 112 break; 113 case 'i': 114 iflag = 1; 115 fflag = 0; 116 break; 117 case 's': 118 sflag = 1; 119 break; 120 case 'v': 121 vflag = 1; 122 break; 123 case '?': 124 default: 125 usage(); 126 } 127 128 argv += optind; 129 argc -= optind; 130 131 linkf = sflag ? symlink : link; 132 linkch = sflag ? '-' : '='; 133 if (sflag == 0) 134 Fflag = 0; 135 if (Fflag == 1 && iflag == 0) 136 fflag = 1; 137 138 switch(argc) { 139 case 0: 140 usage(); 141 /* NOTREACHED */ 142 case 1: /* ln target */ 143 exit(linkit(argv[0], ".", 1)); 144 case 2: /* ln target source */ 145 exit(linkit(argv[0], argv[1], 0)); 146 default: 147 ; 148 } 149 /* ln target1 target2 directory */ 150 sourcedir = argv[argc - 1]; 151 if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) { 152 /* 153 * We were asked not to follow symlinks, but found one at 154 * the target--simulate "not a directory" error 155 */ 156 errno = ENOTDIR; 157 err(1, "%s", sourcedir); 158 } 159 if (stat(sourcedir, &sb)) 160 err(1, "%s", sourcedir); 161 if (!S_ISDIR(sb.st_mode)) 162 usage(); 163 for (exitval = 0; *argv != sourcedir; ++argv) 164 exitval |= linkit(*argv, sourcedir, 1); 165 exit(exitval); 166} 167 168int 169linkit(const char *target, const char *source, int isdir) 170{ 171 struct stat sb; 172 const char *p; 173 int ch, exists, first; 174 char path[PATH_MAX]; 175 char bbuf[PATH_MAX]; 176 177 if (!sflag) { 178 /* If target doesn't exist, quit now. */ 179 if (stat(target, &sb)) { 180 warn("%s", target); 181 return (1); 182 } 183 /* Only symbolic links to directories. */ 184 if (S_ISDIR(sb.st_mode)) { 185 errno = EISDIR; 186 warn("%s", target); 187 return (1); 188 } 189 } 190 191 /* 192 * If the source is a directory (and not a symlink if hflag), 193 * append the target's name. 194 */ 195 if (isdir || 196 (lstat(source, &sb) == 0 && S_ISDIR(sb.st_mode)) || 197 (!hflag && stat(source, &sb) == 0 && S_ISDIR(sb.st_mode))) { 198 if (strlcpy(bbuf, target, sizeof(bbuf)) >= sizeof(bbuf) || 199 (p = basename(bbuf)) == NULL || 200 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 (Fflag && S_ISDIR(sb.st_mode)) { 216 if (rmdir(source)) { 217 warn("%s", source); 218 return (1); 219 } 220 } else if (unlink(source)) { 221 warn("%s", source); 222 return (1); 223 } 224 } else if (iflag && exists) { 225 fflush(stdout); 226 fprintf(stderr, "replace %s? ", source); 227 228 first = ch = getchar(); 229 while(ch != '\n' && ch != EOF) 230 ch = getchar(); 231 if (first != 'y' && first != 'Y') { 232 fprintf(stderr, "not replaced\n"); 233 return (1); 234 } 235 236 if (Fflag && S_ISDIR(sb.st_mode)) { 237 if (rmdir(source)) { 238 warn("%s", source); 239 return (1); 240 } 241 } else if (unlink(source)) { 242 warn("%s", source); 243 return (1); 244 } 245 } 246 247 /* Attempt the link. */ 248 if ((*linkf)(target, source)) { 249 warn("%s", source); 250 return (1); 251 } 252 if (vflag) 253 (void)printf("%s %c> %s\n", source, linkch, target); 254 return (0); 255} 256 257void 258usage(void) 259{ 260 (void)fprintf(stderr, "%s\n%s\n%s\n", 261 "usage: ln [-Ffhinsv] source_file [target_file]", 262 " ln [-Ffhinsv] source_file ... target_dir", 263 " link source_file target_file"); 264 exit(1); 265}