1 /* nice -- run a program with modified scheduling priority
2 Copyright (C) 1990-2003 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 Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* David MacKenzie <djm@gnu.ai.mit.edu> */
26 #include <sys/types.h>
31 /* Include this after "system.h" so we're sure to have definitions
32 (from time.h or sys/time.h) required for e.g. the ru_utime member. */
33 # include <sys/resource.h>
37 #include "long-options.h"
41 /* The official name of this program (e.g., no `g' prefix). */
42 #define PROGRAM_NAME "nice"
44 #define AUTHORS "David MacKenzie"
47 # define GET_PRIORITY() nice (0)
49 # define GET_PRIORITY() getpriority (PRIO_PROCESS, 0)
52 /* The name this program was run with. */
55 static struct option const longopts[] =
57 {"adjustment", required_argument, NULL, 'n'},
65 fprintf (stderr, _("Try `%s --help' for more information.\n"),
69 printf (_("Usage: %s [OPTION] [COMMAND [ARG]...]\n"), program_name);
71 Run COMMAND with an adjusted scheduling priority.\n\
72 With no COMMAND, print the current scheduling priority. ADJUST is 10\n\
73 by default. Range goes from -20 (highest priority) to 19 (lowest).\n\
75 -n, --adjustment=ADJUST increment priority by ADJUST first\n\
77 fputs (HELP_OPTION_DESCRIPTION, stdout);
78 fputs (VERSION_OPTION_DESCRIPTION, stdout);
79 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
85 main (int argc, char **argv)
88 long int adjustment = 0;
90 int adjustment_given = 0;
93 initialize_main (&argc, &argv);
94 program_name = argv[0];
95 setlocale (LC_ALL, "");
96 bindtextdomain (PACKAGE, LOCALEDIR);
99 atexit (close_stdout);
101 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
102 usage, AUTHORS, NULL);
104 for (i = 1; i < argc; /* empty */)
108 if (s[0] == '-' && s[1] == '-' && ISDIGIT (s[2])
109 && posix2_version () < 200112)
111 if (xstrtol (&s[2], NULL, 10, &adjustment, "") != LONGINT_OK)
112 error (EXIT_FAILURE, 0, _("invalid option `%s'"), s);
115 adjustment_given = 1;
119 && (ISDIGIT (s[1]) || (s[1] == '+' && ISDIGIT (s[2])))
120 && posix2_version () < 200112)
124 if (xstrtol (&s[1], NULL, 10, &adjustment, "") != LONGINT_OK)
125 error (EXIT_FAILURE, 0, _("invalid option `%s'"), s);
128 adjustment_given = 1;
134 char **fake_argv = argv + i - 1;
136 /* Initialize getopt_long's internal state. */
139 if ((optc = getopt_long (argc - (i - 1), fake_argv, "+n:",
140 longopts, NULL)) != -1)
145 usage (EXIT_FAILURE);
148 if (xstrtol (optarg, NULL, 10, &adjustment, "")
150 error (EXIT_FAILURE, 0, _("invalid priority `%s'"), optarg);
153 adjustment_given = 1;
166 adjustment = -adjustment;
167 if (!adjustment_given)
172 if (adjustment_given)
174 error (0, 0, _("a command must be given with an adjustment"));
175 usage (EXIT_FAILURE);
177 /* No command given; print the priority. */
179 current_priority = GET_PRIORITY ();
180 if (current_priority == -1 && errno != 0)
181 error (EXIT_FAILURE, errno, _("cannot get priority"));
182 printf ("%d\n", current_priority);
186 #ifndef NICE_PRIORITY
188 current_priority = GET_PRIORITY ();
189 if (current_priority == -1 && errno != 0)
190 error (EXIT_FAILURE, errno, _("cannot get priority"));
191 if (setpriority (PRIO_PROCESS, 0, current_priority + adjustment))
193 if (nice (adjustment) == -1)
195 error (EXIT_FAILURE, errno, _("cannot set priority"));
197 execvp (argv[i], &argv[i]);
200 int exit_status = (errno == ENOENT ? 127 : 126);
201 error (0, errno, "%s", argv[i]);