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> */
26 #include <sys/types.h>
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>
37 #include "long-options.h"
41 /* The official name of this program (e.g., no `g' prefix). */
42 #define PROGRAM_NAME "nice"
44 #define AUTHORS "David MacKenzie"
47 # define GET_NICENESS() nice (0)
49 # define GET_NICENESS() getpriority (PRIO_PROCESS, 0)
56 /* This is required for Darwin Kernel Version 7.7.0. */
62 /* The name this program was run with. */
65 static struct option
const longopts
[] =
67 {"adjustment", required_argument
, NULL
, 'n'},
74 if (status
!= EXIT_SUCCESS
)
75 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
79 printf (_("Usage: %s [OPTION] [COMMAND [ARG]...]\n"), program_name
);
81 Run COMMAND with an adjusted niceness, which affects process scheduling.\n\
82 With no COMMAND, print the current niceness. Nicenesses range from\n\
83 %d (most favorable scheduling) to %d (least favorable).\n\
85 -n, --adjustment=N add integer N to the niceness (default 10)\n\
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
);
97 main (int argc
, char **argv
)
101 char const *adjustment_given
= NULL
;
105 initialize_main (&argc
, &argv
);
106 program_name
= argv
[0];
107 setlocale (LC_ALL
, "");
108 bindtextdomain (PACKAGE
, LOCALEDIR
);
109 textdomain (PACKAGE
);
111 initialize_exit_failure (EXIT_FAIL
);
112 atexit (close_stdout
);
114 parse_long_options (argc
, argv
, PROGRAM_NAME
, GNU_PACKAGE
, VERSION
,
115 usage
, AUTHORS
, (char const *) NULL
);
117 for (i
= 1; i
< argc
; /* empty */)
119 char const *s
= argv
[i
];
121 if (s
[0] == '-' && ISDIGIT (s
[1 + (s
[1] == '-' || s
[1] == '+')]))
123 adjustment_given
= s
+ 1;
129 int fake_argc
= argc
- (i
- 1);
130 char **fake_argv
= argv
+ (i
- 1);
132 /* Ensure that any getopt diagnostics use the right name. */
133 fake_argv
[0] = program_name
;
135 /* Initialize getopt_long's internal state. */
138 optc
= getopt_long (fake_argc
, fake_argv
, "+n:", longopts
, NULL
);
143 else if (optc
== 'n')
144 adjustment_given
= optarg
;
145 else /* optc == -1 */
150 if (adjustment_given
)
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 };
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
));
165 if (adjustment_given
)
167 error (0, 0, _("a command must be given with an adjustment"));
170 /* No command given; print the niceness. */
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
);
181 ok
= (nice (adjustment
) != -1 || errno
== 0);
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);
189 error (errno
== EPERM
? 0 : EXIT_FAIL
, 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
]);