summaryrefslogtreecommitdiff
path: root/array.c
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-08-01 09:29:36 -0400
committerJakob Kaivo <jkk@ung.org>2019-08-01 09:29:36 -0400
commit3cdfc5819b7cdfb4007ed5150f78b5ab697cf583 (patch)
tree175ef7ed9aecd3dcb34ad1d57e707a430feaab4e /array.c
parent8ec89c5a53466a3d0689a406c0604cf9bdbe7640 (diff)
initial commit
Diffstat (limited to 'array.c')
-rw-r--r--array.c85
1 files changed, 85 insertions, 0 deletions
diff --git a/array.c b/array.c
new file mode 100644
index 0000000..6ad54db
--- /dev/null
+++ b/array.c
@@ -0,0 +1,85 @@
+#define _POSIX_C_SOURCE 2
+#include <ctype.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#define CHARS_PER_LINE 8
+
+int make_array(const char *path, int include_size)
+{
+ FILE *f = stdin;
+
+ if (path && strcmp(path, "-") != 0) {
+ f = fopen(path, "rb");
+ if (f == NULL) {
+ perror(path);
+ return 1;
+ }
+ } else {
+ path = "stdin";
+ }
+
+ size_t plen = strlen(path);
+ char id[plen + 1];
+ for (size_t i = 0; i < plen; i++) {
+ id[i] = isalnum(path[i]) ? path[i] : '_';
+ }
+ id[plen] = '\0';
+
+ printf("unsigned char %s[] = {\n", id);
+
+ int c;
+ int i = 0;
+ while ((c = fgetc(f)) != EOF) {
+ if (i == 0) {
+ putchar('\t');
+ }
+ printf("0x%02hhx, ", c);
+ if (++i == CHARS_PER_LINE) {
+ putchar('\n');
+ i = 0;
+ }
+ }
+
+ if (i != 0) {
+ putchar('\n');
+ }
+
+ printf("};\n");
+
+ if (include_size) {
+ printf("size_t %s_size = sizeof(%s);\n", id, id);
+ }
+
+ if (f != stdin) {
+ fclose(f);
+ }
+
+ return 0;
+}
+
+
+int main(int argc, char *argv[])
+{
+ int include_size = 0;
+ int c;
+
+ while ((c = getopt(argc, argv, "s")) != -1) {
+ switch (c) {
+ case 's':
+ include_size = 1;
+ break;
+
+ default:
+ return 1;
+ }
+ }
+
+ int ret = 0;
+ do {
+ ret |= make_array(argv[optind++], include_size);
+ } while (optind < argc);
+
+ return ret;
+}