Update help text and readme
[oweals/nmrpflash.git] / util.c
diff --git a/util.c b/util.c
index 4422442dea94197604636dcd1f4e6d12fa947b3d..0d2aab9a00e5d913ceabf654441dc8d5a1843acf 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1,4 +1,24 @@
+/**
+ * nmrpflash - Netgear Unbrick Utility
+ * Copyright (C) 2016 Joseph Lehner <joseph.c.lehner@gmail.com>
+ *
+ * nmrpflash is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * nmrpflash is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with nmrpflash.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
 #include <stdio.h>
+#include <errno.h>
 #include <time.h>
 #include <math.h>
 #include "nmrpd.h"
@@ -7,6 +27,8 @@
 #include <mach/mach_time.h>
 #endif
 
+volatile sig_atomic_t g_interrupted = 0;
+
 time_t time_monotonic()
 {
 #ifndef NMRPFLASH_WINDOWS
@@ -35,3 +57,26 @@ char *lltostr(long long ll, int base)
        snprintf(buf, sizeof(buf) - 1, (base == 16 ? "%llx" : (base == 8 ? "%llo" : "%lld")), ll);
        return buf;
 }
+
+uint32_t bitcount(uint32_t n)
+{
+       uint32_t c;
+       for (c = 0; n; ++c) {
+               n &= n - 1;
+       }
+       return c;
+}
+
+uint32_t netmask(uint32_t count)
+{
+       return htonl(count <= 32 ? 0xffffffff << (32 - count) : 0);
+}
+
+void xperror(const char *msg)
+{
+       if (errno != EINTR) {
+               perror(msg);
+       } else {
+               printf("\n");
+       }
+}