xref: /original-bsd/usr.bin/nice/nice.c (revision c0f053f7)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)nice.c	5.3 (Berkeley) 05/20/89";
26 #endif /* not lint */
27 
28 #include <sys/time.h>
29 #include <sys/resource.h>
30 #include <stdio.h>
31 #include <ctype.h>
32 
33 #define	DEFNICE	10
34 
35 /* ARGSUSED */
36 main(argc, argv)
37 	int argc;
38 	char **argv;
39 {
40 	extern int errno;
41 	int niceness;
42 	char *strerror();
43 
44 	niceness = DEFNICE;
45 	if (argv[1][0] == '-')
46 		if (isdigit(argv[1][1])) {
47 			niceness = atoi(argv[1] + 1);
48 			++argv;
49 		}
50 		else {
51 			(void)fprintf(stderr, "nice: illegal option -- %c\n",
52 			    argv[1][1]);
53 			usage();
54 		}
55 
56 	if (!argv[1])
57 		usage();
58 
59 	errno = 0;
60 	niceness += getpriority(PRIO_PROCESS, 0);
61 	if (errno) {
62 		(void)fprintf(stderr, "nice: getpriority: %s\n",
63 		    strerror(errno));
64 		exit(1);
65 	}
66 	if (setpriority(PRIO_PROCESS, 0, niceness)) {
67 		(void)fprintf(stderr,
68 		    "nice: setpriority: %s\n", strerror(errno));
69 		exit(1);
70 	}
71 	execvp(argv[1], &argv[1]);
72 	(void)fprintf(stderr,
73 	    "nice: %s: %s\n", argv[1], strerror(errno));
74 	exit(1);
75 }
76 
77 usage()
78 {
79 	(void)fprintf(stderr,
80 	    "nice [ -# ] command [ options ] [ operands ]\n");
81 	exit(1);
82 }
83