// nice.c, version 1.0.2 // OMKOV coreutils implementation of POSIX nice // Copyright (C) 2020, Jakob Wakeling // MIT Licence #include "util/error.h" #include "util/optget.h" #include #include #include #include #include #define VERSION "1.0.2" static struct lop lops[] = { { "help", ARG_NUL, 256 }, { "version", ARG_NUL, 257 }, { NULL, 0, 0 } }; static int n = 10; static void hlp(void); static void ver(void); int main(int ac, char *av[]) { A0 = av[0]; struct opt opt = OPTGET_INIT; opt.str = "n:"; opt.lops = lops; for (int o; (o = optget(&opt, av, 0)) != -1;) switch (o) { case 'n': { register char *s = opt.arg; register int c; bool neg = false; if (*s == '+') { ++s; } else if (*s == '-') { ++s; neg = true; } for (n = 0; *s >= '0' && *s <= '9'; ++s) { c = *s - '0'; if (n > (INT_MAX - c) / 10) { break; } n = n * 10 + c; } if (*s) { error(1, "%s: invalid nice value", opt.arg); } if (neg) { n = -n; } break; } case 256: { hlp(); return 0; } case 257: { ver(); return 0; } default: { return 1; } } if (ac == 1) { printf("%d\n", nice(0)); return 0; } else if (opt.ind == ac) { error(1, "missing operand"); } errno = 0; nice(n); if (errno) { warn("%s", serr()); } execvp(av[opt.ind], &av[opt.ind]); warn("%s: %s", av[opt.ind], serr()); return errno = ENOENT ? 127 : 126; } /* Print help information */ static void hlp(void) { puts("nice - invoke with an altered nice value\n"); puts("usage: nice [-n increment] [command [argument...]]\n"); puts("options:"); puts(" -n increment Nice increment value"); puts(" --help Display help information"); puts(" --version Display version information"); } /* Print version information */ static void ver(void) { puts("OMKOV coreutils nice, version " VERSION); puts("Copyright (C) 2020, Jakob Wakeling"); puts("MIT Licence (https://opensource.org/licenses/MIT)"); }