[hackers] [sbase] Audit time(1) || FRIGN

From: <git_AT_suckless.org>
Date: Tue, 24 Mar 2015 23:54:08 +0100 (CET)

commit 1250a8962f2fe1648ce03988cdc14395712d2189
Author: FRIGN <dev_AT_frign.de>
Date: Thu Mar 5 00:23:09 2015 +0100

    Audit time(1)
    
    1) fix usage().
    2) sort includes and comment properly. rename rbeg and rend to r0 and r1.
    3) argc style and usage fixes.
    4) make error-messages clearer.
    5) BUGFIX: It was ignored when fork() failed.
    6) Don't call enprintf() after execvp and use _exit instead.

diff --git a/time.c b/time.c
index e4be7b4..fc17bf0 100644
--- a/time.c
+++ b/time.c
_AT_@ -11,17 +11,17 @@
 static void
 usage(void)
 {
- eprintf("usage: %s [-p] utility [argument ...]\n", argv0);
+ eprintf("usage: %s [-p] cmd [arg ...]\n", argv0);
 }
 
 int
 main(int argc, char *argv[])
 {
         pid_t pid;
- struct tms tms; /* hold user and sys times */
- clock_t rbeg, rend; /* real time */
- long ticks; /* per second */
- int status;
+ struct tms tms; /* user and sys times */
+ clock_t r0, r1; /* real time */
+ long ticks; /* per second */
+ int status, savederrno;
 
         ARGBEGIN {
         case 'p':
_AT_@ -30,26 +30,33 @@ main(int argc, char *argv[])
                 usage();
         } ARGEND;
 
- if (!*argv)
+ if (!argc)
                 usage();
 
         if ((ticks = sysconf(_SC_CLK_TCK)) <= 0)
- eprintf("sysconf() failed to retrieve clock ticks per second\n");
+ eprintf("sysconf _SC_CLK_TCK:");
 
- if ((rbeg = times(&tms)) < 0)
- eprintf("times() failed to retrieve start times:");
+ if ((r0 = times(&tms)) < 0)
+ eprintf("times:");
 
- if (!(pid = fork())) { /* child */
- execvp(*argv, argv);
- enprintf(errno == ENOENT ? 127 : 126, "failed to exec %s:", *argv);
+ switch ((pid = fork())) {
+ case -1:
+ eprintf("fork:");
+ case 0:
+ execvp(argv[0], argv);
+ savederrno = errno;
+ weprintf("exec %s:", argv[0]);
+ _exit(126 + (savederrno == ENOENT));
+ default:
+ break;
         }
         waitpid(pid, &status, 0);
 
- if ((rend = times(&tms)) < 0)
- eprintf("times() failed to retrieve end times:");
+ if ((r1 = times(&tms)) < 0)
+ eprintf("times:");
 
         fprintf(stderr, "real %f\nuser %f\nsys %f\n",
- (rend - rbeg) / (double)ticks,
+ (r1 - r0) / (double)ticks,
                 tms.tms_cutime / (double)ticks,
                 tms.tms_cstime / (double)ticks);
 
Received on Tue Mar 24 2015 - 23:54:08 CET

This archive was generated by hypermail 2.3.0 : Wed Mar 25 2015 - 00:11:22 CET