scripts: qemustart: support CONFIG_BINARY_FOLDER
[openwrt/staging/stintel.git] / scripts / mkhash.c
index 0f5815f242f6694cf8506e8936767fa804f923f9..a28d5fd5372ff396d7c289e64a8a36345b1a05d0 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)
 {
@@ -124,6 +131,7 @@ be32dec(const void *buf)
 
        return (((uint32_t) be16dec(p)) << 16) | be16dec(p + 2);
 }
+#endif
 
 #define MD5_DIGEST_LENGTH      16
 
@@ -736,7 +744,11 @@ static int usage(const char *progname)
 {
        int i;
 
-       fprintf(stderr, "Usage: %s <hash type> [<file>...]\n"
+       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);
 
        for (i = 0; i < ARRAY_SIZE(types); i++)
@@ -760,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) {
@@ -783,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;
 }
 
@@ -795,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);
                }
@@ -818,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;
 }