nohup nice renice

This commit is contained in:
Your Name 2023-11-26 20:06:39 +03:00
parent c51f739bea
commit 62a96704a3
8 changed files with 159 additions and 8 deletions

3
TODO
View File

@ -5,9 +5,6 @@ tail
expr
uniq
od
nice
renice
nohup
split
date
tr

View File

@ -33,10 +33,9 @@ void print(const char *file, FILE *fp, long lines, long bytes) {
if (c == '\n')
lcount++;
putchar(c);
if (c == EOF || lcount == lines || (c_flag && bcount == bytes))
break;
putchar(c);
}
}

43
coreutils/nice.c Normal file
View File

@ -0,0 +1,43 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/resource.h>
int main(int argc, char **argv) {
int oldp = getpriority(PRIO_PROCESS, 0);
int adj = 10;
int opt;
while ((opt = getopt(argc, argv, "n:")) != -1) {
switch (opt) {
case 'n':
adj = atoi(optarg);
break;
default:
printf("nice [cmd] [arg1] [arg2]\n\t[-n N Add N to the niceness]\n");
return 0;
}
}
argv += optind;
argc -= optind;
if (argc == 0) {
printf("%d\n", oldp);
return 0;
}
if (setpriority(PRIO_PROCESS, 0, oldp + adj) < 0) {
fprintf(stderr, "nice: %s\n", strerror(errno));
return 1;
}
execvp(argv[0], argv);
fprintf(stderr, "nice: %s: %s\n", argv[0], strerror(errno));
return 1;
}

39
coreutils/nohup.c Normal file
View File

@ -0,0 +1,39 @@
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include "unused.h"
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "nohup: missing operand\n");
return 1;
}
signal(SIGHUP, SIG_IGN);
if (isatty(STDOUT_FILENO)) {
int fd = open("nohup.out", O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
if (fd < 0) {
fprintf(stderr, "nohup: nohup.out: %s\n", strerror(errno));
return 1;
}
if (dup2(fd, STDOUT_FILENO) < 0)
fprintf(stderr, "nohup: %s\n", strerror(errno));
close(fd);
}
if (isatty(STDERR_FILENO))
if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
fprintf(stderr, "nohup: %s\n", strerror(errno));
argv++;
execvp(argv[0], argv);
fprintf(stderr, "nohup: %s: %s\n", argv[0], strerror(errno));
return 1;
}

73
coreutils/renice.c Normal file
View File

@ -0,0 +1,73 @@
#include <pwd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/resource.h>
int renice(int which, int who, int adj) {
adj += getpriority(which, who);
if (setpriority(which, who, adj)) {
fprintf(stderr, "renice: %s\n", strerror(errno));
return 1;
}
return 0;
}
int main(int argc, char **argv) {
int adj = 10;
int which = PRIO_PROCESS;
int opt;
while ((opt = getopt(argc, argv, "n:gup")) != -1) {
switch (opt) {
case 'n':
adj = atoi(optarg);
break;
case 'g':
which = PRIO_PGRP;
break;
case 'u':
which = PRIO_USER;
break;
case 'p':
which = PRIO_PROCESS;
break;
default:
printf("renice [id1 id2]\n\t[-n N Add N to the niceness]\n\t[-g Process group ids] [-u Process user names]\n\t[-p Process ids]\n");
return 0;
}
}
argv += optind;
argc -= optind;
int who = 0;
int ret = 0;
for (int i = 0; i < argc; i++) {
if (which == PRIO_USER) {
struct passwd *pw = getpwnam(argv[i]);
if (pw == 0) {
fprintf(stderr, "renice: %s\n", strerror(errno));
return 1;
}
who = pw->pw_uid;
}
else
who = atoi(argv[i]);
if (renice(which, who, adj))
ret = 1;
}
return ret;
}

View File

@ -28,7 +28,7 @@ int mu_get_lstat(const char *prog_name, const char *path, struct stat *stat_path
return 0;
}
int mu_get_stats(const char *prog_name, int flag, const char *path, struct stat *stat_path) {
int mu_get_stats(const char *restrict prog_name, int flag, const char *restrict path, struct stat *restrict stat_path) {
if (flag)
return mu_get_lstat(prog_name, path, stat_path);

View File

@ -5,7 +5,7 @@
#include <string.h>
#include <stdlib.h>
char *mu_make_path(const char *prog_name, const char *src, const char *dst) {
char *mu_make_path(const char *restrict prog_name, const char *restrict src, const char *restrict dst) {
size_t len = strlen(src) + strlen(dst) + 2;
char *full_path = malloc(len + 1);
if (full_path == NULL) {

View File

@ -7,7 +7,7 @@
#include "make_path.h"
#include "get_stat.h"
int mu_recurse(const char *prog_name, int link_flag, const char *path, void *arg, int (*file_act)(const char *path, void *p), int (*dir_act)(const char *path, void *p)) {
int mu_recurse(const char *restrict prog_name, int link_flag, const char *restrict path, void *restrict arg, int (*file_act)(const char *path, void *p), int (*dir_act)(const char *path, void *p)) {
struct stat sb;
if (mu_get_stats(prog_name, link_flag, path, &sb))
return 1;