Changeset 370 in kBuild for trunk/src/gmake/kmkbuiltin/cp.c
- Timestamp:
- Dec 18, 2005 3:48:02 AM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/gmake/kmkbuiltin/cp.c
r299 r370 41 41 static char sccsid[] = "@(#)cp.c 8.2 (Berkeley) 4/1/94"; 42 42 #endif /* not lint */ 43 #endif44 43 #include <sys/cdefs.h> 45 //__FBSDID("$FreeBSD: src/bin/cp/cp.c,v 1.50 2004/04/06 20:06:44 markm Exp $"); 44 __FBSDID("$FreeBSD: src/bin/cp/cp.c,v 1.50 2004/04/06 20:06:44 markm Exp $"); 45 #endif 46 46 47 47 /* … … 63 63 #include <sys/stat.h> 64 64 65 #include <err.h>65 #include "err.h" 66 66 #include <errno.h> 67 #ifdef DO_CP_TREE 67 68 #include <fts.h> 69 #endif 68 70 #include <limits.h> 69 71 #include <signal.h> … … 71 73 #include <stdlib.h> 72 74 #include <string.h> 75 #ifndef _MSC_VER 73 76 #include <unistd.h> 77 #else 78 #include "mscfakes.h" 79 #include "ftsfake.h" 80 #endif 74 81 75 82 #include "cp_extern.h" … … 103 110 104 111 int fflag, iflag, nflag, pflag, vflag; 105 int Rflag, rflag;112 static int Rflag, rflag; 106 113 volatile sig_atomic_t info; 107 114 … … 122 129 char *target; 123 130 124 argv0 = argv[0]; 131 /* init globals */ 132 cp_argv0 = argv[0]; 125 133 to.p_end = to.p_path; 126 134 to.target_end = emptystring; … … 128 136 fflag = iflag = nflag = pflag = vflag = Rflag = rflag = 0; 129 137 info = 0; 138 /* reset getopt and set progname. */ 139 g_progname = argv[0]; 130 140 opterr = 1; 131 141 optarg = NULL; … … 154 164 break; 155 165 case 'R': 166 #ifdef DO_CP_TREE 156 167 Rflag = 1; 157 168 break; 169 #else 170 return errx(1, "recursive copy is not implemented!"); 171 #endif 158 172 case 'f': 159 173 fflag = 1; … … 172 186 break; 173 187 case 'r': 188 #ifdef DO_CP_TREE 174 189 rflag = 1; 175 190 break; 191 #else 192 return errx(1, "recursive copy is not implemented!"); 193 #endif 176 194 case 'v': 177 195 vflag = 1; 178 196 break; 179 197 default: 180 usage(); 181 break; 198 return usage(); 182 199 } 183 200 argc -= optind; … … 185 202 186 203 if (argc < 2) 187 usage();204 return usage(); 188 205 189 206 fts_options = FTS_NOCHDIR | FTS_PHYSICAL; 190 207 if (rflag) { 191 if (Rflag) { 192 fprintf(stderr, 193 "%s: the -R and -r options may not be specified together.\n", argv0); 194 return 1; 195 } 196 if (Hflag || Lflag || Pflag) { 197 fprintf(stderr, "%s: the -H, -L, and -P options may not be specified with the -r option.\n", argv0); 198 return 1; 199 } 208 if (Rflag) 209 return errx(1, 210 "the -R and -r options may not be specified together."); 211 if (Hflag || Lflag || Pflag) 212 errx(1, 213 "the -H, -L, and -P options may not be specified with the -r option."); 214 #ifdef DO_CP_TREE 200 215 fts_options &= ~FTS_PHYSICAL; 201 216 fts_options |= FTS_LOGICAL; 217 #endif 202 218 } 219 #ifdef DO_CP_TREE 203 220 if (Rflag) { 204 221 if (Hflag) … … 208 225 fts_options |= FTS_LOGICAL; 209 226 } 210 } else { 227 } else 228 #endif 229 { 211 230 fts_options &= ~FTS_PHYSICAL; 212 231 fts_options |= FTS_LOGICAL | FTS_COMFOLLOW; … … 218 237 /* Save the target base in "to". */ 219 238 target = argv[--argc]; 220 if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path)) { 221 fprintf(stderr, "%s: %s: name too long\n", argv0, target); 222 return 1; 223 } 239 if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path)) 240 return errx(1, "%s: name too long", target); 224 241 to.p_end = to.p_path + strlen(to.p_path); 225 242 if (to.p_path == to.p_end) { … … 250 267 */ 251 268 r = stat(to.p_path, &to_stat); 252 if (r == -1 && errno != ENOENT) { 253 fprintf(stderr, "%s: %s: %s\n", argv0, to.p_path, strerror(errno)); 254 } 269 if (r == -1 && errno != ENOENT) 270 return err(1, "%s", to.p_path); 255 271 if (r == -1 || !S_ISDIR(to_stat.st_mode)) { 256 272 /* 257 273 * Case (1). Target is not a directory. 258 274 */ 259 if (argc > 1) { 260 usage(); 261 return 1; 262 } 275 if (argc > 1) 276 return usage(); 263 277 /* 264 278 * Need to detect the case: … … 283 297 if (have_trailing_slash && type == FILE_TO_FILE) { 284 298 if (r == -1) 285 fprintf(stderr, "%s: directory %s does not exist\n", argv0,286 to.p_path);299 return errx(1, "directory %s does not exist", 300 to.p_path); 287 301 else 288 fprintf(stderr, "%s: %s is not a directory\n", argv0, to.p_path); 289 return 1; 302 return errx(1, "%s is not a directory", to.p_path); 290 303 } 291 304 } else … … 316 329 umask(~mask); 317 330 318 if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL) { 319 fprintf(stderr, "%s: fts_open: %s\n", argv0, strerror(errno)); 320 return 1; 321 } 331 if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL) 332 return err(1, "fts_open"); 322 333 for (badcp = rval = 0; (curr = fts_read(ftsp)) != NULL; badcp = 0) { 323 334 switch (curr->fts_info) { … … 325 336 case FTS_DNR: 326 337 case FTS_ERR: 327 fprintf(stderr, "%s: %s: %s\n",328 argv0,curr->fts_path, strerror(curr->fts_errno));338 warnx("%s: %s", 339 curr->fts_path, strerror(curr->fts_errno)); 329 340 badcp = rval = 1; 330 341 continue; 331 342 case FTS_DC: /* Warn, continue. */ 332 fprintf(stderr, "%s: %s: directory causes a cycle\n", argv0, curr->fts_path);343 warnx("%s: directory causes a cycle", curr->fts_path); 333 344 badcp = rval = 1; 334 345 continue; … … 381 392 *target_mid = 0; 382 393 if (target_mid - to.p_path + nlen >= PATH_MAX) { 383 fprintf(stderr, "%s: %s%s: name too long (not copied)\n", argv0,394 warnx("%s%s: name too long (not copied)", 384 395 to.p_path, p); 385 396 badcp = rval = 1; … … 417 428 ((mode | S_IRWXU) & mask) != (mode & mask)) 418 429 if (chmod(to.p_path, mode & mask) != 0){ 419 fprintf(stderr, "%s: chmod: %s: %s\n", argv0, to.p_path, strerror(errno));430 warn("chmod: %s", to.p_path); 420 431 rval = 1; 421 432 } … … 429 440 else { 430 441 if (to_stat.st_dev == curr->fts_statp->st_dev && 431 to_stat.st_ino == curr->fts_statp->st_ino) { 432 fprintf(stderr, "%s: %s and %s are identical (not copied).\n", 433 argv0, to.p_path, curr->fts_path); 442 to_stat.st_dev != 0 && 443 to_stat.st_ino == curr->fts_statp->st_ino && 444 to_stat.st_ino != 0) { 445 warnx("%s and %s are identical (not copied).", 446 to.p_path, curr->fts_path); 434 447 badcp = rval = 1; 435 448 if (S_ISDIR(curr->fts_statp->st_mode)) … … 439 452 if (!S_ISDIR(curr->fts_statp->st_mode) && 440 453 S_ISDIR(to_stat.st_mode)) { 441 fprintf(stderr, 442 "%s: cannot overwrite directory %s with " 443 "non-directory %s\n", 444 argv0, to.p_path, curr->fts_path); 454 warnx("cannot overwrite directory %s with " 455 "non-directory %s", 456 to.p_path, curr->fts_path); 445 457 badcp = rval = 1; 446 458 continue; … … 450 462 451 463 switch (curr->fts_statp->st_mode & S_IFMT) { 464 #ifdef S_IFLNK 452 465 case S_IFLNK: 453 466 /* Catch special case of a non-dangling symlink */ … … 462 475 } 463 476 break; 477 #endif 464 478 case S_IFDIR: 465 479 if (!Rflag && !rflag) { 466 fprintf(stderr, "%s: %s is a directory (not copied).\n",467 argv0,curr->fts_path);480 warnx("%s is a directory (not copied).", 481 curr->fts_path); 468 482 (void)fts_set(ftsp, curr, FTS_SKIP); 469 483 badcp = rval = 1; … … 480 494 if (dne) { 481 495 if (mkdir(to.p_path, 482 curr->fts_statp->st_mode | S_IRWXU) < 0) { 483 fprintf(stderr, "%s: %s: %s\n", argv0, to.p_path, strerror(errno)); 484 return 1; 485 } 496 curr->fts_statp->st_mode | S_IRWXU) < 0) 497 return err(1, "%s", to.p_path); 486 498 } else if (!S_ISDIR(to_stat.st_mode)) { 487 499 errno = ENOTDIR; 488 fprintf(stderr, "%s: %s: %s\n", argv0, to.p_path, strerror(errno));500 return err(1, "%s", to.p_path); 489 501 } 490 502 /* … … 495 507 curr->fts_number = pflag || dne; 496 508 break; 509 #ifdef S_IFBLK 497 510 case S_IFBLK: 511 #endif 498 512 case S_IFCHR: 499 513 if (Rflag) { … … 505 519 } 506 520 break; 521 #ifdef S_IFIFO 507 522 case S_IFIFO: 523 #endif 508 524 if (Rflag) { 509 525 if (copy_fifo(curr->fts_statp, !dne)) … … 522 538 (void)printf("%s -> %s\n", curr->fts_path, to.p_path); 523 539 } 524 if (errno) { 525 fprintf(stderr, "%s: fts_read: %s\n", argv0, strerror(errno)); 526 } 540 if (errno) 541 return err(1, "fts_read"); 527 542 return (rval); 528 543 }
Note:
See TracChangeset
for help on using the changeset viewer.