]> granicus.if.org Git - file/commitdiff
Various cleanups from OpenBSD and gcc warnings.
authorChristos Zoulas <christos@zoulas.com>
Thu, 27 Dec 2007 16:35:58 +0000 (16:35 +0000)
committerChristos Zoulas <christos@zoulas.com>
Thu, 27 Dec 2007 16:35:58 +0000 (16:35 +0000)
src/apprentice.c
src/file.c
src/funcs.c
src/magic.c
src/print.c
src/softmagic.c

index 8b12853aef324fc6db9f246bef8c7253439fdd4b..094e4e64c4f9f1b924b37fb56260e42115a9f6c3 100644 (file)
@@ -46,7 +46,7 @@
 #endif
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: apprentice.c,v 1.107 2007/11/08 00:31:37 christos Exp $")
+FILE_RCSID("@(#)$File: apprentice.c,v 1.108 2007/12/27 16:35:58 christos Exp $")
 #endif /* lint */
 
 #define        EATAB {while (isascii((unsigned char) *l) && \
@@ -110,6 +110,7 @@ private int apprentice_compile(struct magic_set *, struct magic **, uint32_t *,
     const char *);
 private int check_format_type(const char *, int);
 private int check_format(struct magic_set *, struct magic *);
+private int get_op(char);
 
 private size_t maxmagic = 0;
 private size_t magicsize = sizeof(struct magic);
@@ -525,7 +526,7 @@ apprentice_file(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
        private const char hdr[] =
                "cont\toffset\ttype\topcode\tmask\tvalue\tdesc";
        FILE *f;
-       char line[BUFSIZ+1];
+       char line[BUFSIZ];
        int errs = 0;
        struct magic_entry *marray;
        uint32_t marraycount, i, mentrycount = 0;
@@ -554,7 +555,7 @@ apprentice_file(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp,
                (void)fprintf(stderr, "%s\n", hdr);
 
        /* read and parse this file */
-       for (ms->line = 1; fgets(line, BUFSIZ, f) != NULL; ms->line++) {
+       for (ms->line = 1; fgets(line, sizeof(line), f) != NULL; ms->line++) {
                size_t len;
                len = strlen(line);
                if (len == 0) /* null line, garbage, etc */
index fa2ce56e14275ee59f7a6737b4f41ce1865f0d81..575b951f817867cf96064907c6ed222c91d56983 100644 (file)
@@ -71,7 +71,7 @@
 #include "patchlevel.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: file.c,v 1.116 2007/10/29 00:54:08 christos Exp $")
+FILE_RCSID("@(#)$File: file.c,v 1.117 2007/12/27 16:35:58 christos Exp $")
 #endif /* lint */
 
 
@@ -122,7 +122,8 @@ private void load(const char *, int);
 int
 main(int argc, char *argv[])
 {
-       int c, i;
+       int c;
+       size_t i;
        int action = 0, didsomefiles = 0, errflg = 0;
        int flags = 0;
        char *home, *usermagic;
@@ -331,9 +332,9 @@ main(int argc, char *argv[])
                }
        }
        else {
-               int i, wid, nw;
-               for (wid = 0, i = optind; i < argc; i++) {
-                       nw = file_mbswidth(argv[i]);
+               size_t j, wid, nw;
+               for (wid = 0, j = (size_t)optind; j < (size_t)argc; j++) {
+                       nw = file_mbswidth(argv[j]);
                        if (nw > wid)
                                wid = nw;
                }
@@ -380,7 +381,6 @@ unwrap(char *fn)
        char buf[MAXPATHLEN];
        FILE *f;
        int wid = 0, cwid;
-       size_t len;
 
        if (strcmp("-", fn) == 0) {
                f = stdin;
@@ -393,9 +393,7 @@ unwrap(char *fn)
                }
 
                while (fgets(buf, MAXPATHLEN, f) != NULL) {
-                       len = strlen(buf);
-                       if (len > 0 && buf[len - 1] == '\n')
-                               buf[len - 1] = '\0';
+                       buf[strcspn(buf, "\n")] = '\0';
                        cwid = file_mbswidth(buf);
                        if (cwid > wid)
                                wid = cwid;
@@ -404,10 +402,8 @@ unwrap(char *fn)
                rewind(f);
        }
 
-       while (fgets(buf, MAXPATHLEN, f) != NULL) {
-               len = strlen(buf);
-               if (len > 0 && buf[len - 1] == '\n')
-                       buf[len - 1] = '\0';
+       while (fgets(buf, sizeof(buf), f) != NULL) {
+               buf[strcspn(buf, "\n")] = '\0';
                process(buf, wid);
                if(nobuffer)
                        (void)fflush(stdout);
index 706c4b5a01f28993ae7323941372e6841f90106a..5cf11812fb754e3d993cc1329894da4105b3a57b 100644 (file)
@@ -48,7 +48,7 @@
 #endif
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: funcs.c,v 1.34 2007/10/17 19:33:31 christos Exp $")
+FILE_RCSID("@(#)$File: funcs.c,v 1.35 2007/12/27 16:35:59 christos Exp $")
 #endif /* lint */
 
 #ifndef HAVE_VSNPRINTF
@@ -62,12 +62,16 @@ protected int
 file_printf(struct magic_set *ms, const char *fmt, ...)
 {
        va_list ap;
-       size_t len, size;
+       size_t size;
+       ssize_t len;
        char *buf;
 
        va_start(ap, fmt);
 
-       if ((len = vsnprintf(ms->o.ptr, ms->o.left, fmt, ap)) >= ms->o.left) {
+       len = vsnprintf(ms->o.ptr, ms->o.left, fmt, ap);
+       if (len == -1)
+               goto out;
+       if (len >= (ssize_t)ms->o.left) {
                long diff;      /* XXX: really ptrdiff_t */
 
                va_end(ap);
@@ -84,11 +88,16 @@ file_printf(struct magic_set *ms, const char *fmt, ...)
 
                va_start(ap, fmt);
                len = vsnprintf(ms->o.ptr, ms->o.left, fmt, ap);
+               if (len == -1)
+                       goto out;
        }
        va_end(ap);
        ms->o.ptr += len;
        ms->o.left -= len;
        return 0;
+out:
+       file_error(ms, errno, "vsnprintf failed");
+       return -1;
 }
 
 /*
index 3242f76a16ae10b83a90c1512b88049acb64010e..84d7c95d8bbf43f1cd98262277e5911ee9e1b82e 100644 (file)
@@ -63,7 +63,7 @@
 #include "patchlevel.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: magic.c,v 1.44 2007/10/17 19:33:31 christos Exp $")
+FILE_RCSID("@(#)$File: magic.c,v 1.45 2007/12/27 16:35:59 christos Exp $")
 #endif /* lint */
 
 #ifdef __EMX__
@@ -260,7 +260,6 @@ file_or_fd(struct magic_set *ms, const char *inname, int fd)
        struct stat     sb;
        ssize_t nbytes = 0;     /* number of bytes read from a datafile */
        int     ispipe = 0;
-       int     mime = ms->flags & MAGIC_MIME;
 
        /*
         * one extra for terminating '\0', and
index d41bb739f72866d866d544d966ac639c855f77bd..c28ee2e145f1649e43479a9a8f2801c424498cc1 100644 (file)
@@ -41,7 +41,7 @@
 #include <time.h>
 
 #ifndef lint
-FILE_RCSID("@(#)$File: print.c,v 1.60 2007/11/08 00:31:37 christos Exp $")
+FILE_RCSID("@(#)$File: print.c,v 1.61 2007/12/27 16:35:59 christos Exp $")
 #endif  /* lint */
 
 #define SZOF(a)        (sizeof(a) / sizeof(a[0]))
@@ -199,7 +199,7 @@ file_magwarn(struct magic_set *ms, const char *f, ...)
 protected const char *
 file_fmttime(uint32_t v, int local)
 {
-       char *pp, *rt;
+       char *pp;
        time_t t = (time_t)v;
        struct tm *tm;
 
@@ -229,7 +229,6 @@ file_fmttime(uint32_t v, int local)
                pp = asctime(tm);
        }
 
-       if ((rt = strchr(pp, '\n')) != NULL)
-               *rt = '\0';
+       pp[strcspn(pp, "\n")] = '\0';
        return pp;
 }
index ba2faa3832e623ff681046ca5bbdee038a5a79be..21a0148dd87fdc15415600451da5daeab635b84c 100644 (file)
@@ -38,7 +38,7 @@
 
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.102 2007/11/08 00:31:37 christos Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.103 2007/12/27 16:35:59 christos Exp $")
 #endif /* lint */
 
 private int match(struct magic_set *, struct magic *, uint32_t,
@@ -401,11 +401,8 @@ mprint(struct magic_set *ms, struct magic *m)
                        t = ms->offset + m->vallen;
                }
                else {
-                       if (*m->value.s == '\0') {
-                               char *cp = strchr(p->s,'\n');
-                               if (cp)
-                                       *cp = '\0';
-                       }
+                       if (*m->value.s == '\0')
+                               p->s[strcspn(p->s, "\n")] = '\0';
                        if (file_printf(ms, m->desc, p->s) == -1)
                                return -1;
                        t = ms->offset + strlen(p->s);