5cf73ae |
Jamozed |
2020-11-17 13:11:49 |
0
|
// nice.c, version 1.0.2 |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
1
|
// OMKOV coreutils implementation of POSIX nice |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
2
|
// Copyright (C) 2020, Jakob Wakeling |
e2140ec |
Jamozed |
2022-03-06 15:27:45 |
3
|
// MIT Licence |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
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" |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
7
|
|
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
8
|
#include <unistd.h> |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
9
|
|
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
10
|
#include <errno.h> |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
11
|
#include <limits.h> |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
12
|
#include <stdbool.h> |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
13
|
#include <stdio.h> |
2e3f630 |
Jamozed |
2020-07-06 23:04:27 |
14
|
|
5cf73ae |
Jamozed |
2020-11-17 13:11:49 |
15
|
#define VERSION "1.0.2" |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
16
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
17
|
static struct lop lops[] = { |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
18
|
{ "help", ARG_NUL, 256 }, |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
19
|
{ "version", ARG_NUL, 257 }, |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
20
|
{ NULL, 0, 0 } |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
21
|
}; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
22
|
|
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
23
|
static int n = 10; |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
24
|
|
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
25
|
static void hlp(void); |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
26
|
static void ver(void); |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
27
|
|
2e3f630 |
Jamozed |
2020-07-06 23:04:27 |
28
|
int main(int ac, char *av[]) { A0 = av[0]; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
29
|
struct opt opt = OPTGET_INIT; opt.str = "n:"; opt.lops = lops; |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
30
|
for (int o; (o = optget(&opt, av, 0)) != -1;) switch (o) { |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
31
|
case 'n': { |
a3ab15f |
Jamozed |
2021-02-22 13:01:57 |
32
|
register char *s = opt.arg; register int c; bool neg = false; |
a3ab15f |
Jamozed |
2021-02-22 13:01:57 |
33
|
if (*s == '+') { ++s; } else if (*s == '-') { ++s; neg = true; } |
a3ab15f |
Jamozed |
2021-02-22 13:01:57 |
34
|
for (n = 0; *s >= '0' && *s <= '9'; ++s) { c = *s - '0'; |
a3ab15f |
Jamozed |
2021-02-22 13:01:57 |
35
|
if (n > (INT_MAX - c) / 10) { break; } n = n * 10 + c; |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
36
|
} |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
37
|
|
a3ab15f |
Jamozed |
2021-02-22 13:01:57 |
38
|
if (*s) { error(1, "%s: invalid nice value", opt.arg); } |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
39
|
if (neg) { n = -n; } break; |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
40
|
} |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
41
|
case 256: { hlp(); return 0; } |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
42
|
case 257: { ver(); return 0; } |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
43
|
default: { return 1; } |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
44
|
} |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
45
|
|
2e3f630 |
Jamozed |
2020-07-06 23:04:27 |
46
|
if (ac == 1) { printf("%d\n", nice(0)); return 0; } |
a3ab15f |
Jamozed |
2021-02-22 13:01:57 |
47
|
else if (opt.ind == ac) { error(1, "missing operand"); } |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
48
|
|
2e3f630 |
Jamozed |
2020-07-06 23:04:27 |
49
|
errno = 0; nice(n); if (errno) { warn("%s", serr()); } |
2e3f630 |
Jamozed |
2020-07-06 23:04:27 |
50
|
execvp(av[opt.ind], &av[opt.ind]); |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
51
|
|
2e3f630 |
Jamozed |
2020-07-06 23:04:27 |
52
|
warn("%s: %s", av[opt.ind], serr()); |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
53
|
return errno = ENOENT ? 127 : 126; |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
54
|
} |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
55
|
|
a3ab15f |
Jamozed |
2021-02-22 13:01:57 |
56
|
/* Print help information */ |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
57
|
static void hlp(void) { |
5cf73ae |
Jamozed |
2020-11-17 13:11:49 |
58
|
puts("nice - invoke with an altered nice value\n"); |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
59
|
puts("usage: nice [-n increment] [command [argument...]]\n"); |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
60
|
puts("options:"); |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
61
|
puts(" -n increment Nice increment value"); |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
62
|
puts(" --help Display help information"); |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
63
|
puts(" --version Display version information"); |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
64
|
} |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
65
|
|
a3ab15f |
Jamozed |
2021-02-22 13:01:57 |
66
|
/* Print version information */ |
30cf081 |
Jamozed |
2020-08-28 23:01:02 |
67
|
static void ver(void) { |
2e3f630 |
Jamozed |
2020-07-06 23:04:27 |
68
|
puts("OMKOV coreutils nice, version " VERSION); |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
69
|
puts("Copyright (C) 2020, Jakob Wakeling"); |
e2140ec |
Jamozed |
2022-03-06 15:27:45 |
70
|
puts("MIT Licence (https://opensource.org/licenses/MIT)"); |
a6ffac5 |
Jamozed |
2020-06-27 00:54:31 |
71
|
} |
|
|
|
72
|
|