* src/dd.c (flags): noatime and nofollow now depend on
[coreutils/bo.git] / src / nice.c
blob19b38bc3423ae2313fa1b4b5050772adac2298f0
1 /* nice -- run a program with modified niceness
2 Copyright (C) 1990-2005 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)
7 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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* David MacKenzie <djm@gnu.ai.mit.edu> */
20 #include <config.h>
21 #include <stdio.h>
22 #include <getopt.h>
23 #include <sys/types.h>
25 #include "system.h"
27 #if ! HAVE_NICE
28 /* Include this after "system.h" so we're sure to have definitions
29 (from time.h or sys/time.h) required for e.g. the ru_utime member. */
30 # include <sys/resource.h>
31 #endif
33 #include "error.h"
34 #include "long-options.h"
35 #include "quote.h"
36 #include "xstrtol.h"
38 /* The official name of this program (e.g., no `g' prefix). */
39 #define PROGRAM_NAME "nice"
41 #define AUTHORS "David MacKenzie"
43 #if HAVE_NICE
44 # define GET_NICENESS() nice (0)
45 #else
46 # define GET_NICENESS() getpriority (PRIO_PROCESS, 0)
47 #endif
49 #ifndef NZERO
50 # define NZERO 20
51 #endif
53 /* This is required for Darwin Kernel Version 7.7.0. */
54 #if NZERO == 0
55 # undef NZERO
56 # define NZERO 20
57 #endif
59 /* The name this program was run with. */
60 char *program_name;
62 static struct option const longopts[] =
64 {"adjustment", required_argument, NULL, 'n'},
65 {NULL, 0, NULL, 0}
68 void
69 usage (int status)
71 if (status != EXIT_SUCCESS)
72 fprintf (stderr, _("Try `%s --help' for more information.\n"),
73 program_name);
74 else
76 printf (_("Usage: %s [OPTION] [COMMAND [ARG]...]\n"), program_name);
77 printf (_("\
78 Run COMMAND with an adjusted niceness, which affects process scheduling.\n\
79 With no COMMAND, print the current niceness. Nicenesses range from\n\
80 %d (most favorable scheduling) to %d (least favorable).\n\
81 \n\
82 -n, --adjustment=N add integer N to the niceness (default 10)\n\
83 "),
84 - NZERO, NZERO - 1);
85 fputs (HELP_OPTION_DESCRIPTION, stdout);
86 fputs (VERSION_OPTION_DESCRIPTION, stdout);
87 printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
88 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
90 exit (status);
93 int
94 main (int argc, char **argv)
96 int current_niceness;
97 int adjustment = 10;
98 char const *adjustment_given = NULL;
99 bool ok;
100 int i;
102 initialize_main (&argc, &argv);
103 program_name = argv[0];
104 setlocale (LC_ALL, "");
105 bindtextdomain (PACKAGE, LOCALEDIR);
106 textdomain (PACKAGE);
108 initialize_exit_failure (EXIT_FAIL);
109 atexit (close_stdout);
111 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
112 usage, AUTHORS, (char const *) NULL);
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 optc;
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] = program_name;
132 /* Initialize getopt_long's internal state. */
133 optind = 0;
135 optc = getopt_long (fake_argc, fake_argv, "+n:", longopts, NULL);
136 i += optind - 1;
138 if (optc == '?')
139 usage (EXIT_FAIL);
140 else if (optc == 'n')
141 adjustment_given = optarg;
142 else /* optc == -1 */
143 break;
147 if (adjustment_given)
149 /* If the requested adjustment is outside the valid range,
150 silently bring it to just within range; this mimics what
151 "setpriority" and "nice" do. */
152 enum { MIN_ADJUSTMENT = 1 - 2 * NZERO, MAX_ADJUSTMENT = 2 * NZERO - 1 };
153 long int tmp;
154 if (LONGINT_OVERFLOW < xstrtol (adjustment_given, NULL, 10, &tmp, ""))
155 error (EXIT_FAIL, 0, _("invalid adjustment %s"),
156 quote (adjustment_given));
157 adjustment = MAX (MIN_ADJUSTMENT, MIN (tmp, MAX_ADJUSTMENT));
160 if (i == argc)
162 if (adjustment_given)
164 error (0, 0, _("a command must be given with an adjustment"));
165 usage (EXIT_FAIL);
167 /* No command given; print the niceness. */
168 errno = 0;
169 current_niceness = GET_NICENESS ();
170 if (current_niceness == -1 && errno != 0)
171 error (EXIT_FAIL, errno, _("cannot get niceness"));
172 printf ("%d\n", current_niceness);
173 exit (EXIT_SUCCESS);
176 errno = 0;
177 #if HAVE_NICE
178 ok = (nice (adjustment) != -1 || errno == 0);
179 #else
180 current_niceness = GET_NICENESS ();
181 if (current_niceness == -1 && errno != 0)
182 error (EXIT_FAIL, errno, _("cannot get niceness"));
183 ok = (setpriority (PRIO_PROCESS, 0, current_niceness + adjustment) == 0);
184 #endif
185 if (!ok)
186 error (errno == EPERM ? 0 : EXIT_FAIL, errno, _("cannot set niceness"));
188 execvp (argv[i], &argv[i]);
191 int exit_status = (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
192 error (0, errno, "%s", argv[i]);
193 exit (exit_status);