xref: /csrg-svn/usr.bin/nice/nice.c (revision 57921)
119845Sdist /*
238087Sbostic  * Copyright (c) 1989 The Regents of the University of California.
338087Sbostic  * All rights reserved.
438087Sbostic  *
542755Sbostic  * %sccs.include.redist.c%
619845Sdist  */
719845Sdist 
812669Ssam #ifndef lint
919845Sdist char copyright[] =
1038087Sbostic "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
1119845Sdist  All rights reserved.\n";
1238087Sbostic #endif /* not lint */
131059Sbill 
1419845Sdist #ifndef lint
15*57921Sbostic static char sccsid[] = "@(#)nice.c	5.6 (Berkeley) 02/10/93";
1638087Sbostic #endif /* not lint */
1719845Sdist 
1854037Sbostic #include <sys/types.h>
1912669Ssam #include <sys/time.h>
2012669Ssam #include <sys/resource.h>
2154037Sbostic 
22*57921Sbostic #include <ctype.h>
23*57921Sbostic #include <errno.h>
2438087Sbostic #include <stdio.h>
25*57921Sbostic #include <stdlib.h>
26*57921Sbostic #include <unistd.h>
27*57921Sbostic #include <string.h>
2812669Ssam 
2938087Sbostic #define	DEFNICE	10
3038087Sbostic 
31*57921Sbostic void usage __P((void));
32*57921Sbostic 
33*57921Sbostic int
341059Sbill main(argc, argv)
3512669Ssam 	int argc;
36*57921Sbostic 	char *argv[];
371059Sbill {
3838087Sbostic 	int niceness;
391059Sbill 
4038087Sbostic 	niceness = DEFNICE;
4138087Sbostic 	if (argv[1][0] == '-')
4238087Sbostic 		if (isdigit(argv[1][1])) {
4338087Sbostic 			niceness = atoi(argv[1] + 1);
4438087Sbostic 			++argv;
4538087Sbostic 		}
4638087Sbostic 		else {
4738087Sbostic 			(void)fprintf(stderr, "nice: illegal option -- %c\n",
4838087Sbostic 			    argv[1][1]);
4938087Sbostic 			usage();
5038087Sbostic 		}
5138087Sbostic 
5238087Sbostic 	if (!argv[1])
5338087Sbostic 		usage();
5438087Sbostic 
5538087Sbostic 	errno = 0;
5638087Sbostic 	niceness += getpriority(PRIO_PROCESS, 0);
5738087Sbostic 	if (errno) {
5838087Sbostic 		(void)fprintf(stderr, "nice: getpriority: %s\n",
5938087Sbostic 		    strerror(errno));
601059Sbill 		exit(1);
611059Sbill 	}
6238087Sbostic 	if (setpriority(PRIO_PROCESS, 0, niceness)) {
6338087Sbostic 		(void)fprintf(stderr,
6438087Sbostic 		    "nice: setpriority: %s\n", strerror(errno));
6512669Ssam 		exit(1);
6612669Ssam 	}
671059Sbill 	execvp(argv[1], &argv[1]);
6838087Sbostic 	(void)fprintf(stderr,
6938087Sbostic 	    "nice: %s: %s\n", argv[1], strerror(errno));
701059Sbill 	exit(1);
711059Sbill }
7238087Sbostic 
73*57921Sbostic void
7438087Sbostic usage()
7538087Sbostic {
7638087Sbostic 	(void)fprintf(stderr,
7738087Sbostic 	    "nice [ -# ] command [ options ] [ operands ]\n");
7838087Sbostic 	exit(1);
7938087Sbostic }
80