]> 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 750a8407abccde0b581432b07e43079749a7cc5b..b237f73f81dc27bba4f5fb08f498d0811248ceef 100644 (file)
@@ -1,5 +1,5 @@
-.\" $File: magic.man,v 1.72 2012/04/03 22:25:07 christos Exp $
-.Dd April 3, 2012
+.\" $File: magic.man,v 1.73 2012/08/26 10:08:29 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 d5cf085698af419c88676f4d815432c575476b46..6e5cdccec7df66d60d0f49c66594c20a0238b565 100644 (file)
@@ -1,6 +1,6 @@
 
 #------------------------------------------------------------------------------
-# $File: filesystems,v 1.60 2010/09/20 21:11:35 rrt Exp $
+# $File: filesystems,v 1.61 2011/01/10 14:01:10 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 eccf5d074cc0d69acc3801081db77f23f7b776b1..57592c753eacd7dc5bde303e9a9f9bfb8826d2fd 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: apprentice.c,v 1.174 2012/04/03 22:25:07 christos Exp $")
+FILE_RCSID("@(#)$File: apprentice.c,v 1.175 2012/04/06 21:14:01 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 1ad67975f93277e18ca9103f815ab1a9a60469a8..07e4b6e0b9ef28da4984ea76ba6bca1e6569e619 100644 (file)
@@ -27,7 +27,7 @@
  */
 /*
  * file.h - definitions for file(1) program
- * @(#)$File: file.h,v 1.137 2012/05/15 17:14:36 christos Exp $
+ * @(#)$File: file.h,v 1.138 2012/06/20 22:33:43 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 784e32e9d0f75d12769b5eab62d63d147760767b..85fffed5802ed582f9ac90a5865a00047fea6646 100644 (file)
@@ -32,7 +32,7 @@
 #include "file.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.149 2012/04/06 21:15:54 christos Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.150 2012/05/15 17:14:36 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);
                }