morpheus-base

morpheus base system
git clone git://git.2f30.org/morpheus-base
Log | Files | Refs

nice.c (920B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <errno.h>
      3 #include <limits.h>
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 #include <sys/resource.h>
      7 #include <sys/time.h>
      8 #include <unistd.h>
      9 
     10 #include "util.h"
     11 
     12 static void
     13 usage(void)
     14 {
     15 	eprintf("usage: nice [-n inc] command [options ...]\n");
     16 }
     17 
     18 int
     19 main(int argc, char *argv[])
     20 {
     21 	long val = 10;
     22 	int savederrno;
     23 
     24 	ARGBEGIN {
     25 	case 'n':
     26 		val = estrtol(EARGF(usage()), 10);
     27 		break;
     28 	default:
     29 		usage();
     30 		break;
     31 	} ARGEND;
     32 
     33 	if (argc == 0)
     34 		usage();
     35 
     36 	errno = 0;
     37 	val += getpriority(PRIO_PROCESS, 0);
     38 	if (errno != 0)
     39 		weprintf("getpriority:");
     40 	val = MAX(PRIO_MIN, MIN(val, PRIO_MAX));
     41 	if (setpriority(PRIO_PROCESS, 0, val) != 0)
     42 		weprintf("setpriority:");
     43 
     44 	/* POSIX specifies the nice failure still invokes the command */
     45 	execvp(argv[0], argv);
     46 	savederrno = errno;
     47 	weprintf("execvp %s:", argv[0]);
     48 	return (savederrno == ENOENT)? 127 : 126;
     49 }