nice.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * nice implementation for busybox
  4. *
  5. * Copyright (C) 2005 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. #include <sys/resource.h>
  10. #include "libbb.h"
  11. int nice_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  12. int nice_main(int argc, char **argv)
  13. {
  14. int old_priority, adjustment;
  15. old_priority = getpriority(PRIO_PROCESS, 0);
  16. if (!*++argv) { /* No args, so (GNU) output current nice value. */
  17. printf("%d\n", old_priority);
  18. fflush_stdout_and_exit(EXIT_SUCCESS);
  19. }
  20. adjustment = 10; /* Set default adjustment. */
  21. if (argv[0][0] == '-') {
  22. if (argv[0][1] == 'n') { /* -n */
  23. if (argv[0][2]) { /* -nNNNN (w/o space) */
  24. argv[0] += 2; argv--; argc++;
  25. }
  26. } else { /* -NNN (NNN may be negative) == -n NNN */
  27. argv[0] += 1; argv--; argc++;
  28. }
  29. if (argc < 4) { /* Missing priority and/or utility! */
  30. bb_show_usage();
  31. }
  32. adjustment = xatoi_range(argv[1], INT_MIN/2, INT_MAX/2);
  33. argv += 2;
  34. }
  35. { /* Set our priority. */
  36. int prio = old_priority + adjustment;
  37. if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
  38. bb_perror_msg_and_die("setpriority(%d)", prio);
  39. }
  40. }
  41. BB_EXECVP_or_die(argv);
  42. }