Skip to content

Instantly share code, notes, and snippets.

@xixitalk
Created September 15, 2016 00:05
Show Gist options
  • Save xixitalk/354a2628bbd21214be5340b0cac0ac52 to your computer and use it in GitHub Desktop.
Save xixitalk/354a2628bbd21214be5340b0cac0ac52 to your computer and use it in GitHub Desktop.
command sleep
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <string.h>
static void Usage(const char *name)
{
printf("%s NUMBER[SUFFIX]...\n\n", name);
printf("The sleep command pauses for an amount of time defined by NUMBER. NUMBER must be an integer. floating-point value is NOT support.\n");
printf("SUFFIX may be \"s\" for seconds (the default), \"m\" for minutes, \"h\" for hours, or \"d\" for days.\n");
printf("If more than one NUMBER is specified, sleep delays for the sum of their values.\n");
}
static unsigned long int calctime(const char *ptime)
{
int i = 0;
unsigned long int timelen = 0;
unsigned long int len = strlen(ptime);
char suffix = 's';
if(len > 1)
{
suffix = ptime[len - 1];
}
timelen = strtoul(ptime, NULL, 10);
switch(suffix)
{
case 's':
break;
case 'm':
timelen = timelen * 60;
break;
case 'h':
timelen = timelen * 60 * 60;
break;
case 'd':
timelen = timelen * 24 * 60 * 60;
break;
default:
break;
}
return timelen;
}
int main(int argc, char *argv[])
{
unsigned long int timelen = 0;
int i = 0;
if(1 == argc)
{
Usage(argv[0]);
return -1;
}
for(i = 1; i < argc; i++)
{
timelen += calctime(argv[i]);
}
if(timelen > 0)
{
sleep(timelen);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment