1 /* nice -- run a program with modified scheduling priority
2 Copyright (C) 1990, 1991 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* David MacKenzie <djm@gnu.ai.mit.edu> */
21 #if defined (CONFIG_BROKETS)
22 /* We use <config.h> instead of "config.h" so that a compilation
23 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
24 (which it would do because it found this file in $srcdir). */
33 #include <sys/types.h>
36 #include <sys/resource.h>
43 #define GET_PRIORITY() nice (0)
45 #define GET_PRIORITY() getpriority (PRIO_PROCESS, 0)
50 static int isinteger ();
53 /* The name this program was run with. */
56 /* If non-zero, display usage information and exit. */
59 /* If non-zero, print the version on standard output and exit. */
60 static int show_version
;
62 static struct option
const longopts
[] =
64 {"adjustment", required_argument
, NULL
, 'n'},
65 {"help", no_argument
, &show_help
, 1},
66 {"version", no_argument
, &show_version
, 1},
78 int adjustment_given
= 0;
81 program_name
= argv
[0];
83 while ((optc
= getopt_long (argc
, argv
, "+0123456789-n:", longopts
,
95 if (!isinteger (optarg
))
96 error (1, 0, "invalid priority `%s'", optarg
);
97 adjustment
= atoi (optarg
);
106 adjustment
= adjustment
* 10 + optc
- '0';
107 adjustment_given
= 1;
113 printf ("%s\n", version_string
);
121 adjustment
= -adjustment
;
122 if (!adjustment_given
)
127 if (adjustment_given
)
129 /* No command given; print the priority. */
131 current_priority
= GET_PRIORITY ();
132 if (current_priority
== -1 && errno
!= 0)
133 error (1, errno
, "cannot get priority");
134 printf ("%d\n", current_priority
);
138 #ifndef NICE_PRIORITY
140 current_priority
= GET_PRIORITY ();
141 if (current_priority
== -1 && errno
!= 0)
142 error (1, errno
, "cannot get priority");
143 if (setpriority (PRIO_PROCESS
, 0, current_priority
+ adjustment
))
145 if (nice (adjustment
) == -1)
147 error (1, errno
, "cannot set priority");
149 execvp (argv
[optind
], &argv
[optind
]);
150 error (errno
== ENOENT
? 127 : 126, errno
, "%s", argv
[optind
]);
153 /* Return nonzero if S represents a (possibly signed) decimal integer,
166 if (*s
< '0' || *s
> '9')
177 Usage: %s [-n adjustment] [-adjustment] [--adjustment=adjustment]\n\
178 [command [arg...]]\n",