- Timestamp:
- Feb 9, 2018 1:19:39 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/kmkbuiltin/md5sum.c
r2984 r3131 41 41 #include "kmkbuiltin.h" 42 42 #include "../../lib/md5.h" 43 #include <k/kTypes.h> 43 44 44 45 /*#define MD5SUM_USE_STDIO*/ … … 61 62 " -b, --binary Read files in binary mode. (default)\n" 62 63 " -t, --text Read files in text mode.\n" 64 " -m, --manifest Output in kBuild fetch 'manifest' format.\n" 63 65 " -p, --progress Show progress indicator on large files.\n" 64 66 " -o, --output Name of the output list file. Useful with -p.\n" … … 262 264 * @param pvFile The opaque pointer returned by open_file 263 265 */ 264 static doublesize_file(void *pvFile)266 static KU64 size_file(void *pvFile) 265 267 { 266 268 #if defined(_MSC_VER) … … 272 274 # endif 273 275 if (cb >= 0) 274 return (double)cb;276 return cb; 275 277 276 278 #elif defined(MD5SUM_USE_STDIO) … … 295 297 * @param pDigest Where to store the MD5 digest. 296 298 * @param fProgress Whether to show a progress bar. 297 */ 298 static int calc_md5sum(void *pvFile, unsigned char pDigest[16], unsigned fProgress) 299 * @param pcbFile Where to return the file size. Optional. 300 */ 301 static int calc_md5sum(void *pvFile, unsigned char pDigest[16], unsigned fProgress, KU64 *pcbFile) 299 302 { 300 303 int cb; … … 302 305 struct MD5Context Ctx; 303 306 unsigned uPercent = 0; 304 double off = 0.0;305 doublecbFile = size_file(pvFile);307 KU64 off = 0; 308 KU64 const cbFile = size_file(pvFile); 306 309 307 310 /* Get a decent sized buffer assuming we'll be spending more time reading … … 346 349 break; 347 350 } 351 off += cb; 348 352 349 353 /* update the progress indicator. */ … … 351 355 { 352 356 unsigned uNewPercent; 353 off += cb; 354 uNewPercent = (unsigned)((off / cbFile) * 100); 357 uNewPercent = (unsigned)(((double)off / cbFile) * 100); 355 358 if (uNewPercent != uPercent) 356 359 { … … 365 368 MD5Final(pDigest, &Ctx); 366 369 370 if (pcbFile) 371 *pcbFile = off; 372 367 373 if (fProgress) 368 374 printf("\b\b\b\b \b\b\b\b"); … … 386 392 int rc; 387 393 388 rc = calc_md5sum(pvFile, DigestFile, fProgress );394 rc = calc_md5sum(pvFile, DigestFile, fProgress, NULL); 389 395 if (!rc) 390 396 rc = memcmp(Digest, DigestFile, 16) ? -1 : 0; … … 583 589 * @param fText The mode to open the file in. 584 590 * @param fQuiet Whether to be quiet or verbose about errors. 591 * @param fManifest Whether to format the output like a fetch manifest. 585 592 * @param fProgress Whether to show an progress indicator on large files. 586 593 * @param pOutput Where to write the list. Progress is always written to stdout. 587 594 */ 588 static int md5sum_file(const char *pszFilename, unsigned fText, unsigned fQuiet, unsigned fProgress, FILE *pOutput) 595 static int md5sum_file(const char *pszFilename, unsigned fText, unsigned fQuiet, unsigned fProgress, 596 unsigned fManifest, FILE *pOutput) 589 597 { 590 598 int rc; … … 598 606 { 599 607 unsigned char Digest[16]; 608 KU64 cbFile = 0; 600 609 601 610 if (fProgress && pOutput) 602 611 fprintf(stdout, "%s: ", pszFilename); 603 612 604 rc = calc_md5sum(pvFile, Digest, fProgress );613 rc = calc_md5sum(pvFile, Digest, fProgress, &cbFile); 605 614 close_file(pvFile); 606 615 … … 616 625 char szDigest[36]; 617 626 digest_to_string(Digest, szDigest); 627 if (!fManifest) 628 { 629 if (pOutput) 630 fprintf(pOutput, "%s %s%s\n", szDigest, fText ? "" : "*", pszFilename); 631 fprintf(stdout, "%s %s%s\n", szDigest, fText ? "" : "*", pszFilename); 632 } 633 else 634 { 635 if (pOutput) 636 fprintf(pOutput, "%s_SIZE := %" KU64_PRI "\n%s_MD5 := %s\n", pszFilename, cbFile, pszFilename, szDigest); 637 fprintf(stdout, "%s_SIZE := %" KU64_PRI "\n%s_MD5 := %s\n", pszFilename, cbFile, pszFilename, szDigest); 638 } 618 639 if (pOutput) 619 {620 fprintf(pOutput, "%s %s%s\n", szDigest, fText ? "" : "*", pszFilename);621 640 fflush(pOutput); 622 }623 fprintf(stdout, "%s %s%s\n", szDigest, fText ? "" : "*", pszFilename);624 641 fflush(stdout); 625 642 } … … 654 671 int fQuiet = 0; 655 672 int fChecking = 0; 673 int fManifest = 0; 656 674 int fProgress = 0; 657 675 int fNoMoreOptions = 0; … … 691 709 else if (!strcmp(psz, "-check-file")) 692 710 psz = "C"; 711 else if (!strcmp(psz, "-manifest")) 712 psz = "m"; 693 713 else if (!strcmp(psz, "-output")) 694 714 psz = "o"; … … 722 742 fText = 1; 723 743 fBinaryTextOpt = 1; 744 break; 745 746 case 'm': 747 fManifest = 1; 724 748 break; 725 749 … … 822 846 } 823 847 824 rc |= md5sum_file(argv[i], fText, fQuiet, fProgress && !fQuiet , pOutput);848 rc |= md5sum_file(argv[i], fText, fQuiet, fProgress && !fQuiet && !fManifest, fManifest, pOutput); 825 849 } 826 850 i++;
Note:
See TracChangeset
for help on using the changeset viewer.