f45f8cb |
Jamozed |
2020-07-09 22:14:59 |
0
|
// sleep.c, version 1.0.2 |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
1
|
// OMKOV coreutils implementation of POSIX sleep |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
2
|
// Copyright (C) 2020, Jakob Wakeling |
e2140ec |
Jamozed |
2022-03-06 15:27:45 |
3
|
// MIT Licence |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
4
|
|
b181413 |
Jamozed |
2022-02-05 22:32:00 |
5
|
#include "util/error.h" |
b181413 |
Jamozed |
2022-02-05 22:32:00 |
6
|
#include "util/optget.h" |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
7
|
|
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
8
|
#include <unistd.h> |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
9
|
|
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
10
|
#include <stdio.h> |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
11
|
#include <stdint.h> |
911e33a |
Jamozed |
2020-07-06 23:28:09 |
12
|
|
f45f8cb |
Jamozed |
2020-07-09 22:14:59 |
13
|
#define VERSION "1.0.2" |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
14
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
15
|
static struct lop lops[] = { |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
16
|
{ "help", ARG_NUL, 256 }, |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
17
|
{ "version", ARG_NUL, 257 }, |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
18
|
{ NULL, 0, 0 } |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
19
|
}; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
20
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
21
|
static void hlp(void); |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
22
|
static void ver(void); |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
23
|
|
911e33a |
Jamozed |
2020-07-06 23:28:09 |
24
|
int main(int ac, char *av[]) { A0 = av[0]; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
25
|
struct opt opt = OPTGET_INIT; opt.str = ""; opt.lops = lops; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
26
|
for (int o; (o = optget(&opt, av, 1)) != -1;) switch (o) { |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
27
|
case 256: { hlp(); return 0; } |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
28
|
case 257: { ver(); return 0; } |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
29
|
default: { return 1; } |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
30
|
} |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
31
|
|
911e33a |
Jamozed |
2020-07-06 23:28:09 |
32
|
if (opt.ind == ac) { error(1, "missing operand"); } |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
33
|
|
f45f8cb |
Jamozed |
2020-07-09 22:14:59 |
34
|
register unsigned n = 0, d; register char *p = av[1]; |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
35
|
for (; *p >= '0' && *p <= '9'; ++p) { |
f45f8cb |
Jamozed |
2020-07-09 22:14:59 |
36
|
d = (unsigned)*p - '0'; |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
37
|
if (n > (UINTMAX_MAX - d) / 10) { break; } |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
38
|
n = n * 10 + d; |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
39
|
} |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
40
|
|
911e33a |
Jamozed |
2020-07-06 23:28:09 |
41
|
if (*p) { error(1, "%s: invalid time interval", av[1]); } |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
42
|
|
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
43
|
return (int)sleep(n); |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
44
|
} |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
45
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
46
|
static void hlp(void) { |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
47
|
puts("sleep - suspend execution for an interval\n"); |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
48
|
puts("usage: sleep time\n"); |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
49
|
puts("options:"); |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
50
|
puts(" --help Display help information"); |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
51
|
puts(" --version Display version information"); |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
52
|
} |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
53
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
54
|
static void ver(void) { |
911e33a |
Jamozed |
2020-07-06 23:28:09 |
55
|
puts("OMKOV coreutils sleep, version " VERSION); |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
56
|
puts("Copyright (C) 2020, Jakob Wakeling"); |
e2140ec |
Jamozed |
2022-03-06 15:27:45 |
57
|
puts("MIT Licence (https://opensource.org/licenses/MIT)"); |
e0fc3a5 |
Jamozed |
2020-06-26 23:26:39 |
58
|
} |
|
|
|
59
|
|