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)
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> */
23 #include <sys/types.h>
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>
34 #include "long-options.h"
38 /* The official name of this program (e.g., no `g' prefix). */
39 #define PROGRAM_NAME "nice"
41 #define AUTHORS "David MacKenzie"
44 # define GET_NICENESS() nice (0)
46 # define GET_NICENESS() getpriority (PRIO_PROCESS, 0)
53 /* This is required for Darwin Kernel Version 7.7.0. */
59 /* The name this program was run with. */
62 static struct option
const longopts
[] =
64 {"adjustment", required_argument
, NULL
, 'n'},
71 if (status
!= EXIT_SUCCESS
)
72 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
76 printf (_("Usage: %s [OPTION] [COMMAND [ARG]...]\n"), program_name
);
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\
82 -n, --adjustment=N add integer N to the niceness (default 10)\n\
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
);
94 main (int argc
, char **argv
)
98 char const *adjustment_given
= NULL
;
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;
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. */
135 optc
= getopt_long (fake_argc
, fake_argv
, "+n:", longopts
, NULL
);
140 else if (optc
== 'n')
141 adjustment_given
= optarg
;
142 else /* optc == -1 */
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 };
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
));
162 if (adjustment_given
)
164 error (0, 0, _("a command must be given with an adjustment"));
167 /* No command given; print the niceness. */
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
);
178 ok
= (nice (adjustment
) != -1 || errno
== 0);
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);
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
]);