aboutsummaryrefslogtreecommitdiff
path: root/contrib/file/src/is_csv.c
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/file/src/is_csv.c')
-rw-r--r--contrib/file/src/is_csv.c19
1 files changed, 10 insertions, 9 deletions
diff --git a/contrib/file/src/is_csv.c b/contrib/file/src/is_csv.c
index 0081088c80ec..7b95e3b85164 100644
--- a/contrib/file/src/is_csv.c
+++ b/contrib/file/src/is_csv.c
@@ -32,7 +32,7 @@
#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: is_csv.c,v 1.4 2019/06/26 20:31:31 christos Exp $")
+FILE_RCSID("@(#)$File: is_csv.c,v 1.13 2023/07/17 16:08:17 christos Exp $")
#endif
#include <string.h>
@@ -94,8 +94,7 @@ csv_parse(const unsigned char *uc, const unsigned char *ue)
size_t nf = 0, tf = 0, nl = 0;
while (uc < ue) {
- unsigned char c;
- switch (c = *uc++) {
+ switch (*uc++) {
case '"':
// Eat until the matching quote
uc = eatquote(uc, ue);
@@ -126,12 +125,13 @@ csv_parse(const unsigned char *uc, const unsigned char *ue)
break;
}
}
- return tf && nl > 2;
+ return tf && nl >= 2;
}
#ifndef TEST
int
-file_is_csv(struct magic_set *ms, const struct buffer *b, int looks_text)
+file_is_csv(struct magic_set *ms, const struct buffer *b, int looks_text,
+ const char *code)
{
const unsigned char *uc = CAST(const unsigned char *, b->fbuf);
const unsigned char *ue = uc + b->flen;
@@ -150,12 +150,13 @@ file_is_csv(struct magic_set *ms, const struct buffer *b, int looks_text)
return 1;
if (mime) {
- if (file_printf(ms, "application/csv") == -1)
+ if (file_printf(ms, "text/csv") == -1)
return -1;
return 1;
}
- if (file_printf(ms, "CSV text") == -1)
+ if (file_printf(ms, "CSV %s%stext", code ? code : "",
+ code ? " " : "") == -1)
return -1;
return 1;
@@ -175,7 +176,7 @@ file_is_csv(struct magic_set *ms, const struct buffer *b, int looks_text)
int
main(int argc, char *argv[])
{
- int fd, rv;
+ int fd;
struct stat st;
unsigned char *p;
@@ -185,7 +186,7 @@ main(int argc, char *argv[])
if (fstat(fd, &st) == -1)
err(EXIT_FAILURE, "Can't stat `%s'", argv[1]);
- if ((p = malloc(st.st_size)) == NULL)
+ if ((p = CAST(char *, malloc(st.st_size))) == NULL)
err(EXIT_FAILURE, "Can't allocate %jd bytes",
(intmax_t)st.st_size);
if (read(fd, p, st.st_size) != st.st_size)