3
0

runlevel.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Prints out the previous and the current runlevel.
  4. *
  5. * Version: @(#)runlevel 1.20 16-Apr-1997 MvS
  6. *
  7. * This file is part of the sysvinit suite,
  8. * Copyright 1991-1997 Miquel van Smoorenburg.
  9. *
  10. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  11. *
  12. * initially busyboxified by Bernhard Reutner-Fischer
  13. */
  14. //usage:#define runlevel_trivial_usage
  15. //usage: "[FILE]"
  16. //usage:#define runlevel_full_usage "\n\n"
  17. //usage: "Find the current and previous system runlevel\n"
  18. //usage: "\n"
  19. //usage: "If no utmp FILE exists or if no runlevel record can be found,\n"
  20. //usage: "print \"unknown\""
  21. //usage:
  22. //usage:#define runlevel_example_usage
  23. //usage: "$ runlevel /var/run/utmp\n"
  24. //usage: "N 2"
  25. #include "libbb.h"
  26. int runlevel_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  27. int runlevel_main(int argc UNUSED_PARAM, char **argv)
  28. {
  29. struct utmp *ut;
  30. char prev;
  31. if (argv[1]) utmpname(argv[1]);
  32. setutent();
  33. while ((ut = getutent()) != NULL) {
  34. if (ut->ut_type == RUN_LVL) {
  35. prev = ut->ut_pid / 256;
  36. if (prev == 0) prev = 'N';
  37. printf("%c %c\n", prev, ut->ut_pid % 256);
  38. if (ENABLE_FEATURE_CLEAN_UP)
  39. endutent();
  40. return 0;
  41. }
  42. }
  43. puts("unknown");
  44. if (ENABLE_FEATURE_CLEAN_UP)
  45. endutent();
  46. return 1;
  47. }