1*d37082b2SThomas Cort /* $NetBSD: nice.c,v 1.15 2008/07/21 14:19:24 lukem Exp $ */
2*d37082b2SThomas Cort
3*d37082b2SThomas Cort /*
4*d37082b2SThomas Cort * Copyright (c) 1989 The Regents of the University of California.
5*d37082b2SThomas Cort * All rights reserved.
6*d37082b2SThomas Cort *
7*d37082b2SThomas Cort * Redistribution and use in source and binary forms, with or without
8*d37082b2SThomas Cort * modification, are permitted provided that the following conditions
9*d37082b2SThomas Cort * are met:
10*d37082b2SThomas Cort * 1. Redistributions of source code must retain the above copyright
11*d37082b2SThomas Cort * notice, this list of conditions and the following disclaimer.
12*d37082b2SThomas Cort * 2. Redistributions in binary form must reproduce the above copyright
13*d37082b2SThomas Cort * notice, this list of conditions and the following disclaimer in the
14*d37082b2SThomas Cort * documentation and/or other materials provided with the distribution.
15*d37082b2SThomas Cort * 3. Neither the name of the University nor the names of its contributors
16*d37082b2SThomas Cort * may be used to endorse or promote products derived from this software
17*d37082b2SThomas Cort * without specific prior written permission.
18*d37082b2SThomas Cort *
19*d37082b2SThomas Cort * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20*d37082b2SThomas Cort * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21*d37082b2SThomas Cort * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22*d37082b2SThomas Cort * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23*d37082b2SThomas Cort * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24*d37082b2SThomas Cort * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25*d37082b2SThomas Cort * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26*d37082b2SThomas Cort * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27*d37082b2SThomas Cort * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28*d37082b2SThomas Cort * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29*d37082b2SThomas Cort * SUCH DAMAGE.
30*d37082b2SThomas Cort */
31*d37082b2SThomas Cort
32*d37082b2SThomas Cort #include <sys/cdefs.h>
33*d37082b2SThomas Cort #ifndef lint
34*d37082b2SThomas Cort __COPYRIGHT("@(#) Copyright (c) 1989\
35*d37082b2SThomas Cort The Regents of the University of California. All rights reserved.");
36*d37082b2SThomas Cort #endif /* not lint */
37*d37082b2SThomas Cort
38*d37082b2SThomas Cort #ifndef lint
39*d37082b2SThomas Cort #if 0
40*d37082b2SThomas Cort static char sccsid[] = "@(#)nice.c 5.4 (Berkeley) 6/1/90";
41*d37082b2SThomas Cort #endif
42*d37082b2SThomas Cort __RCSID("$NetBSD: nice.c,v 1.15 2008/07/21 14:19:24 lukem Exp $");
43*d37082b2SThomas Cort #endif /* not lint */
44*d37082b2SThomas Cort
45*d37082b2SThomas Cort #include <sys/time.h>
46*d37082b2SThomas Cort #include <sys/resource.h>
47*d37082b2SThomas Cort
48*d37082b2SThomas Cort #include <stdio.h>
49*d37082b2SThomas Cort #include <stdlib.h>
50*d37082b2SThomas Cort #include <string.h>
51*d37082b2SThomas Cort #include <limits.h>
52*d37082b2SThomas Cort #include <locale.h>
53*d37082b2SThomas Cort #include <ctype.h>
54*d37082b2SThomas Cort #include <errno.h>
55*d37082b2SThomas Cort #include <err.h>
56*d37082b2SThomas Cort #include <unistd.h>
57*d37082b2SThomas Cort
58*d37082b2SThomas Cort #define DEFNICE 10
59*d37082b2SThomas Cort
60*d37082b2SThomas Cort static void usage(void) __dead;
61*d37082b2SThomas Cort
62*d37082b2SThomas Cort int
main(int argc,char ** argv)63*d37082b2SThomas Cort main(int argc, char **argv)
64*d37082b2SThomas Cort {
65*d37082b2SThomas Cort char *ep;
66*d37082b2SThomas Cort int niceness = DEFNICE;
67*d37082b2SThomas Cort int c;
68*d37082b2SThomas Cort long tmp;
69*d37082b2SThomas Cort
70*d37082b2SThomas Cort setprogname(argv[0]);
71*d37082b2SThomas Cort (void)setlocale(LC_ALL, "");
72*d37082b2SThomas Cort
73*d37082b2SThomas Cort /* handle obsolete -number syntax */
74*d37082b2SThomas Cort if (argc > 1 && argv[1][0] == '-' &&
75*d37082b2SThomas Cort isdigit((unsigned char)argv[1][1])) {
76*d37082b2SThomas Cort niceness = atoi (argv[1] + 1);
77*d37082b2SThomas Cort argc--; argv++;
78*d37082b2SThomas Cort }
79*d37082b2SThomas Cort
80*d37082b2SThomas Cort while ((c = getopt (argc, argv, "n:")) != -1) {
81*d37082b2SThomas Cort switch (c) {
82*d37082b2SThomas Cort case 'n':
83*d37082b2SThomas Cort errno = 0;
84*d37082b2SThomas Cort tmp = strtol(optarg, &ep, 10);
85*d37082b2SThomas Cort if (*ep != '\0' || tmp < INT_MIN || tmp > INT_MAX)
86*d37082b2SThomas Cort errx(EXIT_FAILURE, "invalid argument: `%s'",
87*d37082b2SThomas Cort optarg);
88*d37082b2SThomas Cort niceness = (int)tmp;
89*d37082b2SThomas Cort break;
90*d37082b2SThomas Cort default:
91*d37082b2SThomas Cort usage();
92*d37082b2SThomas Cort break;
93*d37082b2SThomas Cort }
94*d37082b2SThomas Cort }
95*d37082b2SThomas Cort argc -= optind;
96*d37082b2SThomas Cort argv += optind;
97*d37082b2SThomas Cort
98*d37082b2SThomas Cort if (argc == 0)
99*d37082b2SThomas Cort usage();
100*d37082b2SThomas Cort
101*d37082b2SThomas Cort errno = 0;
102*d37082b2SThomas Cort niceness += getpriority(PRIO_PROCESS, 0);
103*d37082b2SThomas Cort if (errno) {
104*d37082b2SThomas Cort err(EXIT_FAILURE, "getpriority");
105*d37082b2SThomas Cort /* NOTREACHED */
106*d37082b2SThomas Cort }
107*d37082b2SThomas Cort if (setpriority(PRIO_PROCESS, 0, niceness) == -1) {
108*d37082b2SThomas Cort warn("setpriority");
109*d37082b2SThomas Cort }
110*d37082b2SThomas Cort
111*d37082b2SThomas Cort (void)execvp(argv[0], &argv[0]);
112*d37082b2SThomas Cort err((errno == ENOENT || errno == ENOTDIR) ? 127 : 126, "%s", argv[0]);
113*d37082b2SThomas Cort /* NOTREACHED */
114*d37082b2SThomas Cort }
115*d37082b2SThomas Cort
116*d37082b2SThomas Cort static void
usage(void)117*d37082b2SThomas Cort usage(void)
118*d37082b2SThomas Cort {
119*d37082b2SThomas Cort (void)fprintf(stderr,
120*d37082b2SThomas Cort "Usage: %s [ -n increment ] utility [ argument ...]\n",
121*d37082b2SThomas Cort getprogname());
122*d37082b2SThomas Cort exit(EXIT_FAILURE);
123*d37082b2SThomas Cort }
124