-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsleep.c
51 lines (43 loc) · 1004 Bytes
/
sleep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
/*
Simulates the behavior of the 'sleep' command in Linux
*/
int main(int argc, char *argv[])
{
if (argc != 2)
{
printf("Usage: %s <duration>\n", argv[0]);
printf("Example durations: 10s, 2m, 1h, 1d\n");
return 1;
}
char *timetosleep = argv[1];
int length = strlen(timetosleep);
char suffix = timetosleep[length - 1];
// Remove the suffix for conversion
timetosleep[length - 1] = '\0';
unsigned int value = atoi(timetosleep);
unsigned int seconds = 0;
switch (suffix)
{
case 's':
seconds = value;
break;
case 'm':
seconds = value * 60;
break;
case 'h':
seconds = value * 3600;
break;
case 'd':
seconds = value * 86400;
break;
default:
printf("Unknown time suffix: %c\n", suffix);
return 1;
}
sleep(seconds);
return 0;
}