]> granicus.if.org Git - file/commitdiff
PR/85: Add a simple CSV detector
authorChristos Zoulas <christos@zoulas.com>
Sat, 8 Jun 2019 22:16:24 +0000 (22:16 +0000)
committerChristos Zoulas <christos@zoulas.com>
Sat, 8 Jun 2019 22:16:24 +0000 (22:16 +0000)
doc/file.man
doc/libmagic.man
src/Makefile.am
src/file.c
src/file.h
src/funcs.c
src/magic.h.in

index 63e95f1365fffb15fb7e2b8c9a01ac38cd193f35..112d4aff0087d0302028a0c568f1d76dfd47ac23 100644 (file)
@@ -1,5 +1,5 @@
-.\" $File: file.man,v 1.135 2019/03/03 02:32:40 christos Exp $
-.Dd February 18, 2019
+.\" $File: file.man,v 1.136 2019/06/08 22:16:24 christos Exp $
+.Dd June 8, 2019
 .Dt FILE __CSECTION__
 .Os
 .Sh NAME
@@ -212,6 +212,8 @@ Ignored for backwards compatibility.
 Prints details of Compound Document Files.
 .It compress
 Checks for, and looks inside, compressed files.
+.It csv
+Checks Comma Separated Value files.
 .It elf
 Prints ELF file details, provided soft magic tests are enabled and the
 elf magic is found.
index 086f0659f1133cc464f0c52ead17289c0cf56c69..b0cf0339f19daa28aeed9048322c655266cbb4cc 100644 (file)
@@ -1,4 +1,4 @@
-.\" $File: libmagic.man,v 1.44 2018/09/09 20:33:28 christos Exp $
+.\" $File: libmagic.man,v 1.45 2019/06/08 22:16:24 christos Exp $
 .\"
 .\" Copyright (c) Christos Zoulas 2003, 2018.
 .\" All Rights Reserved.
@@ -25,7 +25,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd August 18, 2018
+.Dd June 8, 2019
 .Dt LIBMAGIC 3
 .Os
 .Sh NAME
@@ -160,7 +160,9 @@ Don't check for various types of text files.
 .It Dv MAGIC_NO_CHECK_TOKENS
 Don't look for known tokens inside ascii files.
 .It Dv MAGIC_NO_CHECK_JSON
-Don't example JSON files.
+Don't examine JSON files.
+.It Dv MAGIC_NO_CHECK_CSV
+Don't examine CSV files.
 .El
 .Pp
 The
index 2fbefdbfcb5b39cb9258ba635ead3932572cc733..3f67f2cf34900bc03f73b9d7ea9dc96d7b74eec9 100644 (file)
@@ -8,8 +8,8 @@ AM_CPPFLAGS = -DMAGIC='"$(MAGIC)"'
 AM_CFLAGS = $(CFLAG_VISIBILITY) @WARNINGS@
 
 libmagic_la_SOURCES = buffer.c magic.c apprentice.c softmagic.c ascmagic.c \
-       encoding.c compress.c is_json.c is_tar.c readelf.c print.c fsmagic.c \
-       funcs.c file.h readelf.h tar.h apptype.c der.c der.h \
+       encoding.c compress.c is_csv.c is_json.c is_tar.c readelf.c print.c \
+       fsmagic.c funcs.c file.h readelf.h tar.h apptype.c der.c der.h \
        file_opts.h elfclass.h mygetopt.h cdf.c cdf_time.c readcdf.c cdf.h
 libmagic_la_LDFLAGS = -no-undefined -version-info 1:0:0
 if MINGW
index 5b60b95f42401babeede7e9eef910736be6f0f6c..11cb83ca43a000d74106304adb1b658fed183a9d 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: file.c,v 1.181 2019/03/28 20:54:03 christos Exp $")
+FILE_RCSID("@(#)$File: file.c,v 1.182 2019/06/08 22:16:24 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -124,6 +124,7 @@ private const struct {
        { "ascii",      MAGIC_NO_CHECK_ASCII },
        { "cdf",        MAGIC_NO_CHECK_CDF },
        { "compress",   MAGIC_NO_CHECK_COMPRESS },
+       { "csv",        MAGIC_NO_CHECK_CSV },
        { "elf",        MAGIC_NO_CHECK_ELF },
        { "encoding",   MAGIC_NO_CHECK_ENCODING },
        { "soft",       MAGIC_NO_CHECK_SOFT },
index 69a586ab73238e6dccb7e5d14f09ee0020ccf3f5..e3c88569ef5b1b27dc448e1276eff0f26eec2b6b 100644 (file)
@@ -27,7 +27,7 @@
  */
 /*
  * file.h - definitions for file(1) program
- * @(#)$File: file.h,v 1.206 2019/05/07 02:27:11 christos Exp $
+ * @(#)$File: file.h,v 1.207 2019/06/08 22:16:24 christos Exp $
  */
 
 #ifndef __file_h__
@@ -479,6 +479,7 @@ protected int file_ascmagic_with_encoding(struct magic_set *,
 protected int file_encoding(struct magic_set *, const struct buffer *,
     unichar **, size_t *, const char **, const char **, const char **);
 protected int file_is_json(struct magic_set *, const struct buffer *);
+protected int file_is_csv(struct magic_set *, const struct buffer *);
 protected int file_is_tar(struct magic_set *, const struct buffer *);
 protected int file_softmagic(struct magic_set *, const struct buffer *,
     uint16_t *, uint16_t *, int, int);
index 23e7f32e63d26bfa4e13ac4fa55f94198fe788d5..33df4194d3ef846ddaab02c274878e3ca27f013d 100644 (file)
@@ -27,7 +27,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: funcs.c,v 1.104 2019/05/07 02:27:11 christos Exp $")
+FILE_RCSID("@(#)$File: funcs.c,v 1.105 2019/06/08 22:16:24 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -283,6 +283,17 @@ file_buffer(struct magic_set *ms, int fd, struct stat *st,
                }
        }
 
+       /* Check if we have a CSV file */
+       if ((ms->flags & MAGIC_NO_CHECK_CSV) == 0) {
+               m = file_is_csv(ms, &b);
+               if ((ms->flags & MAGIC_DEBUG) != 0)
+                       (void)fprintf(stderr, "[try csv %d]\n", m);
+               if (m) {
+                       if (checkdone(ms, &rv))
+                               goto done;
+               }
+       }
+
        /* Check if we have a CDF file */
        if ((ms->flags & MAGIC_NO_CHECK_CDF) == 0) {
                m = file_trycdf(ms, &b);
index 0580fa21cceac11a560df2079507da29fa63b671..d189a73c248e9d3d0ed84888bbc5cab6629fc223 100644 (file)
@@ -56,6 +56,7 @@
 #define        MAGIC_NO_CHECK_ELF      0x0010000 /* Don't check for elf details */
 #define        MAGIC_NO_CHECK_TEXT     0x0020000 /* Don't check for text files */
 #define        MAGIC_NO_CHECK_CDF      0x0040000 /* Don't check for cdf files */
+#define MAGIC_NO_CHECK_CSV     0x0080000 /* Don't check for CSV files */
 #define        MAGIC_NO_CHECK_TOKENS   0x0100000 /* Don't check tokens */
 #define MAGIC_NO_CHECK_ENCODING 0x0200000 /* Don't check text encodings */
 #define MAGIC_NO_CHECK_JSON    0x0400000 /* Don't check for JSON files */
@@ -68,6 +69,7 @@
        MAGIC_NO_CHECK_APPTYPE  | \
        MAGIC_NO_CHECK_ELF      | \
        MAGIC_NO_CHECK_TEXT     | \
+       MAGIC_NO_CHECK_CSV      | \
        MAGIC_NO_CHECK_CDF      | \
        MAGIC_NO_CHECK_TOKENS   | \
        MAGIC_NO_CHECK_ENCODING | \