Changeset 3192 for trunk/src/kmk/kmkbuiltin/cmp_util.c
- Timestamp:
- Mar 26, 2018, 10:25:56 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kmk/kmkbuiltin/cmp_util.c
r3095 r3192 66 66 67 67 static int 68 errmsg( const char *file, off_t byte, off_t line, int lflag)68 errmsg(PKMKBUILTINCTX pCtx, const char *file, off_t byte, off_t line, int lflag) 69 69 { 70 70 if (lflag) 71 71 #ifdef _MSC_VER 72 return err( ERR_EXIT, "%s: char %I64d, line %lld", file, (__int64)byte, (long long)line);73 #else 74 return err( ERR_EXIT, "%s: char %lld, line %lld", file, (long long)byte, (long long)line);75 #endif 76 return err( ERR_EXIT, "%s", file);72 return err(pCtx, ERR_EXIT, "%s: char %I64d, line %lld", file, (__int64)byte, (long long)line); 73 #else 74 return err(pCtx, ERR_EXIT, "%s: char %lld, line %lld", file, (long long)byte, (long long)line); 75 #endif 76 return err(pCtx, ERR_EXIT, "%s", file); 77 77 } 78 78 79 79 80 80 static int 81 eofmsg( const char *file, off_t byte, off_t line, int sflag, int lflag)81 eofmsg(PKMKBUILTINCTX pCtx, const char *file, off_t byte, off_t line, int sflag, int lflag) 82 82 { 83 83 if (!sflag) 84 84 { 85 85 if (!lflag) 86 warnx( "EOF on %s", file);86 warnx(pCtx, "EOF on %s", file); 87 87 else 88 88 { 89 89 #ifdef _MSC_VER 90 90 if (line > 0) 91 warnx( "EOF on %s: char %I64d, line %I64d", file, (__int64)byte, (__int64)line);91 warnx(pCtx, "EOF on %s: char %I64d, line %I64d", file, (__int64)byte, (__int64)line); 92 92 else 93 warnx( "EOF on %s: char %I64d", file, (__int64)byte);93 warnx(pCtx, "EOF on %s: char %I64d", file, (__int64)byte); 94 94 #else 95 95 if (line > 0) 96 warnx( "EOF on %s: char %lld, line %lld", file, (long long)byte, (long long)line);96 warnx(pCtx, "EOF on %s: char %lld, line %lld", file, (long long)byte, (long long)line); 97 97 else 98 warnx( "EOF on %s: char %lld", file, (long long)byte);98 warnx(pCtx, "EOF on %s: char %lld", file, (long long)byte); 99 99 #endif 100 100 } … … 105 105 106 106 static int 107 diffmsg( const char *file1, const char *file2, off_t byte, off_t line, int sflag)107 diffmsg(PKMKBUILTINCTX pCtx, const char *file1, const char *file2, off_t byte, off_t line, int sflag) 108 108 { 109 109 if (!sflag) 110 110 #ifdef _MSC_VER 111 printf("%s %s differ: char %I64d, line %I64d\n",111 kmk_builtin_ctx_printf(pCtx, 0, "%s %s differ: char %I64d, line %I64d\n", 112 112 file1, file2, (__int64)byte, (__int64)line); 113 113 #else 114 printf("%s %s differ: char %lld, line %lld\n",114 kmk_builtin_ctx_printf(pCtx, 0, "%s %s differ: char %lld, line %lld\n", 115 115 file1, file2, (long long)byte, (long long)line); 116 116 #endif … … 123 123 */ 124 124 static int 125 c_special( int fd1, const char *file1, off_t skip1,125 c_special(PKMKBUILTINCTX pCtx, int fd1, const char *file1, off_t skip1, 126 126 int fd2, const char *file2, off_t skip2, 127 127 int lflag, int sflag) … … 134 134 fd1dup = dup(fd1); 135 135 if (fd1 < 0) 136 return err( ERR_EXIT, "%s", file1);136 return err(pCtx, ERR_EXIT, "%s", file1); 137 137 fp1 = fdopen(fd1dup, "rb"); 138 138 if (!fp1) … … 140 140 if (!fp1) 141 141 { 142 err( ERR_EXIT, "%s", file1);142 err(pCtx, ERR_EXIT, "%s", file1); 143 143 close(fd1dup); 144 144 return ERR_EXIT; … … 189 189 if (!lflag) 190 190 { 191 rc = diffmsg( file1, file2, byte, line, sflag);191 rc = diffmsg(pCtx, file1, file2, byte, line, sflag); 192 192 break; 193 193 } 194 194 rc = DIFF_EXIT; 195 195 #ifdef _MSC_VER 196 printf("%6i64d %3o %3o\n", (__int64)byte, ch1, ch2);197 #else 198 printf("%6lld %3o %3o\n", (long long)byte, ch1, ch2);196 kmk_builtin_ctx_printf(pCtx, 0, "%6i64d %3o %3o\n", (__int64)byte, ch1, ch2); 197 #else 198 kmk_builtin_ctx_printf(pCtx, 0, "%6lld %3o %3o\n", (long long)byte, ch1, ch2); 199 199 #endif 200 200 } … … 206 206 /* Check for errors and length differences (EOF). */ 207 207 if (ferror(fp1) && rc != ERR_EXIT) 208 rc = errmsg( file1, byte, line, lflag);208 rc = errmsg(pCtx, file1, byte, line, lflag); 209 209 if (ferror(fp2) && rc != ERR_EXIT) 210 rc = errmsg( file2, byte, line, lflag);210 rc = errmsg(pCtx, file2, byte, line, lflag); 211 211 if (rc == OK_EXIT) 212 212 { … … 214 214 { 215 215 if (!feof(fp2)) 216 rc = eofmsg( file1, byte, line, sflag, lflag);216 rc = eofmsg(pCtx, file1, byte, line, sflag, lflag); 217 217 } 218 218 else if (feof(fp2)) 219 rc = eofmsg( file2, byte, line, sflag, lflag);219 rc = eofmsg(pCtx, file2, byte, line, sflag, lflag); 220 220 } 221 221 … … 224 224 else 225 225 { 226 rc = err( ERR_EXIT, "%s", file2);226 rc = err(pCtx, ERR_EXIT, "%s", file2); 227 227 close(fd2dup); 228 228 } 229 229 } 230 230 else 231 rc = err( ERR_EXIT, "%s", file2);231 rc = err(pCtx, ERR_EXIT, "%s", file2); 232 232 233 233 fclose(fp1); … … 241 241 */ 242 242 static int 243 c_regular( int fd1, const char *file1, off_t skip1, off_t len1,243 c_regular(PKMKBUILTINCTX pCtx, int fd1, const char *file1, off_t skip1, off_t len1, 244 244 int fd2, const char *file2, off_t skip2, off_t len2, int sflag, int lflag) 245 245 { … … 253 253 254 254 if (skip1 > len1) 255 return eofmsg( file1, len1 + 1, 0, sflag, lflag);255 return eofmsg(pCtx, file1, len1 + 1, 0, sflag, lflag); 256 256 len1 -= skip1; 257 257 if (skip2 > len2) 258 return eofmsg( file2, len2 + 1, 0, sflag, lflag);258 return eofmsg(pCtx, file2, len2 + 1, 0, sflag, lflag); 259 259 len2 -= skip2; 260 260 … … 290 290 dfound = 1; 291 291 #ifdef _MSC_VER 292 printf("%6I64d %3o %3o\n", (__int64)byte, ch, *p2);293 #else 294 printf("%6lld %3o %3o\n", (long long)byte, ch, *p2);292 kmk_builtin_ctx_printf(pCtx, 0, "%6I64d %3o %3o\n", (__int64)byte, ch, *p2); 293 #else 294 kmk_builtin_ctx_printf(pCtx, 0, "%6lld %3o %3o\n", (long long)byte, ch, *p2); 295 295 #endif 296 296 } … … 305 305 306 306 if (len1 != len2) 307 return eofmsg( len1 > len2 ? file2 : file1, byte, line, sflag, lflag);307 return eofmsg(pCtx, len1 > len2 ? file2 : file1, byte, line, sflag, lflag); 308 308 if (dfound) 309 309 return DIFF_EXIT; … … 311 311 312 312 l_mmap_failed: 313 return c_special( fd1, file1, skip1, fd2, file2, skip2, lflag, sflag);313 return c_special(pCtx, fd1, file1, skip1, fd2, file2, skip2, lflag, sflag); 314 314 } 315 315 … … 320 320 */ 321 321 static int 322 c_regular( int fd1, const char *file1, off_t skip1, off_t len1,322 c_regular(PKMKBUILTINCTX pCtx, int fd1, const char *file1, off_t skip1, off_t len1, 323 323 int fd2, const char *file2, off_t skip2, off_t len2, int sflag, int lflag) 324 324 { … … 332 332 333 333 if (skip1 > len1) 334 return eofmsg( file1, len1 + 1, 0, sflag, lflag);334 return eofmsg(pCtx, file1, len1 + 1, 0, sflag, lflag); 335 335 len1 -= skip1; 336 336 if (skip2 > len2) 337 return eofmsg( file2, len2 + 1, 0, sflag, lflag);337 return eofmsg(pCtx, file2, len2 + 1, 0, sflag, lflag); 338 338 len2 -= skip2; 339 339 … … 343 343 { 344 344 if (skip1 && lseek(fd1, 0, SEEK_SET) < 0) 345 return err( 1, "seek failed");345 return err(pCtx, 1, "seek failed"); 346 346 goto l_special; 347 347 } … … 383 383 free(b1); 384 384 free(b2); 385 return diffmsg( file1, file2, byte, line, sflag);385 return diffmsg(pCtx, file1, file2, byte, line, sflag); 386 386 } 387 387 dfound = 1; 388 388 #ifdef _MSC_VER 389 printf("%6I64d %3o %3o\n", (__int64)byte, ch, *p2);390 #else 391 printf("%6lld %3o %3o\n", (long long)byte, ch, *p2);389 kmk_builtin_ctx_printf(pCtx, 0, "%6I64d %3o %3o\n", (__int64)byte, ch, *p2); 390 #else 391 kmk_builtin_ctx_printf(pCtx, 0, "%6lld %3o %3o\n", (long long)byte, ch, *p2); 392 392 #endif 393 393 } … … 400 400 401 401 if (len1 != len2) 402 return eofmsg( len1 > len2 ? file2 : file1, byte, line, sflag, lflag);402 return eofmsg(pCtx, len1 > len2 ? file2 : file1, byte, line, sflag, lflag); 403 403 if (dfound) 404 404 return DIFF_EXIT; … … 409 409 || lseek(fd2, 0, SEEK_SET) < 0) 410 410 { 411 err( 1, "seek failed");411 err(pCtx, 1, "seek failed"); 412 412 free(b1); 413 413 free(b2); … … 418 418 free(b2); 419 419 l_special: 420 return c_special( fd1, file1, skip1, fd2, file2, skip2, lflag, sflag);420 return c_special(pCtx, fd1, file1, skip1, fd2, file2, skip2, lflag, sflag); 421 421 } 422 422 #endif /* non-mmap c_regular */ … … 427 427 */ 428 428 int 429 cmp_fd_and_fd_ex( int fd1, const char *file1, off_t skip1,429 cmp_fd_and_fd_ex(PKMKBUILTINCTX pCtx, int fd1, const char *file1, off_t skip1, 430 430 int fd2, const char *file2, off_t skip2, 431 431 int sflag, int lflag, int special) … … 435 435 436 436 if (fstat(fd1, &st1)) 437 return err( ERR_EXIT, "%s", file1);437 return err(pCtx, ERR_EXIT, "%s", file1); 438 438 if (fstat(fd2, &st2)) 439 return err( ERR_EXIT, "%s", file2);439 return err(pCtx, ERR_EXIT, "%s", file2); 440 440 441 441 if ( !S_ISREG(st1.st_mode) 442 442 || !S_ISREG(st2.st_mode) 443 443 || special) 444 rc = c_special( fd1, file1, skip1,444 rc = c_special(pCtx, fd1, file1, skip1, 445 445 fd2, file2, skip2, sflag, lflag); 446 446 else 447 rc = c_regular( fd1, file1, skip1, st1.st_size,447 rc = c_regular(pCtx, fd1, file1, skip1, st1.st_size, 448 448 fd2, file2, skip2, st2.st_size, sflag, lflag); 449 449 return rc; … … 455 455 */ 456 456 int 457 cmp_fd_and_fd( int fd1, const char *file1,457 cmp_fd_and_fd(PKMKBUILTINCTX pCtx, int fd1, const char *file1, 458 458 int fd2, const char *file2, 459 459 int sflag, int lflag, int special) 460 460 { 461 return cmp_fd_and_fd_ex( fd1, file1, 0, fd2, file2, 0, sflag, lflag, special);461 return cmp_fd_and_fd_ex(pCtx, fd1, file1, 0, fd2, file2, 0, sflag, lflag, special); 462 462 } 463 463 … … 467 467 */ 468 468 int 469 cmp_fd_and_file_ex( int fd1, const char *file1, off_t skip1,469 cmp_fd_and_file_ex(PKMKBUILTINCTX pCtx, int fd1, const char *file1, off_t skip1, 470 470 const char *file2, off_t skip2, 471 471 int sflag, int lflag, int special) … … 484 484 if (fd2 >= 0) 485 485 { 486 rc = cmp_fd_and_fd_ex( fd1, file1, skip1,486 rc = cmp_fd_and_fd_ex(pCtx, fd1, file1, skip1, 487 487 fd2, file2, skip2, sflag, lflag, special); 488 488 close(fd2); … … 491 491 { 492 492 if (!sflag) 493 warn( "%s", file2);493 warn(pCtx, "%s", file2); 494 494 rc = ERR_EXIT; 495 495 } … … 502 502 */ 503 503 int 504 cmp_fd_and_file( int fd1, const char *file1,504 cmp_fd_and_file(PKMKBUILTINCTX pCtx, int fd1, const char *file1, 505 505 const char *file2, 506 506 int sflag, int lflag, int special) 507 507 { 508 return cmp_fd_and_file_ex( fd1, file1, 0,508 return cmp_fd_and_file_ex(pCtx, fd1, file1, 0, 509 509 file2, 0, sflag, lflag, special); 510 510 } … … 515 515 */ 516 516 int 517 cmp_file_and_file_ex( const char *file1, off_t skip1,517 cmp_file_and_file_ex(PKMKBUILTINCTX pCtx, const char *file1, off_t skip1, 518 518 const char *file2, off_t skip2, 519 519 int sflag, int lflag, int special) … … 523 523 524 524 if (lflag && sflag) 525 return errx( ERR_EXIT, "only one of -l and -s may be specified");525 return errx(pCtx, ERR_EXIT, "only one of -l and -s may be specified"); 526 526 527 527 if (!strcmp(file1, "-")) 528 528 { 529 529 if (!strcmp(file2, "-")) 530 return errx( ERR_EXIT, "standard input may only be specified once");530 return errx(pCtx, ERR_EXIT, "standard input may only be specified once"); 531 531 file1 = "stdin"; 532 532 fd1 = 1; … … 537 537 if (fd1 >= 0) 538 538 { 539 rc = cmp_fd_and_file_ex( fd1, file1, skip1,539 rc = cmp_fd_and_file_ex(pCtx, fd1, file1, skip1, 540 540 file2, skip2, sflag, lflag, special); 541 541 close(fd1); … … 544 544 { 545 545 if (!sflag) 546 warn( "%s", file1);546 warn(pCtx, "%s", file1); 547 547 rc = ERR_EXIT; 548 548 } … … 556 556 */ 557 557 int 558 cmp_file_and_file( const char *file1, const char *file2, int sflag, int lflag, int special)559 { 560 return cmp_file_and_file_ex( file1, 0, file2, 0, sflag, lflag, special);561 } 562 558 cmp_file_and_file(PKMKBUILTINCTX pCtx, const char *file1, const char *file2, int sflag, int lflag, int special) 559 { 560 return cmp_file_and_file_ex(pCtx, file1, 0, file2, 0, sflag, lflag, special); 561 } 562
Note:
See TracChangeset
for help on using the changeset viewer.