1 /* nice -- run a program with modified scheduling priority
2 Copyright (C) 90, 91, 92, 93, 94, 1995 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> */
27 #include <sys/types.h>
30 #include <sys/resource.h>
35 #include "long-options.h"
39 #define GET_PRIORITY() nice (0)
41 #define GET_PRIORITY() getpriority (PRIO_PROCESS, 0)
44 static int isinteger ();
47 /* The name this program was run with. */
50 static struct option const longopts[] =
52 {"adjustment", required_argument, NULL, 'n'},
64 int adjustment_given = 0;
65 int long_option_priority = 0;
68 program_name = argv[0];
69 parse_long_options (argc, argv, "nice", version_string, usage);
71 for (optind = 1; optind < argc; /* empty */)
77 if (s[0] == '-' && s[1] == '-' && ISDIGIT (s[2]))
79 if (!isinteger (&s[2]))
80 error (1, 0, "invalid option `%s'", s);
83 adjustment = atoi (&s[2]);
85 long_option_priority = 1;
91 while ((optc = getopt_long (argc, argv, "+0123456789n:", longopts,
100 if (!isinteger (optarg))
101 error (1, 0, "invalid priority `%s'", optarg);
102 adjustment = atoi (optarg);
103 adjustment_given = 1;
107 assert (ISDIGIT (optc));
108 /* Reset ADJUSTMENT if the last priority-specifying option
109 was not of the same type or if it was, but a separate
111 if (long_option_priority ||
112 (adjustment_given && optind != last_optind))
114 long_option_priority = 0;
117 adjustment = adjustment * 10 + optc - '0';
118 adjustment_given = 1;
119 last_optind = optind;
128 adjustment = -adjustment;
129 if (!adjustment_given)
134 if (adjustment_given)
136 error (0, 0, "a command must be given with an adjustment");
139 /* No command given; print the priority. */
141 current_priority = GET_PRIORITY ();
142 if (current_priority == -1 && errno != 0)
143 error (1, errno, "cannot get priority");
144 printf ("%d\n", current_priority);
148 #ifndef NICE_PRIORITY
150 current_priority = GET_PRIORITY ();
151 if (current_priority == -1 && errno != 0)
152 error (1, errno, "cannot get priority");
153 if (setpriority (PRIO_PROCESS, 0, current_priority + adjustment))
155 if (nice (adjustment) == -1)
157 error (1, errno, "cannot set priority");
159 execvp (argv[optind], &argv[optind]);
160 error (errno == ENOENT ? 127 : 126, errno, "%s", argv[optind]);
163 /* Return nonzero if S represents a (possibly signed) decimal integer,
170 if (*s == '-' || *s == '+')
188 fprintf (stderr, "Try `%s --help' for more information.\n",
192 printf ("Usage: %s [OPTION]... [COMMAND [ARG]...]\n", program_name);
195 -ADJUST increment priority by ADJUST first\n\
196 -n, --adjustment=ADJUST same as -ADJUST\n\
197 --help display this help and exit\n\
198 --version output version information and exit\n\
200 With no COMMAND, print the current scheduling priority. ADJUST is 10\n\
201 by default. Range goes from -20 (highest priority) to 19 (lowest).\n\