master xplshn/aruu / cmd / posix / nice.c
 1/* See LICENSE file for copyright and license details. */
 2
 3
 4#include <sys/resource.h>
 5
 6#include <errno.h>
 7#include <stdlib.h>
 8#include <unistd.h>
 9
10#include "util.h"
11
12#ifndef PRIO_MIN
13#define PRIO_MIN -NZERO
14#endif
15
16#ifndef PRIO_MAX
17#define PRIO_MAX (NZERO-1)
18#endif
19
20static void
21usage(void)
22{
23	eprintf("usage: %s [-n inc] cmd [arg ...]\n", argv0);
24}
25
26// ?man nice: run command with modified priority
27// ?man arguments: cmd [arg ...
28// ?man run a command with modified scheduling priority
29int
30main(int argc, char *argv[])
31{
32	int val = 10, r, savederrno;
33
34	ARGBEGIN {
35	// ?man -n:num: print line numbers or counts
36	case 'n':
37		val = estrtonum(EARGF(usage()), PRIO_MIN, PRIO_MAX);
38		break;
39	default:
40		usage();
41		break;
42	} ARGEND
43
44	if (!argc)
45		usage();
46
47	errno = 0;
48	r = getpriority(PRIO_PROCESS, 0);
49	if (errno)
50		weprintf("getpriority:");
51	else
52		val += r;
53	LIMIT(val, PRIO_MIN, PRIO_MAX);
54	if (setpriority(PRIO_PROCESS, 0, val) < 0)
55		weprintf("setpriority:");
56
57	execvp(argv[0], argv);
58	savederrno = errno;
59	weprintf("execvp %s:", argv[0]);
60
61	_exit(126 + (savederrno == ENOENT));
62}