commit 76dbc7d93740ad55fad3c0689476d15e5bc20682
Author: Jake Koroman <jakekoroman@gmail.com>
Date: Sat, 1 Jun 2024 16:51:22 -0400
Ready. Set. Go!
Diffstat:
A | README | | | 12 | ++++++++++++ |
A | build.sh | | | 5 | +++++ |
A | main.c | | | 102 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 119 insertions(+), 0 deletions(-)
diff --git a/README b/README
@@ -0,0 +1,11 @@
+img-gallary
+-----
+
+a simple program that generates an index.html to display
+images and videos in a directory.
+
+usage
+-----
+
+./build.sh
+./main [directory]
+\ No newline at end of file
diff --git a/build.sh b/build.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+set -xe
+
+cc -ggdb -Wall -Werror -pedantic -o main main.c
diff --git a/main.c b/main.c
@@ -0,0 +1,102 @@
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <dirent.h>
+#include <sys/types.h>
+
+#define OUTPUT_FNAME "index.html"
+#define OUTPUT_FNAME_LEN 11
+
+#define ELEMENT_STYLE "width='320' height='240'"
+
+typedef enum {
+ EXT_JPG,
+ EXT_JPEG,
+ EXT_PNG,
+ EXT_WEBP,
+ EXT_MP4,
+ EXT_MPV,
+ EXT_MOV,
+ EXT_UNKNOWN,
+} FileExt;
+
+static FileExt
+fileext(char *fname)
+{
+ assert(fname);
+ FileExt result = EXT_UNKNOWN;
+
+ int d = 0;
+ int n = strnlen(fname, 255);
+ for (int i = 0; i < n; ++i) {
+ if (fname[i] == '.')
+ d = i;
+ }
+
+ char *ext = &fname[d];
+
+ if (!strncmp(ext, ".jpg", 255))
+ result = EXT_JPG;
+ else if (!strncmp(ext, ".jpeg", 255))
+ result = EXT_JPEG;
+ else if (!strncmp(ext, ".png", 255))
+ result = EXT_PNG;
+ else if (!strncmp(ext, ".webp", 255))
+ result = EXT_WEBP;
+ else if (!strncmp(ext, ".mp4", 255))
+ result = EXT_MP4;
+ else if (!strncmp(ext, ".mpv", 255))
+ result = EXT_MPV;
+ else if (!strncmp(ext, ".mov", 255))
+ result = EXT_MOV;
+
+ return result;
+}
+
+int
+main(int argc, char **argv)
+{
+ DIR *dir = opendir(argc > 1 ? argv[1] : "./");
+ struct dirent *db = NULL;
+
+ FILE *fp = fopen(OUTPUT_FNAME, "w");
+ assert(fp);
+
+ fprintf(fp, "<head>\n\t<title>DOCTORS HATE ME FOR THIS ONE SIMPLE TRICK!!!</title>\n</head>\n");
+
+ fprintf(fp, "<body>\n");
+ while ((db = readdir(dir)) != NULL) {
+ /* NOTE(jake): this may not work on all filesystems ... may have to call lstat */
+ if (db->d_type == DT_DIR)
+ continue;
+ if (!strncmp(db->d_name, OUTPUT_FNAME, OUTPUT_FNAME_LEN))
+ continue;
+
+ switch (fileext(db->d_name)) {
+ case EXT_JPG:
+ case EXT_JPEG:
+ case EXT_PNG:
+ case EXT_WEBP:
+ fprintf(fp, "\t<img "ELEMENT_STYLE" src='%s'/>\n", db->d_name);
+ break;
+
+ case EXT_MP4:
+ case EXT_MPV:
+ case EXT_MOV:
+ fprintf(fp, "\t<video "ELEMENT_STYLE" controls>\n");
+ fprintf(fp, "\t\t<source src='%s'>\n", db->d_name);
+ fprintf(fp, "\t</video>\n");
+ break;
+
+ case EXT_UNKNOWN:
+ fprintf(stdout, "[INFO]: skipping unknown file extenstion from file %s\n", db->d_name);
+ break;
+ }
+ }
+ fprintf(fp, "</body>\n");
+
+ fprintf(stdout, "created %s\n", OUTPUT_FNAME);
+
+ return 0;
+}