source: trunk/coreutils/src/nice.c@ 2603

Last change on this file since 2603 was 2554, checked in by bird, 20 years ago

coretuils-5.94

File size: 5.2 KB
Line 
1/* nice -- run a program with modified niceness
2 Copyright (C) 1990-2005 Free Software Foundation, Inc.
3
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.
8
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.
13
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. */
17
18/* David MacKenzie <[email protected]> */
19
20#include <config.h>
21#include <stdio.h>
22
23#include <assert.h>
24
25#include <getopt.h>
26#include <sys/types.h>
27
28#include "system.h"
29
30#if ! HAVE_NICE
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>
34#endif
35
36#include "error.h"
37#include "long-options.h"
38#include "quote.h"
39#include "xstrtol.h"
40
41/* The official name of this program (e.g., no `g' prefix). */
42#define PROGRAM_NAME "nice"
43
44#define AUTHORS "David MacKenzie"
45
46#if HAVE_NICE
47# define GET_NICENESS() nice (0)
48#else
49# define GET_NICENESS() getpriority (PRIO_PROCESS, 0)
50#endif
51
52#ifndef NZERO
53# define NZERO 20
54#endif
55
56/* This is required for Darwin Kernel Version 7.7.0. */
57#if NZERO == 0
58# undef NZERO
59# define NZERO 20
60#endif
61
62/* The name this program was run with. */
63char *program_name;
64
65static struct option const longopts[] =
66{
67 {"adjustment", required_argument, NULL, 'n'},
68 {NULL, 0, NULL, 0}
69};
70
71void
72usage (int status)
73{
74 if (status != EXIT_SUCCESS)
75 fprintf (stderr, _("Try `%s --help' for more information.\n"),
76 program_name);
77 else
78 {
79 printf (_("Usage: %s [OPTION] [COMMAND [ARG]...]\n"), program_name);
80 printf (_("\
81Run COMMAND with an adjusted niceness, which affects process scheduling.\n\
82With no COMMAND, print the current niceness. Nicenesses range from\n\
83%d (most favorable scheduling) to %d (least favorable).\n\
84\n\
85 -n, --adjustment=N add integer N to the niceness (default 10)\n\
86"),
87 - NZERO, NZERO - 1);
88 fputs (HELP_OPTION_DESCRIPTION, stdout);
89 fputs (VERSION_OPTION_DESCRIPTION, stdout);
90 printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
91 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
92 }
93 exit (status);
94}
95
96int
97main (int argc, char **argv)
98{
99 int current_niceness;
100 int adjustment = 10;
101 char const *adjustment_given = NULL;
102 bool ok;
103 int i;
104
105 initialize_main (&argc, &argv);
106 program_name = argv[0];
107 setlocale (LC_ALL, "");
108 bindtextdomain (PACKAGE, LOCALEDIR);
109 textdomain (PACKAGE);
110
111 initialize_exit_failure (EXIT_FAIL);
112 atexit (close_stdout);
113
114 parse_long_options (argc, argv, PROGRAM_NAME, GNU_PACKAGE, VERSION,
115 usage, AUTHORS, (char const *) NULL);
116
117 for (i = 1; i < argc; /* empty */)
118 {
119 char const *s = argv[i];
120
121 if (s[0] == '-' && ISDIGIT (s[1 + (s[1] == '-' || s[1] == '+')]))
122 {
123 adjustment_given = s + 1;
124 ++i;
125 }
126 else
127 {
128 int optc;
129 int fake_argc = argc - (i - 1);
130 char **fake_argv = argv + (i - 1);
131
132 /* Ensure that any getopt diagnostics use the right name. */
133 fake_argv[0] = program_name;
134
135 /* Initialize getopt_long's internal state. */
136 optind = 0;
137
138 optc = getopt_long (fake_argc, fake_argv, "+n:", longopts, NULL);
139 i += optind - 1;
140
141 if (optc == '?')
142 usage (EXIT_FAIL);
143 else if (optc == 'n')
144 adjustment_given = optarg;
145 else /* optc == -1 */
146 break;
147 }
148 }
149
150 if (adjustment_given)
151 {
152 /* If the requested adjustment is outside the valid range,
153 silently bring it to just within range; this mimics what
154 "setpriority" and "nice" do. */
155 enum { MIN_ADJUSTMENT = 1 - 2 * NZERO, MAX_ADJUSTMENT = 2 * NZERO - 1 };
156 long int tmp;
157 if (LONGINT_OVERFLOW < xstrtol (adjustment_given, NULL, 10, &tmp, ""))
158 error (EXIT_FAIL, 0, _("invalid adjustment %s"),
159 quote (adjustment_given));
160 adjustment = MAX (MIN_ADJUSTMENT, MIN (tmp, MAX_ADJUSTMENT));
161 }
162
163 if (i == argc)
164 {
165 if (adjustment_given)
166 {
167 error (0, 0, _("a command must be given with an adjustment"));
168 usage (EXIT_FAIL);
169 }
170 /* No command given; print the niceness. */
171 errno = 0;
172 current_niceness = GET_NICENESS ();
173 if (current_niceness == -1 && errno != 0)
174 error (EXIT_FAIL, errno, _("cannot get niceness"));
175 printf ("%d\n", current_niceness);
176 exit (EXIT_SUCCESS);
177 }
178
179 errno = 0;
180#if HAVE_NICE
181 ok = (nice (adjustment) != -1 || errno == 0);
182#else
183 current_niceness = GET_NICENESS ();
184 if (current_niceness == -1 && errno != 0)
185 error (EXIT_FAIL, errno, _("cannot get niceness"));
186 ok = (setpriority (PRIO_PROCESS, 0, current_niceness + adjustment) == 0);
187#endif
188 if (!ok)
189 error (errno == EPERM ? 0 : EXIT_FAIL, errno, _("cannot set niceness"));
190
191 execvp (argv[i], &argv[i]);
192
193 {
194 int exit_status = (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
195 error (0, errno, "%s", argv[i]);
196 exit (exit_status);
197 }
198}
Note: See TracBrowser for help on using the repository browser.