Subject: bin/34044: sleep patch
To: None <gnats-admin@netbsd.org, netbsd-bugs@netbsd.org>
From: None <new.security@gmail.com>
List: netbsd-bugs
Date: 07/20/2006 21:20:04
>Number:         34044
>Category:       bin
>Synopsis:       sleep patch
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Thu Jul 20 21:20:04 +0000 2006
>Originator:     Kevin Massey
>Release:        
>Organization:
none
>Environment:
>Description:
This is a small patch i wrote to give sleep the ability to accept time values in either the minutes or hours value like so "sleep -h 1" would set  sleep for an hour and "sleep -m 2" ould set sleep to 2 minutes.
>How-To-Repeat:

>Fix:
Index: sleep.c
===================================================================
RCS file: /cvsroot/src/bin/sleep/sleep.c,v
retrieving revision 1.20
diff -u -r1.20 sleep.c
--- sleep.c     17 Oct 2005 10:11:46 -0000      1.20
+++ sleep.c     20 Jul 2006 21:07:47 -0000
@@ -63,23 +63,29 @@
        char *arg, *temp;
        double fval, ival, val;
        struct timespec ntime;
-       int ch, fracflag;
+       int ch, fracflag, minute = 0, hour = 0;

        setprogname(argv[0]);
        (void)setlocale(LC_ALL, "");

        (void)signal(SIGALRM, alarmhandle);

-       while ((ch = getopt(argc, argv, "")) != -1)
+       while ((ch = getopt(argc, argv, "mh")) != -1)
                switch(ch) {
+               case 'm':
+                       minute = 1;
+                       break;
+               case 'h':
+                       hour = 1;
+                       break;
                case '?':
                default:
                        usage();
                }
        argc -= optind;
        argv += optind;
-
-       if (argc != 1)
+
+       if(argc < 1)
                usage();

        /*
@@ -95,7 +101,11 @@
         * handled transparently by the atof code.
         */
        fracflag = 0;
-       arg = *argv;
+       if(hour || minute)
+               arg = argv[0];
+       else
+               arg = *argv;
+
        for (temp = arg; *temp != '\0'; temp++)
                if (!isdigit((unsigned char)*temp))
                        fracflag++;
@@ -106,11 +116,32 @@
                        usage();
                ival = floor(val);
                fval = (1000000000 * (val-ival));
+
                ntime.tv_sec = ival;
-               ntime.tv_nsec = fval;
+
+               if (hour) {
+                       ntime.tv_nsec = fval;
+                       ntime.tv_nsec *= 3600;
+               }
+               if (minute) {
+                       ntime.tv_nsec = fval;
+                       ntime.tv_nsec *= 60;
+               } else {
+                       ntime.tv_nsec = fval;
+               }
        }
-       else{
-               ntime.tv_sec = atol(arg);
+       else {
+               if (hour) {
+                       ntime.tv_sec = atol(arg);
+                       ntime.tv_sec *= 3600;
+               }
+               else if (minute) {
+                       ntime.tv_sec = atol(arg);
+                       ntime.tv_sec *= 60;
+               } else {
+                       ntime.tv_sec = atol(arg);
+               }
+
                if (ntime.tv_sec <= 0)
                        exit(0);
                ntime.tv_nsec = 0;
@@ -126,7 +157,7 @@
 void
 usage(void)
 {
-       (void)fprintf(stderr, "usage: %s seconds\n", getprogname());
+       (void)fprintf(stderr, "usage: %s seconds | %s -h hours | %s -m minutes\n", getprogname(), getprogname(), getprogname());
        exit(1);
        /* NOTREACHED */
 }