busybox/coreutils/nice.c
Denys Vlasenko 31f45c1b36 libbb: factor out fflush_stdout_and_exit(EXIT_SUCCESS)
function                                             old     new   delta
fflush_stdout_and_exit_SUCCESS                         -       7      +7
xxd_main                                             890     888      -2
vlock_main                                           353     351      -2
uuencode_main                                        318     316      -2
uniq_main                                            427     425      -2
uname_main                                           250     248      -2
sort_main                                            853     851      -2
shuf_main                                            500     498      -2
route_main                                           238     236      -2
readlink_main                                        113     111      -2
nice_main                                            156     154      -2
last_main                                            957     955      -2
ipcs_main                                            960     958      -2
env_main                                             209     207      -2
chrt_main                                            464     462      -2
cal_main                                             921     919      -2
baseNUM_main                                         650     648      -2
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/16 up/down: 7/-32)            Total: -25 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2022-01-04 23:31:58 +01:00

69 lines
1.7 KiB
C

/* vi: set sw=4 ts=4: */
/*
* nice implementation for busybox
*
* Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org>
*
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
//config:config NICE
//config: bool "nice (2.1 kb)"
//config: default y
//config: help
//config: nice runs a program with modified scheduling priority.
//applet:IF_NICE(APPLET_NOEXEC(nice, nice, BB_DIR_BIN, BB_SUID_DROP, nice))
//kbuild:lib-$(CONFIG_NICE) += nice.o
//usage:#define nice_trivial_usage
//usage: "[-n ADJUST] [PROG ARGS]"
//usage:#define nice_full_usage "\n\n"
//usage: "Change scheduling priority, run PROG\n"
//usage: "\n -n ADJUST Adjust priority by ADJUST"
#include "libbb.h"
int nice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int nice_main(int argc UNUSED_PARAM, char **argv)
{
int old_priority, adjustment;
old_priority = getpriority(PRIO_PROCESS, 0);
if (!*++argv) { /* No args, so (GNU) output current nice value. */
printf("%d\n", old_priority);
fflush_stdout_and_exit_SUCCESS();
}
adjustment = 10; /* Set default adjustment. */
if (argv[0][0] == '-') {
char *nnn = argv[0] + 1;
if (nnn[0] == 'n') { /* -n */
nnn += 1;
if (!nnn[0]) { /* "-n NNN" */
nnn = *++argv;
}
/* else: "-nNNN" (w/o space) */
}
/* else: "-NNN" (NNN may be negative) - same as "-n NNN" */
if (!nnn || !argv[1]) { /* Missing priority or PROG! */
bb_show_usage();
}
adjustment = xatoi_range(nnn, INT_MIN/2, INT_MAX/2);
argv++;
}
{ /* Set our priority. */
int prio = old_priority + adjustment;
if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
bb_perror_msg_and_die("setpriority(%d)", prio);
}
}
BB_EXECVP_or_die(argv);
}