35FILE_RCSID(
"@(#)$File: is_csv.c,v 1.13 2023/07/17 16:08:17 christos Exp $")
47#define DPRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
49#define DPRINTF(fmt, ...)
64static int csv_parse(
const unsigned char *,
const unsigned char *);
66static const unsigned char *
67eatquote(
const unsigned char *uc,
const unsigned char *ue)
72 unsigned char c = *uc++;
92csv_parse(
const unsigned char *uc,
const unsigned char *ue)
94 size_t nf = 0, tf = 0, nl = 0;
100 uc = eatquote(uc, ue);
106 DPRINTF(
"%zu %zu %zu\n", nl, nf, tf);
110 return tf != 0 && tf == nf;
118 }
else if (tf != nf) {
128 return tf && nl >= 2;
136 const unsigned char *uc =
CAST(
const unsigned char *, b->
fbuf);
137 const unsigned char *ue = uc + b->
flen;
146 if (!csv_parse(uc, ue))
158 if (
file_printf(ms,
"CSV %s%stext", code ? code :
"",
159 code ?
" " :
"") == -1)
167#include <sys/types.h>
177main(
int argc,
char *argv[])
183 if ((
fd = open(argv[1], O_RDONLY)) == -1)
184 err(EXIT_FAILURE,
"Can't open `%s'", argv[1]);
187 err(EXIT_FAILURE,
"Can't stat `%s'", argv[1]);
189 if ((
p =
CAST(
char *, malloc(st.st_size))) ==
NULL)
190 err(EXIT_FAILURE,
"Can't allocate %jd bytes",
191 (intmax_t)st.st_size);
192 if (read(
fd,
p, st.st_size) != st.st_size)
193 err(EXIT_FAILURE,
"Can't read %jd bytes",
194 (intmax_t)st.st_size);
195 printf(
"is csv %d\n", csv_parse(
p,
p + st.st_size));
printf(string $format, mixed ... $values)
file_protected int file_printf(struct magic_set *, const char *,...) __attribute__((__format__(__printf__
int file_is_csv(struct magic_set *ms, const struct buffer *b, int looks_text, const char *code)
#define MAGIC_MIME_ENCODING