~sircmpwn/ctools

ref: 216800a3be17262645afd196a72ba2feedc3bc9e ctools/src/nice.c -rw-r--r-- 932 bytes
216800a3Drew DeVault nice: no-op on non-XSI systems 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static void
usage(void)
{
	fprintf(stderr, "usage: nice [-n increment] utility [argument...]\n");
}

int
main(int argc, char *argv[])
{
	int increment = 10;

	char opt;
	while ((opt = getopt(argc, argv, "n:")) != -1) {
		switch (opt) {
		case 'n': {
			char *endptr = NULL;
			long value = strtol(optarg, &endptr, 10);
			if (value < INT_MIN || value > INT_MAX
			    		|| *endptr != '\0') {
				fprintf(stderr,
					"nice: invalid increment value '%s'\n",
					optarg);
				return 1;
			}
			break;
		}
		default:
			  usage();
			  return 1;
		}
	}

	if (optind == argc) {
		usage();
		return 1;
	}

#ifdef _XOPEN_SOURCE
	if (nice(increment) == -1) {
		perror("nice");
	}
#else
	fprintf(stderr, "nice: compiled without XSI support; no-op\n");
#endif

	if (execvp(argv[optind], argv + optind) == -1) {
		perror("execvp");
		return 1;
	}

	return 0;
}