uptime.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. /* This version of uptime doesn't display the number of users on the system,
  23. * since busybox init doesn't mess with utmp. For folks using utmp that are
  24. * just dying to have # of users reported, feel free to write it as some type
  25. * of CONFIG_FEATURE_UTMP_SUPPORT #define
  26. */
  27. /* getopt not needed */
  28. #include <stdio.h>
  29. #include <time.h>
  30. #include <errno.h>
  31. #include <stdlib.h>
  32. #include "busybox.h"
  33. static const int FSHIFT = 16; /* nr of bits of precision */
  34. #define FIXED_1 (1<<FSHIFT) /* 1.0 as fixed-point */
  35. #define LOAD_INT(x) ((x) >> FSHIFT)
  36. #define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)
  37. extern int uptime_main(int argc, char **argv)
  38. {
  39. int updays, uphours, upminutes;
  40. struct sysinfo info;
  41. struct tm *current_time;
  42. time_t current_secs;
  43. time(&current_secs);
  44. current_time = localtime(&current_secs);
  45. sysinfo(&info);
  46. printf(" %02d:%02d:%02d up ",
  47. current_time->tm_hour, current_time->tm_min, current_time->tm_sec);
  48. updays = (int) info.uptime / (60*60*24);
  49. if (updays)
  50. printf("%d day%s, ", updays, (updays != 1) ? "s" : "");
  51. upminutes = (int) info.uptime / 60;
  52. uphours = (upminutes / 60) % 24;
  53. upminutes %= 60;
  54. if(uphours)
  55. printf("%2d:%02d, ", uphours, upminutes);
  56. else
  57. printf("%d min, ", upminutes);
  58. printf("load average: %ld.%02ld, %ld.%02ld, %ld.%02ld\n",
  59. LOAD_INT(info.loads[0]), LOAD_FRAC(info.loads[0]),
  60. LOAD_INT(info.loads[1]), LOAD_FRAC(info.loads[1]),
  61. LOAD_INT(info.loads[2]), LOAD_FRAC(info.loads[2]));
  62. return EXIT_SUCCESS;
  63. }