xref: /netbsd-src/usr.bin/nice/nice.c (revision 98e5374ccbbb6278b5bad9598b53dc8190372d05)
1*98e5374cSlukem /*	$NetBSD: nice.c,v 1.15 2008/07/21 14:19:24 lukem Exp $	*/
262803f02Sjtc 
361f28255Scgd /*
461f28255Scgd  * Copyright (c) 1989 The Regents of the University of California.
561f28255Scgd  * All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
1589aaa1bbSagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32c9af5d97Slukem #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
34*98e5374cSlukem __COPYRIGHT("@(#) Copyright (c) 1989\
35*98e5374cSlukem  The Regents of the University of California.  All rights reserved.");
3661f28255Scgd #endif /* not lint */
3761f28255Scgd 
3861f28255Scgd #ifndef lint
3962803f02Sjtc #if 0
4062803f02Sjtc static char sccsid[] = "@(#)nice.c	5.4 (Berkeley) 6/1/90";
4162803f02Sjtc #endif
42*98e5374cSlukem __RCSID("$NetBSD: nice.c,v 1.15 2008/07/21 14:19:24 lukem Exp $");
4361f28255Scgd #endif /* not lint */
4461f28255Scgd 
4561f28255Scgd #include <sys/time.h>
4661f28255Scgd #include <sys/resource.h>
47c2bd98ebSchristos 
4861f28255Scgd #include <stdio.h>
497b5c7989Sjtc #include <stdlib.h>
50b3617ad5Sjtc #include <string.h>
51c2bd98ebSchristos #include <limits.h>
526b9684b2Sjtc #include <locale.h>
537b5c7989Sjtc #include <ctype.h>
547b5c7989Sjtc #include <errno.h>
557b5c7989Sjtc #include <err.h>
56f7c6bf57Sjtc #include <unistd.h>
5761f28255Scgd 
5861f28255Scgd #define	DEFNICE	10
5961f28255Scgd 
608b0f9554Sperry static void usage(void) __dead;
61f7c6bf57Sjtc 
62f7c6bf57Sjtc int
main(int argc,char ** argv)63c2bd98ebSchristos main(int argc, char **argv)
6461f28255Scgd {
65c2bd98ebSchristos 	char *ep;
667b5c7989Sjtc 	int niceness = DEFNICE;
677b5c7989Sjtc 	int c;
68c2bd98ebSchristos 	long tmp;
69b3617ad5Sjtc 
70c2bd98ebSchristos 	setprogname(argv[0]);
71c2bd98ebSchristos 	(void)setlocale(LC_ALL, "");
726b9684b2Sjtc 
737b5c7989Sjtc         /* handle obsolete -number syntax */
7421366354Schristos         if (argc > 1 && argv[1][0] == '-' &&
7521366354Schristos 	    isdigit((unsigned char)argv[1][1])) {
7661f28255Scgd 		niceness = atoi (argv[1] + 1);
777b5c7989Sjtc                 argc--; argv++;
7861f28255Scgd         }
7961f28255Scgd 
807b5c7989Sjtc 	while ((c = getopt (argc, argv, "n:")) != -1) {
817b5c7989Sjtc 		switch (c) {
827b5c7989Sjtc 		case 'n':
83c2bd98ebSchristos 			errno = 0;
84c2bd98ebSchristos 			tmp = strtol(optarg, &ep, 10);
85c2bd98ebSchristos 			if (*ep != '\0' || tmp < INT_MIN || tmp > INT_MAX)
86c2bd98ebSchristos 				errx(EXIT_FAILURE, "invalid argument: `%s'",
87c2bd98ebSchristos 				    optarg);
88c2bd98ebSchristos 			niceness = (int)tmp;
897b5c7989Sjtc 			break;
907b5c7989Sjtc 		default:
917b5c7989Sjtc 			usage();
927b5c7989Sjtc 			break;
937b5c7989Sjtc 		}
947b5c7989Sjtc 	}
95c2bd98ebSchristos 	argc -= optind;
96c2bd98ebSchristos 	argv += optind;
977b5c7989Sjtc 
987b5c7989Sjtc 	if (argc == 0)
9961f28255Scgd 		usage();
10061f28255Scgd 
10161f28255Scgd 	errno = 0;
10261f28255Scgd 	niceness += getpriority(PRIO_PROCESS, 0);
10361f28255Scgd 	if (errno) {
104c2bd98ebSchristos 		err(EXIT_FAILURE, "getpriority");
1057b5c7989Sjtc 		/* NOTREACHED */
10661f28255Scgd 	}
107c2bd98ebSchristos 	if (setpriority(PRIO_PROCESS, 0, niceness) == -1) {
1087b5c7989Sjtc 		warn("setpriority");
10961f28255Scgd 	}
11081fd674bSjtc 
111c2bd98ebSchristos 	(void)execvp(argv[0], &argv[0]);
112c2bd98ebSchristos 	err((errno == ENOENT || errno == ENOTDIR) ? 127 : 126, "%s", argv[0]);
11381fd674bSjtc 	/* NOTREACHED */
11461f28255Scgd }
11561f28255Scgd 
116f7c6bf57Sjtc static void
usage(void)117c2bd98ebSchristos usage(void)
11861f28255Scgd {
11961f28255Scgd 	(void)fprintf(stderr,
120c2bd98ebSchristos 	    "Usage: %s [ -n increment ] utility [ argument ...]\n",
121c2bd98ebSchristos 	    getprogname());
122c2bd98ebSchristos 	exit(EXIT_FAILURE);
12361f28255Scgd }
124