Changeset 3192 for trunk/src/kmk/kmkbuiltin/cp.c
- Timestamp:
- Mar 26, 2018, 10:25:56 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/kmkbuiltin/cp.c
r3148 r3192 157 157 158 158 159 static int copy( char *[], enum op, int);159 static int copy(PKMKBUILTINCTX pCtx, char *[], enum op, int); 160 160 static int mastercmp(const FTSENT **, const FTSENT **); 161 161 #ifdef SIGINFO 162 162 static void siginfo(int __unused); 163 163 #endif 164 static int usage( FILE *);164 static int usage(PKMKBUILTINCTX, int); 165 165 166 166 int 167 kmk_builtin_cp(int argc, char * argv[], char **envp)167 kmk_builtin_cp(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx) 168 168 { 169 169 struct stat to_stat, tmp_stat; … … 180 180 info = 0; 181 181 cp_ignore_non_existing = cp_changed_only = 0; 182 kBuildProtectionInit(&g_ProtData );182 kBuildProtectionInit(&g_ProtData, pCtx); 183 183 184 184 /* reset getopt and set progname. */ 185 g_progname = argv[0];186 185 opterr = 1; 187 186 optarg = NULL; … … 229 228 break; 230 229 case CP_OPT_HELP: 231 usage( stdout);230 usage(pCtx, 0); 232 231 kBuildProtectionTerm(&g_ProtData); 233 232 return 0; … … 261 260 default: 262 261 kBuildProtectionTerm(&g_ProtData); 263 return usage( stderr);262 return usage(pCtx, 1); 264 263 } 265 264 argc -= optind; … … 268 267 if (argc < 2) { 269 268 kBuildProtectionTerm(&g_ProtData); 270 return usage( stderr);269 return usage(pCtx, 1); 271 270 } 272 271 … … 275 274 if (Rflag) { 276 275 kBuildProtectionTerm(&g_ProtData); 277 return errx( 1,276 return errx(pCtx, 1, 278 277 "the -R and -r options may not be specified together."); 279 278 } 280 279 if (Hflag || Lflag || Pflag) 281 errx( 1,280 errx(pCtx, 1, 282 281 "the -H, -L, and -P options may not be specified with the -r option."); 283 282 fts_options &= ~FTS_PHYSICAL; … … 303 302 if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path)) { 304 303 kBuildProtectionTerm(&g_ProtData); 305 return errx( 1, "%s: name too long", target);304 return errx(pCtx, 1, "%s: name too long", target); 306 305 } 307 306 to.p_end = to.p_path + strlen(to.p_path); … … 335 334 if (r == -1 && errno != ENOENT) { 336 335 kBuildProtectionTerm(&g_ProtData); 337 return err( 1, "stat: %s", to.p_path);336 return err(pCtx, 1, "stat: %s", to.p_path); 338 337 } 339 338 if (r == -1 || !S_ISDIR(to_stat.st_mode)) { … … 343 342 if (argc > 1) { 344 343 kBuildProtectionTerm(&g_ProtData); 345 return usage( stderr);344 return usage(pCtx, 1); 346 345 } 347 346 /* … … 368 367 kBuildProtectionTerm(&g_ProtData); 369 368 if (r == -1) 370 return errx( 1, "directory %s does not exist",369 return errx(pCtx, 1, "directory %s does not exist", 371 370 to.p_path); 372 371 else 373 return errx( 1, "%s is not a directory", to.p_path);372 return errx(pCtx, 1, "%s is not a directory", to.p_path); 374 373 } 375 374 } else … … 387 386 : KBUILDPROTECTIONTYPE_FULL, 388 387 to.p_path)) { 389 rc = copy( argv, type, fts_options);388 rc = copy(pCtx, argv, type, fts_options); 390 389 } 391 390 … … 394 393 } 395 394 395 #ifdef KMK_BUILTIN_STANDALONE 396 int main(int argc, char **argv, char **envp) 397 { 398 KMKBUILTINCTX Ctx = { "kmk_cp", NULL }; 399 return kmk_builtin_cp(argc, argv, envp, &Ctx); 400 } 401 #endif 402 396 403 static int 397 copy( char *argv[], enum op type, int fts_options)404 copy(PKMKBUILTINCTX pCtx, char *argv[], enum op type, int fts_options) 398 405 { 399 406 struct stat to_stat; … … 413 420 414 421 if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL) 415 return err( 1, "fts_open");422 return err(pCtx, 1, "fts_open"); 416 423 for (badcp = rval = 0; (curr = fts_read(ftsp)) != NULL; badcp = 0) { 417 424 int copied = 0; … … 422 429 && curr->fts_errno == ENOENT) { 423 430 if (vflag) { 424 warnx( "fts: %s: %s", curr->fts_path,431 warnx(pCtx, "fts: %s: %s", curr->fts_path, 425 432 strerror(curr->fts_errno)); 426 433 } … … 430 437 case FTS_DNR: 431 438 case FTS_ERR: 432 warnx( "fts: %s: %s",439 warnx(pCtx, "fts: %s: %s", 433 440 curr->fts_path, strerror(curr->fts_errno)); 434 441 badcp = rval = 1; 435 442 continue; 436 443 case FTS_DC: /* Warn, continue. */ 437 warnx( "%s: directory causes a cycle", curr->fts_path);444 warnx(pCtx, "%s: directory causes a cycle", curr->fts_path); 438 445 badcp = rval = 1; 439 446 continue; … … 490 497 *target_mid = 0; 491 498 if (target_mid - to.p_path + nlen >= PATH_MAX) { 492 warnx( "%s%s: name too long (not copied)",499 warnx(pCtx, "%s%s: name too long (not copied)", 493 500 to.p_path, p); 494 501 badcp = rval = 1; … … 519 526 */ 520 527 if (pflag) { 521 if (setfile( curr->fts_statp, -1))528 if (setfile(pCtx, curr->fts_statp, -1)) 522 529 rval = 1; 523 530 } else { … … 526 533 ((mode | S_IRWXU) & mask) != (mode & mask)) 527 534 if (chmod(to.p_path, mode & mask) != 0){ 528 warn( "chmod: %s", to.p_path);535 warn(pCtx, "chmod: %s", to.p_path); 529 536 rval = 1; 530 537 } … … 541 548 to_stat.st_ino == curr->fts_statp->st_ino && 542 549 to_stat.st_ino != 0) { 543 warnx( "%s and %s are identical (not copied).",550 warnx(pCtx, "%s and %s are identical (not copied).", 544 551 to.p_path, curr->fts_path); 545 552 badcp = rval = 1; … … 550 557 if (!S_ISDIR(curr->fts_statp->st_mode) && 551 558 S_ISDIR(to_stat.st_mode)) { 552 warnx( "cannot overwrite directory %s with "559 warnx(pCtx, "cannot overwrite directory %s with " 553 560 "non-directory %s", 554 561 to.p_path, curr->fts_path); … … 566 573 ((fts_options & FTS_COMFOLLOW) && 567 574 curr->fts_level == 0)) { 568 if (copy_file( curr, dne, cp_changed_only, &copied))575 if (copy_file(pCtx, curr, dne, cp_changed_only, &copied)) 569 576 badcp = rval = 1; 570 577 } else { 571 if (copy_link( curr, !dne))578 if (copy_link(pCtx, curr, !dne)) 572 579 badcp = rval = 1; 573 580 } … … 576 583 case S_IFDIR: 577 584 if (!Rflag && !rflag) { 578 warnx( "%s is a directory (not copied).",585 warnx(pCtx, "%s is a directory (not copied).", 579 586 curr->fts_path); 580 587 (void)fts_set(ftsp, curr, FTS_SKIP); … … 593 600 if (mkdir(to.p_path, 594 601 curr->fts_statp->st_mode | S_IRWXU) < 0) 595 return err( 1, "mkdir: %s", to.p_path);602 return err(pCtx, 1, "mkdir: %s", to.p_path); 596 603 } else if (!S_ISDIR(to_stat.st_mode)) { 597 604 errno = ENOTDIR; 598 return err( 1, "to-mode: %s", to.p_path);605 return err(pCtx, 1, "to-mode: %s", to.p_path); 599 606 } 600 607 /* … … 610 617 case S_IFCHR: 611 618 if (Rflag) { 612 if (copy_special( curr->fts_statp, !dne))619 if (copy_special(pCtx, curr->fts_statp, !dne)) 613 620 badcp = rval = 1; 614 621 } else { 615 if (copy_file( curr, dne, cp_changed_only, &copied))622 if (copy_file(pCtx, curr, dne, cp_changed_only, &copied)) 616 623 badcp = rval = 1; 617 624 } … … 621 628 #endif 622 629 if (Rflag) { 623 if (copy_fifo( curr->fts_statp, !dne))630 if (copy_fifo(pCtx, curr->fts_statp, !dne)) 624 631 badcp = rval = 1; 625 632 } else { 626 if (copy_file( curr, dne, cp_changed_only, &copied))633 if (copy_file(pCtx, curr, dne, cp_changed_only, &copied)) 627 634 badcp = rval = 1; 628 635 } 629 636 break; 630 637 default: 631 if (copy_file( curr, dne, cp_changed_only, &copied))638 if (copy_file(pCtx, curr, dne, cp_changed_only, &copied)) 632 639 badcp = rval = 1; 633 640 break; 634 641 } 635 642 if (vflag && !badcp) 636 (void)printf(copied ? "%s -> %s\n" : "%s matches %s - not copied\n",637 curr->fts_path, to.p_path);643 kmk_builtin_ctx_printf(pCtx, 0, copied ? "%s -> %s\n" : "%s matches %s - not copied\n", 644 curr->fts_path, to.p_path); 638 645 } 639 646 if (errno) 640 return err( 1, "fts_read");647 return err(pCtx, 1, "fts_read"); 641 648 return (rval); 642 649 } … … 679 686 680 687 static int 681 usage( FILE *fp)688 usage(PKMKBUILTINCTX pCtx, int fIsErr) 682 689 { 683 fprintf(fp,690 kmk_builtin_ctx_printf(pCtx, fIsErr, 684 691 "usage: %s [options] src target\n" 685 692 " or: %s [options] src1 ... srcN directory\n" … … 728 735 "yourself in the foot.\n" 729 736 , 730 g_progname, g_progname, g_progname, g_progname, 737 pCtx->pszProgName, pCtx->pszProgName, 738 pCtx->pszProgName, pCtx->pszProgName, 731 739 kBuildProtectionDefaultDepth(), kBuildProtectionDefaultDepth()); 732 740 return 1;
Note:
See TracChangeset
for help on using the changeset viewer.