nice.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 "busybox.h"
  11. int nice_main(int argc, char **argv)
  12. {
  13. int old_priority, adjustment;
  14. old_priority = getpriority(PRIO_PROCESS, 0);
  15. if (!*++argv) { /* No args, so (GNU) output current nice value. */
  16. printf("%d\n", old_priority);
  17. fflush_stdout_and_exit(EXIT_SUCCESS);
  18. }
  19. adjustment = 10; /* Set default adjustment. */
  20. if (argv[0][0] == '-') {
  21. if (argv[0][1] == 'n') { /* -n */
  22. if (argv[0][2]) { /* -nNNNN (w/o space) */
  23. argv[0] += 2; argv--; argc++;
  24. }
  25. } else { /* -NNN (NNN may be negative) == -n NNN */
  26. argv[0] += 1; argv--; argc++;
  27. }
  28. if (argc < 4) { /* Missing priority and/or utility! */
  29. bb_show_usage();
  30. }
  31. adjustment = xatoi_range(argv[1], INT_MIN/2, INT_MAX/2);
  32. argv += 2;
  33. }
  34. { /* Set our priority. */
  35. int prio = old_priority + adjustment;
  36. if (setpriority(PRIO_PROCESS, 0, prio) < 0) {
  37. bb_perror_msg_and_die("setpriority(%d)", prio);
  38. }
  39. }
  40. execvp(*argv, argv); /* Now exec the desired program. */
  41. /* The exec failed... */
  42. xfunc_error_retval = (errno == ENOENT) ? 127 : 126; /* SUSv3 */
  43. bb_perror_msg_and_die("%s", *argv);
  44. }