-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc/cmds] Fix time command hour wrapping problem
- Loading branch information
Showing
5 changed files
with
48 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,72 @@ | ||
/* time command - modified from BSD 4.2 by Greg Haerr*/ | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
#include <signal.h> | ||
#include <sys/types.h> | ||
#include <sys/time.h> | ||
#include <sys/times.h> | ||
#include <sys/wait.h> | ||
|
||
static void printt(char * s, long us) | ||
/* return current time in microseconds */ | ||
clock_t time_usecs(void) | ||
{ | ||
struct timeval tv; | ||
|
||
if (gettimeofday(&tv, (void *)0) < 0) | ||
return 0; | ||
|
||
return (tv.tv_sec * 1000000LL + tv.tv_usec); | ||
} | ||
|
||
static void print_time(char * s, clock_t us) | ||
{ | ||
long mins, secs; | ||
|
||
if (us < 1000L && us > 499L) /* round up to 1/1000 second*/ | ||
us = 1000L; | ||
mins = us / 60000000L; | ||
if (us < 1000 && us > 499) /* round up to 1/1000 second*/ | ||
us = 1000; | ||
mins = us / 60000000LL; | ||
if (mins) | ||
us -= mins * 60000000L; | ||
us -= mins * 60000000LL; | ||
|
||
secs = us / 1000000L; | ||
if (secs) | ||
us -= secs * 1000000L; | ||
us -= secs * 1000000LL; | ||
|
||
fprintf(stderr, "%s\t%lum%lu.%03lus\n", s, mins, secs, us/1000); | ||
fprintf(stderr, "%s\t%lum%lu.%03lus\n", s, mins, secs, (long)us/1000); | ||
|
||
} | ||
|
||
|
||
int main(int argc, char **argv) | ||
{ | ||
int status, p; | ||
struct tms end, start; | ||
clock_t start, end; | ||
|
||
if(argc <= 1) { | ||
fprintf(stderr, "Usage: time <program ...>\n"); | ||
exit(0); | ||
return 0; | ||
} | ||
|
||
times(&start); | ||
start = time_usecs(); | ||
p = fork(); | ||
if(p == -1) { | ||
fprintf(stderr, "Try again.\n"); | ||
exit(1); | ||
perror(NULL); | ||
return 1; | ||
} | ||
if(p == 0) { | ||
execvp(argv[1], &argv[1]); | ||
perror(argv[1]); | ||
exit(1); | ||
return 1; | ||
} | ||
signal(SIGINT, SIG_IGN); | ||
signal(SIGQUIT, SIG_IGN); | ||
while (waitpid(p, &status, 0) != p) | ||
continue; | ||
if((status&0377) != 0) | ||
fprintf(stderr,"Command terminated abnormally.\n"); | ||
times(&end); | ||
end = time_usecs(); | ||
fprintf(stderr, "\n"); | ||
printt("Real", end.tms_cstime - start.tms_cstime); | ||
print_time("Real", end - start); | ||
exit(status>>8); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,10 +12,6 @@ struct tms { | |
clock_t tms_cstime; | ||
}; | ||
|
||
__BEGIN_DECLS | ||
|
||
clock_t times (struct tms *tp); | ||
|
||
__END_DECLS | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters