xref: /original-bsd/usr.bin/nice/nice.c (revision d272e02a)
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.4 (Berkeley) 06/01/90";
16 #endif /* not lint */
17 
18 #include <sys/time.h>
19 #include <sys/resource.h>
20 #include <stdio.h>
21 #include <ctype.h>
22 
23 #define	DEFNICE	10
24 
25 /* ARGSUSED */
26 main(argc, argv)
27 	int argc;
28 	char **argv;
29 {
30 	extern int errno;
31 	int niceness;
32 	char *strerror();
33 
34 	niceness = DEFNICE;
35 	if (argv[1][0] == '-')
36 		if (isdigit(argv[1][1])) {
37 			niceness = atoi(argv[1] + 1);
38 			++argv;
39 		}
40 		else {
41 			(void)fprintf(stderr, "nice: illegal option -- %c\n",
42 			    argv[1][1]);
43 			usage();
44 		}
45 
46 	if (!argv[1])
47 		usage();
48 
49 	errno = 0;
50 	niceness += getpriority(PRIO_PROCESS, 0);
51 	if (errno) {
52 		(void)fprintf(stderr, "nice: getpriority: %s\n",
53 		    strerror(errno));
54 		exit(1);
55 	}
56 	if (setpriority(PRIO_PROCESS, 0, niceness)) {
57 		(void)fprintf(stderr,
58 		    "nice: setpriority: %s\n", strerror(errno));
59 		exit(1);
60 	}
61 	execvp(argv[1], &argv[1]);
62 	(void)fprintf(stderr,
63 	    "nice: %s: %s\n", argv[1], strerror(errno));
64 	exit(1);
65 }
66 
67 usage()
68 {
69 	(void)fprintf(stderr,
70 	    "nice [ -# ] command [ options ] [ operands ]\n");
71 	exit(1);
72 }
73