commit 8533b3f31397e52afc7ada17e8529629569f9998
parent acff4a471145bb9e9842e0d0881cd9fd35b67936
Author: bsandro <[email protected]>
Date: Thu, 20 Jul 2023 08:35:55 +0300
Handling different directories and respecting PATH_MAX from limits.h
Diffstat:
2 files changed, 23 insertions(+), 9 deletions(-)
diff --git a/Makefile b/Makefile
@@ -27,5 +27,5 @@ run: $(NAME)
@./$(NAME)
test: $(NAME)
- @./$(NAME) FlanClap.webp 5
+ @./$(NAME) FlanClap.webp 7
feh atlas_FlanClap.webp
diff --git a/src/main.c b/src/main.c
@@ -2,15 +2,27 @@
* Based on the libwebp example program "anim_dump"
*/
+#define _GNU_SOURCE
+
#include <stdio.h>
#include "webp/decode.h"
#include "webp/encode.h"
#include "webp/demux.h"
#include <stdbool.h>
#include <stdlib.h>
+#include <libgen.h>
+#include <string.h>
#include <strings.h>
#include <assert.h>
+#ifdef __OpenBSD__
+#include <sys/limits.h>
+#endif
+
+#ifdef __linux__
+#include <linux/limits.h>
+#endif
+
#define NUM_CHANNELS 4
static void print_webp_version() {
@@ -194,17 +206,19 @@ int main(int argc, const char **argv) {
return 0;
}
- char *outname = calloc(255, 1);
- assert(outname!=NULL);
AnimatedImage img = {0};
- const char *inname = argv[1];
+ char out_name[PATH_MAX];
+ char *in_path = strndup(argv[1], PATH_MAX-1);
+ assert(in_path!=NULL);
int cols = atoi(argv[2]);
- assert(read_webp(inname, &img) == 0);
- int n = snprintf(outname, 254, "atlas_%s", inname);
+ int r = read_webp(in_path, &img);
+ assert(r==0);
+ char *in_name = basename((char *)in_path);
+ char *in_dir = dirname((char *)in_path);
+ int n = snprintf(out_name, NAME_MAX, "atlas_%s", in_name);
assert(n>0);
- printf("[%s -> %s(%d)]\ndimensions: %dx%d\nframes: %d\n", inname, outname, cols, img.width, img.height, img.frame_count);
- write_webp(outname, &img, cols);
- free(outname);
+ printf("[path:%s][%s -> %s(%d)]\ndimensions: %dx%d\nframes: %d\n", in_dir, in_name, out_name, cols, img.width, img.height, img.frame_count);
+ write_webp(out_name, &img, cols);
return 0;
}