xref: /original-bsd/usr.bin/nice/nice.c (revision 7921151c)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)nice.c	5.6 (Berkeley) 02/10/93";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 #include <sys/time.h>
20 #include <sys/resource.h>
21 
22 #include <ctype.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 
29 #define	DEFNICE	10
30 
31 void usage __P((void));
32 
33 int
34 main(argc, argv)
35 	int argc;
36 	char *argv[];
37 {
38 	int niceness;
39 
40 	niceness = DEFNICE;
41 	if (argv[1][0] == '-')
42 		if (isdigit(argv[1][1])) {
43 			niceness = atoi(argv[1] + 1);
44 			++argv;
45 		}
46 		else {
47 			(void)fprintf(stderr, "nice: illegal option -- %c\n",
48 			    argv[1][1]);
49 			usage();
50 		}
51 
52 	if (!argv[1])
53 		usage();
54 
55 	errno = 0;
56 	niceness += getpriority(PRIO_PROCESS, 0);
57 	if (errno) {
58 		(void)fprintf(stderr, "nice: getpriority: %s\n",
59 		    strerror(errno));
60 		exit(1);
61 	}
62 	if (setpriority(PRIO_PROCESS, 0, niceness)) {
63 		(void)fprintf(stderr,
64 		    "nice: setpriority: %s\n", strerror(errno));
65 		exit(1);
66 	}
67 	execvp(argv[1], &argv[1]);
68 	(void)fprintf(stderr,
69 	    "nice: %s: %s\n", argv[1], strerror(errno));
70 	exit(1);
71 }
72 
73 void
74 usage()
75 {
76 	(void)fprintf(stderr,
77 	    "nice [ -# ] command [ options ] [ operands ]\n");
78 	exit(1);
79 }
80