uptime.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini uptime implementation for busybox
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this source tree.
  8. */
  9. /* This version of uptime doesn't display the number of users on the system,
  10. * since busybox init doesn't mess with utmp. For folks using utmp that are
  11. * just dying to have # of users reported, feel free to write it as some type
  12. * of CONFIG_FEATURE_UTMP_SUPPORT #define
  13. */
  14. /* getopt not needed */
  15. #include "libbb.h"
  16. #ifndef FSHIFT
  17. # define FSHIFT 16 /* nr of bits of precision */
  18. #endif
  19. #define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */
  20. #define LOAD_INT(x) ((x) >> FSHIFT)
  21. #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
  22. int uptime_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  23. int uptime_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  24. {
  25. int updays, uphours, upminutes;
  26. struct sysinfo info;
  27. struct tm *current_time;
  28. time_t current_secs;
  29. time(&current_secs);
  30. current_time = localtime(&current_secs);
  31. sysinfo(&info);
  32. printf(" %02d:%02d:%02d up ",
  33. current_time->tm_hour, current_time->tm_min, current_time->tm_sec);
  34. updays = (int) info.uptime / (60*60*24);
  35. if (updays)
  36. printf("%d day%s, ", updays, (updays != 1) ? "s" : "");
  37. upminutes = (int) info.uptime / 60;
  38. uphours = (upminutes / 60) % 24;
  39. upminutes %= 60;
  40. if (uphours)
  41. printf("%2d:%02d, ", uphours, upminutes);
  42. else
  43. printf("%d min, ", upminutes);
  44. printf("load average: %ld.%02ld, %ld.%02ld, %ld.%02ld\n",
  45. LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]),
  46. LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]),
  47. LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2]));
  48. return EXIT_SUCCESS;
  49. }