build: preserve profiles.json between builds
[openwrt/staging/dedeckeh.git] / scripts / mkhash.c
index e26ca3a350148aa5ec500a5dee459f7b31f749cd..ed3d42d4c35c2dc6d8c165fc386c4be8f90d77bf 100644 (file)
 
 
 
+#ifndef __FreeBSD__
 #include <endian.h>
+#else
+#include <sys/endian.h>
+#endif
+
 #include <stdio.h>
 #include <string.h>
 #include <stdint.h>
 #include <stdbool.h>
 #include <unistd.h>
+#include <sys/stat.h>
 
 #define ARRAY_SIZE(_n) (sizeof(_n) / sizeof((_n)[0]))
 
+#ifndef __FreeBSD__
 static void
 be32enc(void *buf, uint32_t u)
 {
@@ -98,6 +105,7 @@ be32enc(void *buf, uint32_t u)
        p[2] = ((uint8_t) ((u >> 8) & 0xff));
        p[3] = ((uint8_t) (u & 0xff));
 }
+#endif
 
 static void
 be64enc(void *buf, uint64_t u)
@@ -739,6 +747,7 @@ static int usage(const char *progname)
        fprintf(stderr, "Usage: %s <hash type> [options] [<file>...]\n"
                "Options:\n"
                "       -n              Print filename(s)\n"
+               "       -N              Suppress trailing newline\n"
                "\n"
                "Supported hash types:", progname);
 
@@ -763,13 +772,21 @@ static struct hash_type *get_hash_type(const char *name)
 }
 
 
-static int hash_file(struct hash_type *t, const char *filename, bool add_filename)
+static int hash_file(struct hash_type *t, const char *filename, bool add_filename,
+       bool no_newline)
 {
        const char *str;
 
        if (!filename || !strcmp(filename, "-")) {
                str = t->func(stdin);
        } else {
+               struct stat path_stat;
+               stat(filename, &path_stat);
+               if (S_ISDIR(path_stat.st_mode)) {
+                       fprintf(stderr, "Failed to open '%s': Is a directory\n", filename);
+                       return 1;
+               }
+
                FILE *f = fopen(filename, "r");
 
                if (!f) {
@@ -786,9 +803,10 @@ static int hash_file(struct hash_type *t, const char *filename, bool add_filenam
        }
 
        if (add_filename)
-               printf("%s %s\n", str, filename ? filename : "-");
+               printf("%s %s%s", str, filename ? filename : "-",
+                       no_newline ? "" : "\n");
        else
-               printf("%s\n", str);
+               printf("%s%s", str, no_newline ? "" : "\n");
        return 0;
 }
 
@@ -798,13 +816,16 @@ int main(int argc, char **argv)
        struct hash_type *t;
        const char *progname = argv[0];
        int i, ch;
-       bool add_filename = false;
+       bool add_filename = false, no_newline = false;
 
-       while ((ch = getopt(argc, argv, "n")) != -1) {
+       while ((ch = getopt(argc, argv, "nN")) != -1) {
                switch (ch) {
                case 'n':
                        add_filename = true;
                        break;
+               case 'N':
+                       no_newline = true;
+                       break;
                default:
                        return usage(progname);
                }
@@ -821,10 +842,13 @@ int main(int argc, char **argv)
                return usage(progname);
 
        if (argc < 2)
-               return hash_file(t, NULL, add_filename);
+               return hash_file(t, NULL, add_filename, no_newline);
 
-       for (i = 0; i < argc - 1; i++)
-               hash_file(t, argv[1 + i], add_filename);
+       for (i = 0; i < argc - 1; i++) {
+               int ret = hash_file(t, argv[1 + i], add_filename, no_newline);
+               if (ret)
+                       return ret;
+       }
 
        return 0;
 }