Improve sleep's input parsing.

This commit is contained in:
Mid Favila 2022-10-01 18:55:35 -04:00
parent 8783d7f5fc
commit 9a1a9a27c9
1 changed files with 12 additions and 5 deletions

View File

@ -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);