ref: 013a0cb2aa0b73d9d5e6748d4bccc0f04720433e
parent: 244d6dbe34069fb8f8c4cc61e5c4725537f0cfd6
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Wed Mar 13 20:25:52 EDT 2024
readtags example: add -i option to dump an image; use stderr more
--- a/examples/readtags.c
+++ b/examples/readtags.c
@@ -1,10 +1,13 @@
-#include <fcntl.h>
#include <stdio.h>
-#include <sys/types.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
#include <unistd.h>
-#define USED(x) (void)x
#include "tags.h"
+#define USED(x) (void)x
+
typedef struct Aux Aux;
struct Aux {
@@ -28,10 +31,25 @@
[Ttrackpeak] = "trackpeak",
};
+static bool image;
+
static void
tag(Tagctx *ctx, int t, const char *k, const char *v, int offset, int size, Tagread f)
{
- USED(ctx); USED(k); USED(f);
+ USED(k); USED(f);
+ if(image){
+ if(t != Timage)
+ return;
+ char *raw = malloc(size);
+ Aux *aux = ctx->aux;
+ if(read(aux->fd, raw, size) != size || (f != NULL && f(raw, &size) != 0)){
+ fprintf(stderr, "failed to read the image\n");
+ exit(1);
+ }
+ write(1, raw, size);
+ exit(0);
+ return;
+ }
if(t == Timage)
printf("%-12s %s %d %d\n", t2s[t], v, offset, size);
else if(t == Tunknown)
@@ -64,7 +82,7 @@
main(int argc, char **argv)
{
int i;
- char buf[256];
+ char buf[256], *argv0;
Aux aux;
Tagctx ctx = {
.read = ctxread,
@@ -76,8 +94,15 @@
.aux = &aux,
};
+ argv0 = argv[0];
+ if(argc > 1 && strcmp(argv[1], "-i") == 0){
+ image = true;
+ argc--;
+ argv++;
+ }
+
if(argc < 2){
- printf("usage: readtags FILE...\n");
+ fprintf(stderr, "usage: %s [-i] FILE...\n", argv0);
return 1;
}
@@ -86,9 +111,13 @@
if((aux.fd = open(argv[i], O_RDONLY)) < 0)
perror("failed to open");
else{
- if(tagsget(&ctx) != 0)
- printf("no tags or failed to read tags\n");
- else{
+ if(tagsget(&ctx) != 0){
+ fprintf(stderr, "no tags or failed to read tags\n");
+ return 1;
+ }else if(image){
+ fprintf(stderr, "no images found\n");
+ return 1;
+ }else{
if(ctx.duration > 0)
printf("%-12s %d ms\n", "duration", ctx.duration);
if(ctx.samplerate > 0)
@@ -100,7 +129,8 @@
}
close(aux.fd);
}
- printf("\n");
+ if(i+1 < argc)
+ printf("\n");
}
return 0;