Author | Jamozed <[email protected]> |
Date | 2020-06-26 12:54:31 |
Commit | a6ffac50f36b372d8c88ab9b1ce6d07b5bc33f24 |
Parent | 35a6103176443f6447f0186b7de860b1f9ff35a0 |
nice: Add POSIX nice
Diffstat
M | CMakeLists.txt | | | 1 | + |
M | README.md | | | 1 | + |
A | man/nice.1 | | | 49 | +++++++++++++++++++++++++++++++++++++++++++++++++ |
A | src/nice.c | | | 104 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
4 files changed, 155 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 217d169..673c463 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -18,6 +18,7 @@ ADD_EXECUTABLE(false ${CMAKE_SOURCE_DIR}/src/false.c) ADD_EXECUTABLE(link ${CMAKE_SOURCE_DIR}/src/link.c) ADD_EXECUTABLE(logname ${CMAKE_SOURCE_DIR}/src/logname.c) ADD_EXECUTABLE(mkdir ${CMAKE_SOURCE_DIR}/src/mkdir.c) +ADD_EXECUTABLE(nice ${CMAKE_SOURCE_DIR}/src/nice.c) ADD_EXECUTABLE(pwd ${CMAKE_SOURCE_DIR}/src/pwd.c) ADD_EXECUTABLE(rmdir ${CMAKE_SOURCE_DIR}/src/rmdir.c) ADD_EXECUTABLE(sleep ${CMAKE_SOURCE_DIR}/src/sleep.c) diff --git a/README.md b/README.md index a29971b..03d2bcb 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ UNIX-like systems. | link | Create a link to a file | POSIX | | logname | Return the user's login name | POSIX | | mkdir | Make directories | POSIX | +| nice | Execute with an altered nice value | POSIX | | pwd | Print working directory | POSIX | | rmdir | Remove directories | POSIX | | sleep | Suspend execution for an interval | POSIX | diff --git a/man/nice.1 b/man/nice.1 new file mode 100644 index 0000000..17ac50c --- /dev/null +++ b/man/nice.1 @@ -0,0 +1,49 @@ +.TH NICE 1 2020-06-27 "OMKOV coreutils" "General Commands Manual" +.SH NAME +nice \(em execute with an altered nice value +.SH SYNOPSYS +\fBnice\fR [-n \fIincrement\fR] [\fIcommand\fR [\fIargument\fR...]] +.SH DESCRIPTION +Execute \fIcommand\fR with the specified nice \fIincrement\fR value. If no +\fIincrement\fR is given, it will default to 10. If no \fIcommand\fR is given, +the current nice value will be output. +.SH OPTIONS +The following options are supported: +.TP +.B -n \fIincrement\fR +Nice increment value. +.TP +.B --help +Display help message. +.TP +.B --version +Display version message. +.SH OPERANDS +The following operands are supported: +.TP +.I command +A command to execute with the altered nice value. +.TP +.I argument +An argument to pass to \fIcommand\fR. +.SH EXIT STATUS +Upon successful completion, the exit status of nice shall be the exit status of +\fIcommand\fR. Otherwise, the following exit values will be returned: +.TP +1-125 +An error occurred. +.TP +\ \ 126 +\fIcommand\fR was found but could not be invoked. +.TP +\ \ 127 +\fIcommand\fR was not found. +.SH STANDARDS +The \fInice\fR utility is compliant with the IEEE Std 1003.2-1992 ("POSIX.2") +specification. +.SH COPYRIGHT +.nf +Copyright (C) 2020, Jakob Wakeling +All rights reserved. +OMKOV Permissive Licence (https://www.omkov.net/OLPE) +.fi diff --git a/src/nice.c b/src/nice.c new file mode 100644 index 0000000..f5b98d4 --- /dev/null +++ b/src/nice.c @@ -0,0 +1,104 @@ +// nice.c, version 1.0.0 +// OMKOV coreutils implementation of POSIX nice +// Copyright (C) 2020, Jakob Wakeling +// All rights reserved. + +/* +OMKOV Permissive Licence, version 1.0 + +Copyright (C) 2020, Jakob Wakeling +All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal with +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimers. +* Redistributions in binary form must reproduce the above copyright notice, this + list of conditions and the following disclaimers in the documentation and/or + other materials provided with the distribution. +* Neither the names of the copyright holders, nor the names of its contributors + may be used to endorse or promote products derived from this Software without + specific prior written permission. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT +HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. +*/ + +#include "error.h" +#include "optget.h" + +#include <unistd.h> + +#include <errno.h> +#include <limits.h> +#include <stdbool.h> +#include <stdio.h> +#include <string.h> + +static int n = 10; + +static inline void help(void); +static inline void version(void); + +int main(int argc, char *argv[]) { + lop_t lops[] = { + { "help", ARG_NUL, 256 }, + { "version", ARG_NUL, 257 }, + { NULL, 0, 0 } + }; + + opt_t opt = OPTGET_INIT; opt.str = "n:"; opt.lops = lops; int o; + while ((o = optget(&opt, argv, 0)) != -1) switch (o) { + case 'n': { + register int d; register char *p = opt.arg; bool neg = false; + if (*p == '-') { neg = true; ++p; } else if (*p == '+') { ++p; } + for (n = 0; *p >= '0' && *p <= '9'; ++p) { + d = (int)*p - '0'; + if (n > (INT_MAX - d) / 10) { break; } + n = n * 10 + d; + } + + if (*p) { error(1, "%s: %s: invalid nice value", argv[0], opt.arg); } + if (neg) { n = -n; } break; + } + case 256: { help(); return 0; } + case 257: { version(); return 0; } + default: { return 1; } + } + + if (argc == 1) { printf("%d\n", nice(0)); return 0; } + else if (opt.ind == argc) { error(1, "%s: missing operand\n", argv[0]); } + + errno = 0; nice(n); if (errno) { warn("%s: %s", argv[0], serrno); } + execvp(argv[opt.ind], &argv[opt.ind]); + + warn("%s: %s: %s", argv[0], argv[opt.ind], serrno); + return errno = ENOENT ? 127 : 126; +} + +static inline void help(void) { + puts("nice - execute 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"); + return; +} + +static inline void version() { + puts("OMKOV coreutils nice, version 1.0.0"); + puts("Copyright (C) 2020, Jakob Wakeling"); + puts("All rights reserved."); + puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)"); + return; +}