diff options
author | Sören Tempel <soeren+git@soeren-tempel.net> | 2021-07-17 21:45:40 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2021-10-12 17:32:03 +0200 |
commit | c33bbcb92fc255e4bb058e64874289cdeb0701f9 (patch) | |
tree | 29c40735df84c05f7a806d2fa1dd0d33768bde43 | |
parent | 94c78aa0b91f2150bd038866addf3d0ee69474a8 (diff) | |
download | busybox-w32-c33bbcb92fc255e4bb058e64874289cdeb0701f9.tar.gz busybox-w32-c33bbcb92fc255e4bb058e64874289cdeb0701f9.tar.bz2 busybox-w32-c33bbcb92fc255e4bb058e64874289cdeb0701f9.zip |
ed: align output of read/write commands with POSIX-1.2008
POSIX.1-2008 mandates the following regarding the write command:
If the command is successful, the number of bytes written shall
be written to standard output, unless the -s option was
specified, in the following format:
"%d\n", <number of bytes written>
function old new delta
readLines 447 409 -38
doCommands 1940 1889 -51
.rodata 104219 104163 -56
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-145) Total: -145 bytes
Signed-off-by: Sören Tempel <soeren+git@soeren-tempel.net>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | editors/ed.c | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/editors/ed.c b/editors/ed.c index c50faeefa..14540e566 100644 --- a/editors/ed.c +++ b/editors/ed.c | |||
@@ -380,7 +380,8 @@ static void addLines(int num) | |||
380 | static int readLines(const char *file, int num) | 380 | static int readLines(const char *file, int num) |
381 | { | 381 | { |
382 | int fd, cc; | 382 | int fd, cc; |
383 | int len, lineCount, charCount; | 383 | int len; |
384 | unsigned charCount; | ||
384 | char *cp; | 385 | char *cp; |
385 | 386 | ||
386 | if ((num < 1) || (num > lastNum + 1)) { | 387 | if ((num < 1) || (num > lastNum + 1)) { |
@@ -396,7 +397,6 @@ static int readLines(const char *file, int num) | |||
396 | 397 | ||
397 | bufPtr = bufBase; | 398 | bufPtr = bufBase; |
398 | bufUsed = 0; | 399 | bufUsed = 0; |
399 | lineCount = 0; | ||
400 | charCount = 0; | 400 | charCount = 0; |
401 | cc = 0; | 401 | cc = 0; |
402 | 402 | ||
@@ -415,7 +415,6 @@ static int readLines(const char *file, int num) | |||
415 | bufPtr += len; | 415 | bufPtr += len; |
416 | bufUsed -= len; | 416 | bufUsed -= len; |
417 | charCount += len; | 417 | charCount += len; |
418 | lineCount++; | ||
419 | num++; | 418 | num++; |
420 | continue; | 419 | continue; |
421 | } | 420 | } |
@@ -449,15 +448,18 @@ static int readLines(const char *file, int num) | |||
449 | close(fd); | 448 | close(fd); |
450 | return -1; | 449 | return -1; |
451 | } | 450 | } |
452 | lineCount++; | ||
453 | charCount += bufUsed; | 451 | charCount += bufUsed; |
454 | } | 452 | } |
455 | 453 | ||
456 | close(fd); | 454 | close(fd); |
457 | 455 | ||
458 | printf("%d lines%s, %d chars\n", lineCount, | 456 | /* https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ed.html |
459 | (bufUsed ? " (incomplete)" : ""), charCount); | 457 | * "Read Command" |
460 | 458 | * "...the number of bytes read shall be written to standard output | |
459 | * in the following format: | ||
460 | * "%d\n", <number of bytes read> | ||
461 | */ | ||
462 | printf("%u\n", charCount); | ||
461 | return TRUE; | 463 | return TRUE; |
462 | } | 464 | } |
463 | 465 | ||
@@ -468,12 +470,12 @@ static int readLines(const char *file, int num) | |||
468 | static int writeLines(const char *file, int num1, int num2) | 470 | static int writeLines(const char *file, int num1, int num2) |
469 | { | 471 | { |
470 | LINE *lp; | 472 | LINE *lp; |
471 | int fd, lineCount, charCount; | 473 | int fd; |
474 | unsigned charCount; | ||
472 | 475 | ||
473 | if (bad_nums(num1, num2, "write")) | 476 | if (bad_nums(num1, num2, "write")) |
474 | return FALSE; | 477 | return FALSE; |
475 | 478 | ||
476 | lineCount = 0; | ||
477 | charCount = 0; | 479 | charCount = 0; |
478 | 480 | ||
479 | fd = creat(file, 0666); | 481 | fd = creat(file, 0666); |
@@ -482,9 +484,6 @@ static int writeLines(const char *file, int num1, int num2) | |||
482 | return FALSE; | 484 | return FALSE; |
483 | } | 485 | } |
484 | 486 | ||
485 | printf("\"%s\", ", file); | ||
486 | fflush_all(); | ||
487 | |||
488 | lp = findLine(num1); | 487 | lp = findLine(num1); |
489 | if (lp == NULL) { | 488 | if (lp == NULL) { |
490 | close(fd); | 489 | close(fd); |
@@ -498,7 +497,6 @@ static int writeLines(const char *file, int num1, int num2) | |||
498 | return FALSE; | 497 | return FALSE; |
499 | } | 498 | } |
500 | charCount += lp->len; | 499 | charCount += lp->len; |
501 | lineCount++; | ||
502 | lp = lp->next; | 500 | lp = lp->next; |
503 | } | 501 | } |
504 | 502 | ||
@@ -507,7 +505,13 @@ static int writeLines(const char *file, int num1, int num2) | |||
507 | return FALSE; | 505 | return FALSE; |
508 | } | 506 | } |
509 | 507 | ||
510 | printf("%d lines, %d chars\n", lineCount, charCount); | 508 | /* https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ed.html |
509 | * "Write Command" | ||
510 | * "...the number of bytes written shall be written to standard output, | ||
511 | * unless the -s option was specified, in the following format: | ||
512 | * "%d\n", <number of bytes written> | ||
513 | */ | ||
514 | printf("%u\n", charCount); | ||
511 | return TRUE; | 515 | return TRUE; |
512 | } | 516 | } |
513 | 517 | ||