Improve sleep's input parsing.

master
Mid Favila 2 months ago
parent 8783d7f5fc
commit 9a1a9a27c9
  1. 17
      src/sleep.c

@ -2,7 +2,7 @@
#include <stdio.h>
#include <errno.h>
#include <time.h>
#include <ctype.h>
#include "support.h"
@ -18,15 +18,22 @@ int main(int argc, char **argv)
throw(NEEDARG_FAIL, mastrcat(argv[0], " int"));
}
/* have we received a positive integer? */
/* subtract one from strlen result to account for zero indexing */
for(i = (strlen(argv[1])-1); i >= 0; i--)
{
if(!isdigit(argv[1][i]))
{
throw(NEEDARG_FAIL, mastrcat(argv[0], " int"));
}
}
i = (int) (strtol(argv[1], (char **) NULL, 10));
if(errno == ERANGE)
{
throw(TIME_FAIL, mastrcat(argv[0], " ERANGE error."));
}
if(i < 0)
{
throw(MISC_FAIL, mastrcat(argv[0], ": need positive number."));
}
sleep(i);

Loading…
Cancel
Save