env, printenv: add -0/--null option
[coreutils.git] / src / nice.c
blobe157db80111375532d984bb5ba81f0d8a5c0f17c
1 /* nice -- run a program with modified niceness
2 Copyright (C) 1990-2005, 2007-2009 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 "long-options.h"
34 #include "quote.h"
35 #include "xstrtol.h"
37 /* The official name of this program (e.g., no `g' prefix). */
38 #define PROGRAM_NAME "nice"
40 #define AUTHORS proper_name ("David MacKenzie")
42 #if HAVE_NICE
43 # define GET_NICENESS() nice (0)
44 #else
45 # define GET_NICENESS() getpriority (PRIO_PROCESS, 0)
46 #endif
48 #ifndef NZERO
49 # define NZERO 20
50 #endif
52 /* This is required for Darwin Kernel Version 7.7.0. */
53 #if NZERO == 0
54 # undef NZERO
55 # define NZERO 20
56 #endif
58 static struct option const longopts[] =
60 {"adjustment", required_argument, NULL, 'n'},
61 {NULL, 0, NULL, 0}
64 void
65 usage (int status)
67 if (status != EXIT_SUCCESS)
68 fprintf (stderr, _("Try `%s --help' for more information.\n"),
69 program_name);
70 else
72 printf (_("Usage: %s [OPTION] [COMMAND [ARG]...]\n"), program_name);
73 printf (_("\
74 Run COMMAND with an adjusted niceness, which affects process scheduling.\n\
75 With no COMMAND, print the current niceness. Nicenesses range from\n\
76 %d (most favorable scheduling) to %d (least favorable).\n\
77 \n\
78 -n, --adjustment=N add integer N to the niceness (default 10)\n\
79 "),
80 - NZERO, NZERO - 1);
81 fputs (HELP_OPTION_DESCRIPTION, stdout);
82 fputs (VERSION_OPTION_DESCRIPTION, stdout);
83 printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
84 emit_ancillary_info ();
86 exit (status);
89 static bool
90 perm_related_errno (int err)
92 return err == EACCES || err == EPERM;
95 int
96 main (int argc, char **argv)
98 int current_niceness;
99 int adjustment = 10;
100 char const *adjustment_given = NULL;
101 bool ok;
102 int i;
104 initialize_main (&argc, &argv);
105 set_program_name (argv[0]);
106 setlocale (LC_ALL, "");
107 bindtextdomain (PACKAGE, LOCALEDIR);
108 textdomain (PACKAGE);
110 initialize_exit_failure (EXIT_CANCELED);
111 atexit (close_stdout);
113 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version,
114 usage, AUTHORS, (char const *) NULL);
116 for (i = 1; i < argc; /* empty */)
118 char const *s = argv[i];
120 if (s[0] == '-' && ISDIGIT (s[1 + (s[1] == '-' || s[1] == '+')]))
122 adjustment_given = s + 1;
123 ++i;
125 else
127 int optc;
128 int fake_argc = argc - (i - 1);
129 char **fake_argv = argv + (i - 1);
131 /* Ensure that any getopt diagnostics use the right name. */
132 fake_argv[0] = argv[0];
134 /* Initialize getopt_long's internal state. */
135 optind = 0;
137 optc = getopt_long (fake_argc, fake_argv, "+n:", longopts, NULL);
138 i += optind - 1;
140 if (optc == '?')
141 usage (EXIT_CANCELED);
142 else if (optc == 'n')
143 adjustment_given = optarg;
144 else /* optc == -1 */
145 break;
149 if (adjustment_given)
151 /* If the requested adjustment is outside the valid range,
152 silently bring it to just within range; this mimics what
153 "setpriority" and "nice" do. */
154 enum { MIN_ADJUSTMENT = 1 - 2 * NZERO, MAX_ADJUSTMENT = 2 * NZERO - 1 };
155 long int tmp;
156 if (LONGINT_OVERFLOW < xstrtol (adjustment_given, NULL, 10, &tmp, ""))
157 error (EXIT_CANCELED, 0, _("invalid adjustment %s"),
158 quote (adjustment_given));
159 adjustment = MAX (MIN_ADJUSTMENT, MIN (tmp, MAX_ADJUSTMENT));
162 if (i == argc)
164 if (adjustment_given)
166 error (0, 0, _("a command must be given with an adjustment"));
167 usage (EXIT_CANCELED);
169 /* No command given; print the niceness. */
170 errno = 0;
171 current_niceness = GET_NICENESS ();
172 if (current_niceness == -1 && errno != 0)
173 error (EXIT_CANCELED, errno, _("cannot get niceness"));
174 printf ("%d\n", current_niceness);
175 exit (EXIT_SUCCESS);
178 errno = 0;
179 #if HAVE_NICE
180 ok = (nice (adjustment) != -1 || errno == 0);
181 #else
182 current_niceness = GET_NICENESS ();
183 if (current_niceness == -1 && errno != 0)
184 error (EXIT_CANCELED, errno, _("cannot get niceness"));
185 ok = (setpriority (PRIO_PROCESS, 0, current_niceness + adjustment) == 0);
186 #endif
187 if (!ok)
188 error (perm_related_errno (errno) ? 0
189 : EXIT_CANCELED, errno, _("cannot set niceness"));
191 execvp (argv[i], &argv[i]);
194 int exit_status = (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
195 error (0, errno, "%s", argv[i]);
196 exit (exit_status);