3
0

uptime.c 2.4 KB

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