util.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * nmrpflash - Netgear Unbrick Utility
  3. * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
  4. *
  5. * nmrpflash is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * nmrpflash is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with nmrpflash. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include <stdio.h>
  20. #include <errno.h>
  21. #include <time.h>
  22. #include <math.h>
  23. #include "nmrpd.h"
  24. #ifdef NMRPFLASH_OSX
  25. #include <mach/mach_time.h>
  26. #endif
  27. volatile sig_atomic_t g_interrupted = 0;
  28. int verbosity = 0;
  29. time_t time_monotonic()
  30. {
  31. #ifndef NMRPFLASH_WINDOWS
  32. #ifndef NMRPFLASH_OSX
  33. struct timespec ts;
  34. clock_gettime(CLOCK_MONOTONIC, &ts);
  35. return ts.tv_sec;
  36. #else
  37. static double factor = 0.0;
  38. mach_timebase_info_data_t timebase;
  39. if (factor == 0.0) {
  40. mach_timebase_info(&timebase);
  41. factor = (double)timebase.numer / timebase.denom;
  42. }
  43. return round(mach_absolute_time() * factor / 1e9);
  44. #endif
  45. #else
  46. return round(GetTickCount() / 1000.0);
  47. #endif
  48. }
  49. char *lltostr(long long ll, int base)
  50. {
  51. static char buf[32];
  52. snprintf(buf, sizeof(buf) - 1, (base == 16 ? "%llx" : (base == 8 ? "%llo" : "%lld")), ll);
  53. return buf;
  54. }
  55. const char *mac_to_str(uint8_t *mac)
  56. {
  57. static char buf[18];
  58. snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
  59. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  60. return buf;
  61. }
  62. uint32_t bitcount(uint32_t n)
  63. {
  64. uint32_t c;
  65. for (c = 0; n; ++c) {
  66. n &= n - 1;
  67. }
  68. return c;
  69. }
  70. uint32_t netmask(uint32_t count)
  71. {
  72. return htonl(count <= 32 ? 0xffffffff << (32 - count) : 0);
  73. }
  74. int select_fd(int fd, unsigned timeout)
  75. {
  76. struct timeval tv;
  77. int status;
  78. fd_set fds;
  79. FD_ZERO(&fds);
  80. FD_SET(fd, &fds);
  81. tv.tv_sec = timeout / 1000;
  82. tv.tv_usec = 1000 * (timeout % 1000);
  83. status = select(fd + 1, &fds, NULL, NULL, &tv);
  84. if (status < 0) {
  85. sock_perror("select");
  86. }
  87. return status;
  88. }
  89. void xperror(const char *msg)
  90. {
  91. if (errno != EINTR) {
  92. perror(msg);
  93. } else {
  94. printf("\n");
  95. }
  96. }