commit 05429c0040ad1a642daca41782338ea751638764
parent 86b483dfb27ad3bb5e32a55e79e92424d2e9e57b
Author: phoebos <ben@bvnf.space>
Date: Thu, 10 Mar 2022 03:27:03 +0000
nice: add
Diffstat:
4 files changed, 37 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
@@ -9,6 +9,7 @@ BINS = \
head \
ls \
mkdir \
+ nice \
pwd \
rmdir \
sort \
diff --git a/PROGRESS b/PROGRESS
@@ -26,6 +26,7 @@
[ ] mkdir
[ ] more
[ ] mv
+[x] nice [-n]
[ ] nl
[ ] printf
[ ] ps
diff --git a/TODO.posix b/TODO.posix
@@ -70,7 +70,7 @@
[ ] more
[ ] mv
[ ] newgrp
-[ ] nice
+[x] nice
[ ] nl
[ ] nm
[ ] nohup
diff --git a/nice.c b/nice.c
@@ -0,0 +1,34 @@
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+int
+main(int argc, char **argv) {
+ int n = 10;
+ if (argc > 3 && strcmp("-n", argv[1]) == 0) {
+ n = atoi(argv[2]);
+ argv += 2;
+ argc -= 2;
+ }
+
+ if (argc == 1) {
+ fprintf(stderr, "usage: nice [-n increment] utility [args]\n");
+ return 1;
+ }
+
+ errno = 0;
+ if (nice(n) == -1 && errno) {
+ perror("nice");
+ }
+
+ errno = 0;
+ execvp(argv[1], argv + 1);
+
+ if (errno) {
+ perror(argv[1]);
+ return 127;
+ }
+ return 126;
+}