blob: ff3eb1140ee4face070fa24d111ef20b49ef453c [file] [log] [blame]
Manuel Novoa III 2c511602005-02-13 20:14:05 +00001/* vi: set sw=4 ts=4: */
2/*
3 * nice implementation for busybox
4 *
5 * Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org>
6 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Manuel Novoa III 2c511602005-02-13 20:14:05 +00008 */
9
Manuel Novoa III 2c511602005-02-13 20:14:05 +000010#include <sys/resource.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000011#include "libbb.h"
Manuel Novoa III 2c511602005-02-13 20:14:05 +000012
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000013int nice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Manuel Novoa III 2c511602005-02-13 20:14:05 +000014int nice_main(int argc, char **argv)
15{
Manuel Novoa III 2c511602005-02-13 20:14:05 +000016 int old_priority, adjustment;
17
Manuel Novoa III 2c511602005-02-13 20:14:05 +000018 old_priority = getpriority(PRIO_PROCESS, 0);
Manuel Novoa III 2c511602005-02-13 20:14:05 +000019
20 if (!*++argv) { /* No args, so (GNU) output current nice value. */
Denis Vlasenkof0ed3762006-10-26 23:21:47 +000021 printf("%d\n", old_priority);
22 fflush_stdout_and_exit(EXIT_SUCCESS);
Manuel Novoa III 2c511602005-02-13 20:14:05 +000023 }
24
25 adjustment = 10; /* Set default adjustment. */
26
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000027 if (argv[0][0] == '-') {
28 if (argv[0][1] == 'n') { /* -n */
29 if (argv[0][2]) { /* -nNNNN (w/o space) */
30 argv[0] += 2; argv--; argc++;
31 }
32 } else { /* -NNN (NNN may be negative) == -n NNN */
33 argv[0] += 1; argv--; argc++;
34 }
Manuel Novoa III 2c511602005-02-13 20:14:05 +000035 if (argc < 4) { /* Missing priority and/or utility! */
36 bb_show_usage();
37 }
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000038 adjustment = xatoi_range(argv[1], INT_MIN/2, INT_MAX/2);
Manuel Novoa III 2c511602005-02-13 20:14:05 +000039 argv += 2;
40 }
41
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000042 { /* Set our priority. */
43 int prio = old_priority + adjustment;
Manuel Novoa III 2c511602005-02-13 20:14:05 +000044
Denis Vlasenkoca3c9812006-10-08 23:36:17 +000045 if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
46 bb_perror_msg_and_die("setpriority(%d)", prio);
Manuel Novoa III 2c511602005-02-13 20:14:05 +000047 }
48 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000049
Pascal Bellard21e8e8d2010-07-04 00:57:03 +020050 BB_EXECVP_or_die(argv);
Manuel Novoa III 2c511602005-02-13 20:14:05 +000051}