]> 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 ed7d146ef6e32899e1216d4678221e9b3c68dab4..b9b2b2228b22c94ccaf254d94803d9544002243d 100644 (file)
@@ -46,7 +46,7 @@
 #endif
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: apprentice.c,v 1.106 2007/10/23 19:58:59 christos Exp $")
+FILE_RCSID("@(#)$File: apprentice.c,v 1.107 2007/11/08 00:31:37 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 b4c96dadc5cef0004ced7edad743ed2cbc127b24..782b61cfc82fe66deb9e7eef440013edafc533da 100644 (file)
@@ -71,7 +71,7 @@
 #include "patchlevel.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: file.c,v 1.115 2007/10/25 15:33:17 christos Exp $")
+FILE_RCSID("@(#)$File: file.c,v 1.116 2007/10/29 00:54:08 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 5885ff39b32ac5110424c5bbc5e205b40030bf52..54e3dc18984a52415e08a1291f0c4f01f1d87d1d 100644 (file)
@@ -48,7 +48,7 @@
 #endif
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: funcs.c,v 1.33 2007/06/15 00:01:15 christos Exp $")
+FILE_RCSID("@(#)$File: funcs.c,v 1.34 2007/10/17 19:33:31 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 11cfb20e1f238826ba32d43a6f8e7a1cbb79cc06..1fd288aadf0c4370f89d080f4dc81c74300323b5 100644 (file)
@@ -63,7 +63,7 @@
 #include "patchlevel.h"
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: magic.c,v 1.43 2007/09/26 20:45:26 christos Exp $")
+FILE_RCSID("@(#)$File: magic.c,v 1.44 2007/10/17 19:33:31 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 e3241dbba179882e0ee56a5e37b5632ccc0bd4f1..4ccd4857bd5844cc5d328c07ccb6afa5aa50478b 100644 (file)
@@ -41,7 +41,7 @@
 #include <time.h>
 
 #ifndef lint
-FILE_RCSID("@(#)$File: print.c,v 1.59 2007/03/05 02:41:29 christos Exp $")
+FILE_RCSID("@(#)$File: print.c,v 1.60 2007/11/08 00:31:37 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 609dcc525162dfb31cace042e44728297dd8896c..5bdb457c6f21cdcb8671f74740c0412a7d5b00da 100644 (file)
@@ -38,7 +38,7 @@
 
 
 #ifndef        lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.101 2007/10/17 19:33:31 christos Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.102 2007/11/08 00:31:37 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);