Changeset 3192 for trunk/src/kmk/kmkbuiltin/md5sum.c
- Timestamp:
- Mar 26, 2018, 10:25:56 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/kmkbuiltin/md5sum.c
r3159 r3192 49 49 * Prints the usage and return 1. 50 50 */ 51 static int usage( FILE *pOut)52 { 53 fprintf(pOut,54 "usage: md5sum [-bt] [-o list-file] file(s)\n"55 " or: md5sum [-btwq] -c list-file(s)\n"56 " or: md5sum [-btq] -C MD5 file\n"57 "\n"58 " -c, --check Check MD5 and files found in the specified list file(s).\n"59 " The default is to compute MD5 sums of the specified files\n"60 " and print them to stdout in list form.\n"61 " -C, --check-file This is followed by an MD5 sum and the file to check.\n"62 " -b, --binary Read files in binary mode. (default)\n"63 " -t, --text Read files in text mode.\n"64 " -m, --manifest Output in kBuild fetch 'manifest' format.\n"65 " -p, --progress Show progress indicator on large files.\n"66 " -o, --output Name of the output list file. Useful with -p.\n"67 " -q, --status Be quiet.\n"68 " -w, --warn Ignored. Always warn, unless quiet.\n"69 " -h, --help This usage info.\n"70 " -v, --version Show version information and exit.\n"71 );51 static int usage(PKMKBUILTINCTX pCtx, int fIsErr) 52 { 53 kmk_builtin_ctx_printf(pCtx, fIsErr, 54 "usage: md5sum [-bt] [-o list-file] file(s)\n" 55 " or: md5sum [-btwq] -c list-file(s)\n" 56 " or: md5sum [-btq] -C MD5 file\n" 57 "\n" 58 " -c, --check Check MD5 and files found in the specified list file(s).\n" 59 " The default is to compute MD5 sums of the specified files\n" 60 " and print them to stdout in list form.\n" 61 " -C, --check-file This is followed by an MD5 sum and the file to check.\n" 62 " -b, --binary Read files in binary mode. (default)\n" 63 " -t, --text Read files in text mode.\n" 64 " -m, --manifest Output in kBuild fetch 'manifest' format.\n" 65 " -p, --progress Show progress indicator on large files.\n" 66 " -o, --output Name of the output list file. Useful with -p.\n" 67 " -q, --status Be quiet.\n" 68 " -w, --warn Ignored. Always warn, unless quiet.\n" 69 " -h, --help This usage info.\n" 70 " -v, --version Show version information and exit.\n" 71 ); 72 72 return 1; 73 73 } … … 405 405 * 406 406 * @returns 0 if it matches, 1 if it doesn't or an error occurs. 407 * @param pCtx The command execution context. 407 408 * @param pszFilename The name of the file to check. 408 409 * @param pszDigest The MD5 digest string. … … 411 412 * @param fProgress Whether to show an progress indicator on large files. 412 413 */ 413 static int check_one_file(const char *pszFilename, const char *pszDigest, unsigned fText, unsigned fQuiet, unsigned fProgress) 414 static int check_one_file(PKMKBUILTINCTX pCtx, const char *pszFilename, const char *pszDigest, unsigned fText, 415 unsigned fQuiet, unsigned fProgress) 414 416 { 415 417 unsigned char Digest[16]; … … 425 427 { 426 428 if (!fQuiet) 427 fprintf(stdout, "%s: ", pszFilename);429 kmk_builtin_ctx_printf(pCtx, 0, "%s: ", pszFilename); 428 430 rc = check_md5sum(pvFile, Digest, fProgress); 429 431 close_file(pvFile); 430 432 if (!fQuiet) 431 433 { 432 fprintf(stdout, "%s\n", !rc ? "OK" : rc < 0 ? "FAILURE" : "ERROR"); 433 fflush(stdout); 434 kmk_builtin_ctx_printf(pCtx, 0, "%s\n", !rc ? "OK" : rc < 0 ? "FAILURE" : "ERROR"); 434 435 if (rc > 0) 435 errx( 1, "Error reading '%s': %s", pszFilename, strerror(rc));436 errx(pCtx, 1, "Error reading '%s': %s", pszFilename, strerror(rc)); 436 437 } 437 438 if (rc) … … 441 442 { 442 443 if (!fQuiet) 443 errx( 1, "Failed to open '%s': %s", pszFilename, strerror(errno));444 errx(pCtx, 1, "Failed to open '%s': %s", pszFilename, strerror(errno)); 444 445 rc = 1; 445 446 } … … 447 448 else 448 449 { 449 errx( 1, "Malformed MD5 digest '%s'!", pszDigest);450 errx( 1, " %*s^", rc - 1, "");450 errx(pCtx, 1, "Malformed MD5 digest '%s'!", pszDigest); 451 errx(pCtx, 1, " %*s^", rc - 1, ""); 451 452 rc = 1; 452 453 } … … 460 461 * 461 462 * @returns 0 if all checks out file, 1 if one or more fails or there are read errors. 463 * @param pCtx The command execution context. 462 464 * @param pszFilename The name of the file. 463 465 * @param fText The default mode, text or binary. Only used when fBinaryTextOpt is true. 464 466 * @param fBinaryTextOpt Whether a -b or -t option was specified and should be used. 465 467 * @param fQuiet Whether to be quiet. 466 * @param fProgress Whether to show an progress indicator on large files. 467 */ 468 static int check_files(const char *pszFilename, int fText, int fBinaryTextOpt, int fQuiet, unsigned fProgress) 468 * @param fProgress Whether to show an progress indicator on large files. 469 */ 470 static int check_files(PKMKBUILTINCTX pCtx, const char *pszFilename, int fText, int fBinaryTextOpt, 471 int fQuiet, unsigned fProgress) 469 472 { 470 473 int rc = 0; … … 539 542 { 540 543 if (!fQuiet) 541 fprintf(stdout, "%s: ", pszFilename);544 kmk_builtin_ctx_printf(pCtx, 0, "%s: ", pszFilename); 542 545 rc2 = check_md5sum(pvFile, Digest, fProgress); 543 546 close_file(pvFile); 544 547 if (!fQuiet) 545 548 { 546 fprintf(stdout, "%s\n", !rc2 ? "OK" : rc2 < 0 ? "FAILURE" : "ERROR"); 547 fflush(stdout); 549 kmk_builtin_ctx_printf(pCtx, 0, "%s\n", !rc2 ? "OK" : rc2 < 0 ? "FAILURE" : "ERROR"); 548 550 if (rc2 > 0) 549 errx( 1, "Error reading '%s': %s", pszFilename, strerror(rc2));551 errx(pCtx, 1, "Error reading '%s': %s", pszFilename, strerror(rc2)); 550 552 } 551 553 if (rc2) … … 555 557 { 556 558 if (!fQuiet) 557 errx( 1, "Failed to open '%s': %s", pszFilename, strerror(errno));559 errx(pCtx, 1, "Failed to open '%s': %s", pszFilename, strerror(errno)); 558 560 rc = 1; 559 561 } … … 561 563 else if (!fQuiet) 562 564 { 563 errx( 1, "%s (%d): Ignoring malformed digest '%s' (digest)", pszFilename, iLine, pszDigest);564 errx( 1, "%s (%d): %*s^", pszFilename, iLine, rc2 - 1, "");565 errx(pCtx, 1, "%s (%d): Ignoring malformed digest '%s' (digest)", pszFilename, iLine, pszDigest); 566 errx(pCtx, 1, "%s (%d): %*s^", pszFilename, iLine, rc2 - 1, ""); 565 567 } 566 568 } 567 569 else if (!fQuiet) 568 errx( 1, "%s (%d): Ignoring malformed line!", pszFilename, iLine);570 errx(pCtx, 1, "%s (%d): Ignoring malformed line!", pszFilename, iLine); 569 571 } 570 572 else if (!fQuiet) 571 errx( 1, "%s (%d): Ignoring malformed line!", pszFilename, iLine);573 errx(pCtx, 1, "%s (%d): Ignoring malformed line!", pszFilename, iLine); 572 574 } /* while more lines */ 573 575 … … 576 578 else 577 579 { 578 errx( 1, "Failed to open '%s': %s", pszFilename, strerror(errno));580 errx(pCtx, 1, "Failed to open '%s': %s", pszFilename, strerror(errno)); 579 581 rc = 1; 580 582 } … … 588 590 * 589 591 * @returns 0 on success, 1 on any kind of failure. 592 * @param pCtx Command context. 590 593 * @param pszFilename The file to process. 591 594 * @param fText The mode to open the file in. … … 595 598 * @param pOutput Where to write the list. Progress is always written to stdout. 596 599 */ 597 static int md5sum_file( const char *pszFilename, unsigned fText, unsigned fQuiet, unsigned fProgress,600 static int md5sum_file(PKMKBUILTINCTX pCtx, const char *pszFilename, unsigned fText, unsigned fQuiet, unsigned fProgress, 598 601 unsigned fManifest, FILE *pOutput) 599 602 { … … 631 634 if (pOutput) 632 635 fprintf(pOutput, "%s %s%s\n", szDigest, fText ? "" : "*", pszFilename); 633 fprintf(stdout, "%s %s%s\n", szDigest, fText ? "" : "*", pszFilename);636 kmk_builtin_ctx_printf(pCtx, 0, "%s %s%s\n", szDigest, fText ? "" : "*", pszFilename); 634 637 } 635 638 else … … 637 640 if (pOutput) 638 641 fprintf(pOutput, "%s_SIZE := %" KU64_PRI "\n%s_MD5 := %s\n", pszFilename, cbFile, pszFilename, szDigest); 639 fprintf(stdout, "%s_SIZE := %" KU64_PRI "\n%s_MD5 := %s\n", pszFilename, cbFile, pszFilename, szDigest); 642 kmk_builtin_ctx_printf(pCtx, 0, "%s_SIZE := %" KU64_PRI "\n%s_MD5 := %s\n", 643 pszFilename, cbFile, pszFilename, szDigest); 640 644 } 641 645 if (pOutput) 642 646 fflush(pOutput); 643 fflush(stdout);644 647 } 645 648 else 646 649 { 647 650 if (!fQuiet) 648 errx( 1, "Failed to open '%s': %s", pszFilename, strerror(rc));651 errx(pCtx, 1, "Failed to open '%s': %s", pszFilename, strerror(rc)); 649 652 rc = 1; 650 653 } … … 653 656 { 654 657 if (!fQuiet) 655 errx( 1, "Failed to open '%s': %s", pszFilename, strerror(errno));658 errx(pCtx, 1, "Failed to open '%s': %s", pszFilename, strerror(errno)); 656 659 rc = 1; 657 660 } … … 665 668 * Somewhat similar to the GNU coreutil md5sum command. 666 669 */ 667 int kmk_builtin_md5sum(int argc, char **argv, char **envp )670 int kmk_builtin_md5sum(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx) 668 671 { 669 672 int i; … … 679 682 FILE *pOutput = NULL; 680 683 681 g_progname = argv[0];682 683 684 /* 684 685 * Print usage if no arguments. 685 686 */ 686 687 if (argc <= 1) 687 return usage( stderr);688 return usage(pCtx, 1); 688 689 689 690 /* … … 751 752 752 753 case 'p': 753 fProgress = 1; 754 fProgress = 1 && isatty(fileno(stdout)) 755 #ifndef KMK_BUILTIN_STANDALONE 756 && (!pCtx->pOut || !pCtx->pOut->syncout) 757 #endif 758 ; 754 759 break; 755 760 … … 763 768 764 769 case 'h': 765 usage( stdout);770 usage(pCtx, 0); 766 771 return 0; 767 772 … … 783 788 else 784 789 { 785 errx( 1, "'-C' is missing the MD5 sum!");790 errx(pCtx, 1, "'-C' is missing the MD5 sum!"); 786 791 return 1; 787 792 } … … 790 795 else 791 796 { 792 errx( 1, "'-C' is missing the filename!");797 errx(pCtx, 1, "'-C' is missing the filename!"); 793 798 return 1; 794 799 } 795 800 796 rc |= check_one_file(p szFilename, pszDigest, fText, fQuiet, fProgress && !fQuiet);801 rc |= check_one_file(pCtx, pszFilename, pszDigest, fText, fQuiet, fProgress && !fQuiet); 797 802 psz = "\0"; 798 803 break; … … 806 811 if (fChecking) 807 812 { 808 errx( 1, "'-o' cannot be used with -c or -C!");813 errx(pCtx, 1, "'-o' cannot be used with -c or -C!"); 809 814 return 1; 810 815 } … … 816 821 else 817 822 { 818 errx( 1, "'-o' is missing the file name!");823 errx(pCtx, 1, "'-o' is missing the file name!"); 819 824 return 1; 820 825 } … … 825 830 826 831 default: 827 errx( 1, "Invalid option '%c'! (%s)", *psz, argv[i]);828 return usage( stderr);832 errx(pCtx, 1, "Invalid option '%c'! (%s)", *psz, argv[i]); 833 return usage(pCtx, 1); 829 834 } 830 835 } while (*++psz); 831 836 } 832 837 else if (fChecking) 833 rc |= check_files( argv[i], fText, fBinaryTextOpt, fQuiet, fProgress && !fQuiet);838 rc |= check_files(pCtx, argv[i], fText, fBinaryTextOpt, fQuiet, fProgress && !fQuiet); 834 839 else 835 840 { … … 842 847 if (!pOutput) 843 848 { 844 rc = err( 1, "fopen(\"%s\", \"w" KMK_FOPEN_NO_INHERIT_MODE "\") failed", pszOutput);849 rc = err(pCtx, 1, "fopen(\"%s\", \"w" KMK_FOPEN_NO_INHERIT_MODE "\") failed", pszOutput); 845 850 break; 846 851 } … … 848 853 } 849 854 850 rc |= md5sum_file( argv[i], fText, fQuiet, fProgress && !fQuiet && !fManifest, fManifest, pOutput);855 rc |= md5sum_file(pCtx, argv[i], fText, fQuiet, fProgress && !fQuiet && !fManifest, fManifest, pOutput); 851 856 } 852 857 i++; … … 858 863 } 859 864 865 866 #ifdef KMK_BUILTIN_STANDALONE 867 int main(int argc, char **argv, char **envp) 868 { 869 KMKBUILTINCTX Ctx = { "kmk_md5sum", NULL }; 870 return kmk_builtin_md5sum(argc, argv, envp, &Ctx); 871 } 872 #endif 873 874
Note:
See TracChangeset
for help on using the changeset viewer.