]> granicus.if.org Git - file/commitdiff
PR/197: Add string trimming patch.
authorChristos Zoulas <christos@zoulas.com>
Thu, 6 Sep 2012 14:42:39 +0000 (14:42 +0000)
committerChristos Zoulas <christos@zoulas.com>
Thu, 6 Sep 2012 14:42:39 +0000 (14:42 +0000)
doc/magic.man
magic/Magdir/filesystems
src/apprentice.c
src/file.h
src/softmagic.c

index 399d78405b8e8433bb74114b01bdb736dcc0a1b9..d442abbb87694f9c37a662092f3656c47431b5b4 100644 (file)
@@ -1,5 +1,5 @@
-.\" $File: magic.man,v 1.73 2012/08/26 10:08:29 christos Exp $
-.Dd April 3, 2012
+.\" $File: magic.man,v 1.74 2012/09/06 14:42:39 christos Exp $
+.Dd September 6, 2012
 .Dt MAGIC __FSECTION__
 .Os
 .\" install as magic.4 on USG, magic.5 on V7, Berkeley and Linux systems.
@@ -51,7 +51,7 @@ A 64-bit double precision IEEE floating point number in this machine's native by
 .It Dv string
 A string of bytes.
 The string type specification can be optionally followed
-by /[WwcCtb]*.
+by /[WwcCtbT]*.
 The
 .Dq W
 flag compacts whitespace in the target, which must
@@ -85,6 +85,10 @@ The
 flag forces the test to be done for text files, while the
 .Dq b
 flag forces the test to be done for binary files.
+The
+.Dq T
+flag causes the string to be trimmed, i.e. leading and trailing whitespace
+is deleted before the string is printed.
 .It Dv pstring
 A Pascal-style string where the first byte/short/int is interpreted as the
 unsigned length.
index af9695b9a42a7374114f826adbfc3159be4f7794..f26168ca62b6e3e6ed5dd17173f80c817e0f2ca7 100644 (file)
@@ -1,6 +1,6 @@
 
 #------------------------------------------------------------------------------
-# $File: filesystems,v 1.61 2011/01/10 14:01:10 christos Exp $
+# $File: filesystems,v 1.62 2012/09/06 14:42:39 christos Exp $
 # filesystems:  file(1) magic for different filesystems
 #
 0      string  \366\366\366\366        PC formatted floppy with no filesystem
 >>38917        byte     >0x33      (unknown version, ID 0x%X)
 >>38917        byte     <0x31      (unknown version, ID 0x%X)
 # "application id" which appears to be used as a volume label
->32808 string    >\0       '%s'
+>32808 string/T  >\0       '%s'
 >34816 string    \000CD001\001EL\ TORITO\ SPECIFICATION    (bootable)
 37633  string    CD001     ISO 9660 CD-ROM filesystem data (raw 2352 byte sectors)
 !:mime application/x-iso9660-image
index b1d190b9bcecbd0ea607d949ba7e9f16e09312bf..b7aab7bc759fcbb3f6900c7db18faba0d4130be4 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: apprentice.c,v 1.175 2012/04/06 21:14:01 christos Exp $")
+FILE_RCSID("@(#)$File: apprentice.c,v 1.176 2012/09/06 14:42:39 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -1437,6 +1437,9 @@ parse(struct magic_set *ms, struct magic_entry **mentryp, uint32_t *nmentryp,
                                case CHAR_TEXTTEST:
                                        m->str_flags |= STRING_TEXTTEST;
                                        break;
+                               case CHAR_TRIM:
+                                       m->str_flags |= STRING_TRIM;
+                                       break;
                                case CHAR_PSTRING_1_LE:
                                        if (m->type != FILE_PSTRING)
                                                goto bad;
index 761fdae1f597e6c2e66d7b729b4a44189386fe9c..7afa18e546ba65e5445bd44e5504d2b94b143c8e 100644 (file)
@@ -27,7 +27,7 @@
  */
 /*
  * file.h - definitions for file(1) program
- * @(#)$File: file.h,v 1.138 2012/06/20 22:33:43 christos Exp $
+ * @(#)$File: file.h,v 1.139 2012/09/06 14:42:39 christos Exp $
  */
 
 #ifndef __file_h__
@@ -319,12 +319,14 @@ struct magic {
 #define PSTRING_LEN    \
     (PSTRING_1_BE|PSTRING_2_LE|PSTRING_2_BE|PSTRING_4_LE|PSTRING_4_BE)
 #define PSTRING_LENGTH_INCLUDES_ITSELF         BIT(12)
+#define        STRING_TRIM                             BIT(13)
 #define CHAR_COMPACT_WHITESPACE                        'W'
 #define CHAR_COMPACT_OPTIONAL_WHITESPACE       'w'
 #define CHAR_IGNORE_LOWERCASE                  'c'
 #define CHAR_IGNORE_UPPERCASE                  'C'
 #define CHAR_REGEX_OFFSET_START                        's'
 #define CHAR_TEXTTEST                          't'
+#define        CHAR_TRIM                               'T'
 #define CHAR_BINTEST                           'b'
 #define CHAR_PSTRING_1_BE                      'B'
 #define CHAR_PSTRING_1_LE                      'B'
index ae0532a5386321150513dd0843664d5ffd465a09..e76858ad26eb04b489bb73c8979eacbd599b2166 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.150 2012/05/15 17:14:36 christos Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.151 2012/09/06 14:42:39 christos Exp $")
 #endif /* lint */
 
 #include "magic.h"
@@ -448,11 +448,30 @@ mprint(struct magic_set *ms, struct magic *m)
                        t = ms->offset + m->vallen;
                }
                else {
+                       char *str = p->s;
+
+                       /* compute t before we mangle the string? */
+                       t = ms->offset + strlen(str);
+
                        if (*m->value.s == '\0')
-                               p->s[strcspn(p->s, "\n")] = '\0';
-                       if (file_printf(ms, m->desc, p->s) == -1)
+                               str[strcspn(str, "\n")] = '\0';
+
+                       if (m->str_flags & STRING_TRIM) {
+                               char *last;
+                               while (isspace((unsigned char)*str))
+                                       str++;
+                               last = str;
+                               while (*last)
+                                       last++;
+                               --last;
+                               while (isspace((unsigned char)*last))
+                                       last--;
+                               *++last = '\0';
+                       }
+
+                       if (file_printf(ms, m->desc, str) == -1)
                                return -1;
-                       t = ms->offset + strlen(p->s);
+
                        if (m->type == FILE_PSTRING)
                                t += file_pstring_length_size(m);
                }