aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2019-08-03 18:08:27 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2019-08-03 18:08:27 +0200
commitb81f80565854d59c0209a6dbaa2022caa6f0d468 (patch)
tree43655744e152896903e0a8e8e881830e4f7c14c8
parent2f9c124f7daf4645ca62f6a25ca9be40fcc5275d (diff)
downloadbusybox-w32-b81f80565854d59c0209a6dbaa2022caa6f0d468.tar.gz
busybox-w32-b81f80565854d59c0209a6dbaa2022caa6f0d468.tar.bz2
busybox-w32-b81f80565854d59c0209a6dbaa2022caa6f0d468.zip
stat: print nanosecond times, fix printing of empty lines
function old new delta human_time 36 73 +37 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--coreutils/stat.c75
1 files changed, 36 insertions, 39 deletions
diff --git a/coreutils/stat.c b/coreutils/stat.c
index b6ab5205b..fa9ac7b10 100644
--- a/coreutils/stat.c
+++ b/coreutils/stat.c
@@ -150,20 +150,16 @@ static const char *file_type(const struct stat *st)
150 return "weird file"; 150 return "weird file";
151} 151}
152 152
153static const char *human_time(time_t t) 153static const char *human_time(struct timespec *ts)
154{ 154{
155 /* Old 155 char fmt[sizeof("%Y-%m-%d %H:%M:%S.123456789 %z") + /*paranoia*/ 8];
156 static char *str; 156
157 str = ctime(&t); 157 /* coreutils 6.3 compat */
158 str[strlen(str)-1] = '\0';
159 return str;
160 */
161 /* coreutils 6.3 compat: */
162
163 /*static char buf[sizeof("YYYY-MM-DD HH:MM:SS.000000000")] ALIGN1;*/
164#define buf bb_common_bufsiz1 158#define buf bb_common_bufsiz1
165 setup_common_bufsiz(); 159 setup_common_bufsiz();
166 strcpy(strftime_YYYYMMDDHHMMSS(buf, COMMON_BUFSIZE, &t), ".000000000"); 160
161 sprintf(stpcpy(fmt, "%Y-%m-%d %H:%M:%S"), ".%09u %%z", (unsigned)ts->tv_nsec);
162 strftime(buf, COMMON_BUFSIZE, fmt, localtime(&ts->tv_sec));
167 return buf; 163 return buf;
168#undef buf 164#undef buf
169} 165}
@@ -384,19 +380,19 @@ static void FAST_FUNC print_stat(char *pformat, const char m,
384 strcat(pformat, "lu"); 380 strcat(pformat, "lu");
385 printf(pformat, (unsigned long) statbuf->st_blksize); 381 printf(pformat, (unsigned long) statbuf->st_blksize);
386 } else if (m == 'x') { 382 } else if (m == 'x') {
387 printfs(pformat, human_time(statbuf->st_atime)); 383 printfs(pformat, human_time(&statbuf->st_atim));
388 } else if (m == 'X') { 384 } else if (m == 'X') {
389 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu"); 385 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
390 /* note: (unsigned long) would be wrong: 386 /* note: (unsigned long) would be wrong:
391 * imagine (unsigned long64)int32 */ 387 * imagine (unsigned long64)int32 */
392 printf(pformat, (long) statbuf->st_atime); 388 printf(pformat, (long) statbuf->st_atime);
393 } else if (m == 'y') { 389 } else if (m == 'y') {
394 printfs(pformat, human_time(statbuf->st_mtime)); 390 printfs(pformat, human_time(&statbuf->st_mtim));
395 } else if (m == 'Y') { 391 } else if (m == 'Y') {
396 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu"); 392 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
397 printf(pformat, (long) statbuf->st_mtime); 393 printf(pformat, (long) statbuf->st_mtime);
398 } else if (m == 'z') { 394 } else if (m == 'z') {
399 printfs(pformat, human_time(statbuf->st_ctime)); 395 printfs(pformat, human_time(&statbuf->st_ctim));
400 } else if (m == 'Z') { 396 } else if (m == 'Z') {
401 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu"); 397 strcat(pformat, TYPE_SIGNED(time_t) ? "ld" : "lu");
402 printf(pformat, (long) statbuf->st_ctime); 398 printf(pformat, (long) statbuf->st_ctime);
@@ -501,7 +497,7 @@ static bool do_statfs(const char *filename, const char *format)
501 if (format == NULL) { 497 if (format == NULL) {
502# if !ENABLE_SELINUX 498# if !ENABLE_SELINUX
503 format = (option_mask32 & OPT_TERSE 499 format = (option_mask32 & OPT_TERSE
504 ? "%n %i %l %t %s %b %f %a %c %d\n" 500 ? "%n %i %l %t %s %b %f %a %c %d"
505 : " File: \"%n\"\n" 501 : " File: \"%n\"\n"
506 " ID: %-8i Namelen: %-7l Type: %T\n" 502 " ID: %-8i Namelen: %-7l Type: %T\n"
507 "Block size: %-10s\n" 503 "Block size: %-10s\n"
@@ -509,25 +505,26 @@ static bool do_statfs(const char *filename, const char *format)
509 "Inodes: Total: %-10c Free: %d"); 505 "Inodes: Total: %-10c Free: %d");
510# else 506# else
511 format = (option_mask32 & OPT_TERSE 507 format = (option_mask32 & OPT_TERSE
512 ? (option_mask32 & OPT_SELINUX ? "%n %i %l %t %s %b %f %a %c %d %C\n": 508 ? (option_mask32 & OPT_SELINUX
513 "%n %i %l %t %s %b %f %a %c %d\n") 509 ? "%n %i %l %t %s %b %f %a %c %d %C"
514 : (option_mask32 & OPT_SELINUX ? 510 : "%n %i %l %t %s %b %f %a %c %d")
515 " File: \"%n\"\n" 511 : (option_mask32 & OPT_SELINUX
516 " ID: %-8i Namelen: %-7l Type: %T\n" 512 ? " File: \"%n\"\n"
517 "Block size: %-10s\n" 513 " ID: %-8i Namelen: %-7l Type: %T\n"
518 "Blocks: Total: %-10b Free: %-10f Available: %a\n" 514 "Block size: %-10s\n"
519 "Inodes: Total: %-10c Free: %d" 515 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
520 " S_context: %C\n": 516 "Inodes: Total: %-10c Free: %d"
521 " File: \"%n\"\n" 517 " S_context: %C"
522 " ID: %-8i Namelen: %-7l Type: %T\n" 518 : " File: \"%n\"\n"
523 "Block size: %-10s\n" 519 " ID: %-8i Namelen: %-7l Type: %T\n"
524 "Blocks: Total: %-10b Free: %-10f Available: %a\n" 520 "Block size: %-10s\n"
525 "Inodes: Total: %-10c Free: %d\n") 521 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
522 "Inodes: Total: %-10c Free: %d")
526 ); 523 );
527# endif /* SELINUX */ 524# endif /* SELINUX */
528 } 525 }
529 print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext)); 526 print_it(format, filename, print_statfs, &statfsbuf IF_SELINUX(, scontext));
530#else /* FEATURE_STAT_FORMAT */ 527#else /* !FEATURE_STAT_FORMAT */
531 format = (option_mask32 & OPT_TERSE 528 format = (option_mask32 & OPT_TERSE
532 ? "%s %llx %lu " 529 ? "%s %llx %lu "
533 : " File: \"%s\"\n" 530 : " File: \"%s\"\n"
@@ -624,14 +621,14 @@ static bool do_stat(const char *filename, const char *format)
624 "Device: %Dh/%dd\tInode: %-10i Links: %-5h" 621 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
625 " Device type: %t,%T\n" 622 " Device type: %t,%T\n"
626 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n" 623 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
627 "Access: %x\n" "Modify: %y\n" "Change: %z\n"; 624 "Access: %x\n" "Modify: %y\n" "Change: %z";
628 } else { 625 } else {
629 format = 626 format =
630 " File: %N\n" 627 " File: %N\n"
631 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n" 628 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
632 "Device: %Dh/%dd\tInode: %-10i Links: %h\n" 629 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
633 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n" 630 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
634 "Access: %x\n" "Modify: %y\n" "Change: %z\n"; 631 "Access: %x\n" "Modify: %y\n" "Change: %z";
635 } 632 }
636 } 633 }
637# else 634# else
@@ -650,14 +647,14 @@ static bool do_stat(const char *filename, const char *format)
650 " Device type: %t,%T\n" 647 " Device type: %t,%T\n"
651 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n" 648 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
652 " S_Context: %C\n" 649 " S_Context: %C\n"
653 "Access: %x\n" "Modify: %y\n" "Change: %z\n" 650 "Access: %x\n" "Modify: %y\n" "Change: %z"
654 : 651 :
655 " File: %N\n" 652 " File: %N\n"
656 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n" 653 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
657 "Device: %Dh/%dd\tInode: %-10i Links: %-5h" 654 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
658 " Device type: %t,%T\n" 655 " Device type: %t,%T\n"
659 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n" 656 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
660 "Access: %x\n" "Modify: %y\n" "Change: %z\n" 657 "Access: %x\n" "Modify: %y\n" "Change: %z"
661 ); 658 );
662 } else { 659 } else {
663 format = (option_mask32 & OPT_SELINUX ? 660 format = (option_mask32 & OPT_SELINUX ?
@@ -666,13 +663,13 @@ static bool do_stat(const char *filename, const char *format)
666 "Device: %Dh/%dd\tInode: %-10i Links: %h\n" 663 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
667 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n" 664 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
668 "S_Context: %C\n" 665 "S_Context: %C\n"
669 "Access: %x\n" "Modify: %y\n" "Change: %z\n" 666 "Access: %x\n" "Modify: %y\n" "Change: %z"
670 : 667 :
671 " File: %N\n" 668 " File: %N\n"
672 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n" 669 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
673 "Device: %Dh/%dd\tInode: %-10i Links: %h\n" 670 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
674 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n" 671 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
675 "Access: %x\n" "Modify: %y\n" "Change: %z\n" 672 "Access: %x\n" "Modify: %y\n" "Change: %z"
676 ); 673 );
677 } 674 }
678 } 675 }
@@ -749,9 +746,9 @@ static bool do_stat(const char *filename, const char *format)
749 if (option_mask32 & OPT_SELINUX) 746 if (option_mask32 & OPT_SELINUX)
750 printf(" S_Context: %s\n", scontext); 747 printf(" S_Context: %s\n", scontext);
751# endif 748# endif
752 printf("Access: %s\n", human_time(statbuf.st_atime)); 749 printf("Access: %s\n", human_time(&statbuf.st_atim));
753 printf("Modify: %s\n", human_time(statbuf.st_mtime)); 750 printf("Modify: %s\n", human_time(&statbuf.st_mtim));
754 printf("Change: %s\n", human_time(statbuf.st_ctime)); 751 printf("Change: %s\n", human_time(&statbuf.st_ctim));
755 } 752 }
756#endif /* FEATURE_STAT_FORMAT */ 753#endif /* FEATURE_STAT_FORMAT */
757 return 1; 754 return 1;