~sircmpwn/ctools

bf7328dd1f69a415778e4be33c8c03a763ade1b2 — Adrian Perez de Castro 1 year, 4 months ago 64b95b5
Implement nice
3 files changed, 59 insertions(+), 1 deletions(-)

M STATUS
M meson.build
A src/nice.c
M STATUS => STATUS +1 -1
@@ 88,7 88,7 @@ T       mkfifo
    W   more
T       mv
T       newgrp
T       nice
  D     nice
T       nl
    W   nm
T       nohup

M meson.build => meson.build +5 -0
@@ 24,10 24,15 @@ oneshots = [
	'unlink',
]

xsi_oneshots = [
	'nice',
]

cc = meson.get_compiler('c')

if get_option('xsi')
	add_project_arguments(['-D_XOPEN_SOURCE=700'], language: 'c')
	oneshots += xsi_oneshots
else
	add_project_arguments(['-D_POSIX_C_SOURCE=200809L'], language: 'c')
endif

A src/nice.c => src/nice.c +53 -0
@@ 0,0 1,53 @@
#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;
	}

	if (nice(increment) == -1) {
		perror("nice");
	}

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

	return 0;
}