maint: revert "build: update gnulib submodule to latest"
[coreutils/ericb.git] / src / nice.c
bloba011eecd1b3e1b18d116668a8bb39b6add2a1e7b
1 /* nice -- run a program with modified niceness
2 Copyright (C) 1990-2005, 2007-2011 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 3 of the License, or
7 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
17 /* David MacKenzie <djm@gnu.ai.mit.edu> */
19 #include <config.h>
20 #include <stdio.h>
21 #include <getopt.h>
22 #include <sys/types.h>
24 #include "system.h"
26 #if ! HAVE_NICE
27 /* Include this after "system.h" so we're sure to have definitions
28 (from time.h or sys/time.h) required for e.g. the ru_utime member. */
29 # include <sys/resource.h>
30 #endif
32 #include "error.h"
33 #include "quote.h"
34 #include "xstrtol.h"
36 /* The official name of this program (e.g., no `g' prefix). */
37 #define PROGRAM_NAME "nice"
39 #define AUTHORS proper_name ("David MacKenzie")
41 #if HAVE_NICE
42 # define GET_NICENESS() nice (0)
43 #else
44 # define GET_NICENESS() getpriority (PRIO_PROCESS, 0)
45 #endif
47 #ifndef NZERO
48 # define NZERO 20
49 #endif
51 /* This is required for Darwin Kernel Version 7.7.0. */
52 #if NZERO == 0
53 # undef NZERO
54 # define NZERO 20
55 #endif
57 static struct option const longopts[] =
59 {"adjustment", required_argument, NULL, 'n'},
60 {GETOPT_HELP_OPTION_DECL},
61 {GETOPT_VERSION_OPTION_DECL},
62 {NULL, 0, NULL, 0}
65 void
66 usage (int status)
68 if (status != EXIT_SUCCESS)
69 fprintf (stderr, _("Try `%s --help' for more information.\n"),
70 program_name);
71 else
73 printf (_("Usage: %s [OPTION] [COMMAND [ARG]...]\n"), program_name);
74 printf (_("\
75 Run COMMAND with an adjusted niceness, which affects process scheduling.\n\
76 With no COMMAND, print the current niceness. Nicenesses range from\n\
77 %d (most favorable scheduling) to %d (least favorable).\n\
78 \n\
79 -n, --adjustment=N add integer N to the niceness (default 10)\n\
80 "),
81 - NZERO, NZERO - 1);
82 fputs (HELP_OPTION_DESCRIPTION, stdout);
83 fputs (VERSION_OPTION_DESCRIPTION, stdout);
84 printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
85 emit_ancillary_info ();
87 exit (status);
90 static bool
91 perm_related_errno (int err)
93 return err == EACCES || err == EPERM;
96 int
97 main (int argc, char **argv)
99 int current_niceness;
100 int adjustment = 10;
101 char const *adjustment_given = NULL;
102 bool ok;
103 int i;
105 initialize_main (&argc, &argv);
106 set_program_name (argv[0]);
107 setlocale (LC_ALL, "");
108 bindtextdomain (PACKAGE, LOCALEDIR);
109 textdomain (PACKAGE);
111 initialize_exit_failure (EXIT_CANCELED);
112 atexit (close_stdout);
114 for (i = 1; i < argc; /* empty */)
116 char const *s = argv[i];
118 if (s[0] == '-' && ISDIGIT (s[1 + (s[1] == '-' || s[1] == '+')]))
120 adjustment_given = s + 1;
121 ++i;
123 else
125 int c;
126 int fake_argc = argc - (i - 1);
127 char **fake_argv = argv + (i - 1);
129 /* Ensure that any getopt diagnostics use the right name. */
130 fake_argv[0] = argv[0];
132 /* Initialize getopt_long's internal state. */
133 optind = 0;
135 c = getopt_long (fake_argc, fake_argv, "+n:", longopts, NULL);
136 i += optind - 1;
138 switch (c)
140 case 'n':
141 adjustment_given = optarg;
142 break;
144 case -1:
145 break;
147 case_GETOPT_HELP_CHAR;
149 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
151 default:
152 usage (EXIT_CANCELED);
153 break;
156 if (c == -1)
157 break;
161 if (adjustment_given)
163 /* If the requested adjustment is outside the valid range,
164 silently bring it to just within range; this mimics what
165 "setpriority" and "nice" do. */
166 enum { MIN_ADJUSTMENT = 1 - 2 * NZERO, MAX_ADJUSTMENT = 2 * NZERO - 1 };
167 long int tmp;
168 if (LONGINT_OVERFLOW < xstrtol (adjustment_given, NULL, 10, &tmp, ""))
169 error (EXIT_CANCELED, 0, _("invalid adjustment %s"),
170 quote (adjustment_given));
171 adjustment = MAX (MIN_ADJUSTMENT, MIN (tmp, MAX_ADJUSTMENT));
174 if (i == argc)
176 if (adjustment_given)
178 error (0, 0, _("a command must be given with an adjustment"));
179 usage (EXIT_CANCELED);
181 /* No command given; print the niceness. */
182 errno = 0;
183 current_niceness = GET_NICENESS ();
184 if (current_niceness == -1 && errno != 0)
185 error (EXIT_CANCELED, errno, _("cannot get niceness"));
186 printf ("%d\n", current_niceness);
187 exit (EXIT_SUCCESS);
190 errno = 0;
191 #if HAVE_NICE
192 ok = (nice (adjustment) != -1 || errno == 0);
193 #else
194 current_niceness = GET_NICENESS ();
195 if (current_niceness == -1 && errno != 0)
196 error (EXIT_CANCELED, errno, _("cannot get niceness"));
197 ok = (setpriority (PRIO_PROCESS, 0, current_niceness + adjustment) == 0);
198 #endif
199 if (!ok)
201 error (perm_related_errno (errno) ? 0
202 : EXIT_CANCELED, errno, _("cannot set niceness"));
203 /* error() flushes stderr, but does not check for write failure.
204 Normally, we would catch this via our atexit() hook of
205 close_stdout, but execvp() gets in the way. If stderr
206 encountered a write failure, there is no need to try calling
207 error() again. */
208 if (ferror (stderr))
209 exit (EXIT_CANCELED);
212 execvp (argv[i], &argv[i]);
215 int exit_status = (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
216 error (0, errno, "%s", argv[i]);
217 exit (exit_status);