commit 4e496009616a878222acbad2366f2cb395563a31
parent 8d9e0a9e53a467e9a4685c27213a20d39cb2ab81
Author: Louis Burda <contact@sinitax.com>
Date: Sat, 22 Feb 2025 23:25:52 +0100
Add png format hint when extension is missing
Diffstat:
M | xsnip.c | | | 32 | ++++++++++++++++++++++++++------ |
1 file changed, 26 insertions(+), 6 deletions(-)
diff --git a/xsnip.c b/xsnip.c
@@ -6,6 +6,7 @@
#include <unistd.h>
#include <err.h>
+#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
@@ -16,7 +17,7 @@
static const char usage[] = "xsnip [-h] [-d DELAY] OUTFILE";
-static const char *filename;
+static const char *filepath;
static XWindowAttributes scr;
static Display *display;
@@ -69,6 +70,9 @@ void
saveimg(void)
{
Imlib_Image img;
+ const char *file;
+ const char *hint;
+ int fd;
imlib_context_set_display(display);
imlib_context_set_visual(DefaultVisual(display, 0));
@@ -77,7 +81,21 @@ saveimg(void)
img = imlib_create_image_from_drawable(0, rx, ry, rw, rh, 1);
imlib_context_set_image(img);
- imlib_save_image(filename);
+
+ fd = open(filepath, O_WRONLY | O_CREAT);
+ if (!fd) perror("open");
+
+ file = strrchr(filepath, '/');
+ file = file ? file + 1 : filepath;
+ hint = strchr(file, '.') ? file : "dsnip.png";
+ imlib_save_image_fd(fd, hint);
+ if (imlib_get_error()) {
+ fprintf(stderr, "xsnip: imlib_save_image: %s\n", imlib_strerror(imlib_get_error()));
+ exit(1);
+ }
+
+ close(fd);
+
imlib_free_image();
}
@@ -150,7 +168,7 @@ main(int argc, char **argv)
int delay;
delay = 0;
- filename = NULL;
+ filepath = NULL;
for (arg = &argv[1]; *arg; arg++) {
if (!strcmp(*arg, "-d")) {
delay = strtol(*(++arg), &end, 10);
@@ -159,15 +177,17 @@ main(int argc, char **argv)
} else if (!strcmp(*arg, "-h")) {
printf("Usage: %s\n", usage);
return 0;
- } else if (!filename) {
- filename = *arg;
+ } else if (!filepath) {
+ filepath = *arg;
+ if (!strcmp(filepath, "-"))
+ filepath = "/dev/stdout";
} else {
fprintf(stderr, "Usage: %s\n", usage);
return 1;
}
}
- if (!filename) {
+ if (!filepath) {
fprintf(stderr, "USAGE: %s\n", usage);
return 1;
}