[BACK]Return to ascmagic.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / file

Annotation of src/usr.bin/file/ascmagic.c, Revision 1.3

1.3     ! millert     1: /*     $OpenBSD: ascmagic.c,v 1.2 1996/06/26 05:32:54 deraadt Exp $    */
        !             2:
1.1       deraadt     3: /*
                      4:  * ASCII magic -- file types that we know based on keywords
                      5:  * that can appear anywhere in the file.
                      6:  *
                      7:  * Copyright (c) Ian F. Darwin, 1987.
                      8:  * Written by Ian F. Darwin.
                      9:  *
                     10:  * This software is not subject to any license of the American Telephone
                     11:  * and Telegraph Company or of the Regents of the University of California.
                     12:  *
                     13:  * Permission is granted to anyone to use this software for any purpose on
                     14:  * any computer system, and to alter it and redistribute it freely, subject
                     15:  * to the following restrictions:
                     16:  *
                     17:  * 1. The author is not responsible for the consequences of use of this
                     18:  *    software, no matter how awful, even if they arise from flaws in it.
                     19:  *
                     20:  * 2. The origin of this software must not be misrepresented, either by
                     21:  *    explicit claim or by omission.  Since few users ever read sources,
                     22:  *    credits must appear in the documentation.
                     23:  *
                     24:  * 3. Altered versions must be plainly marked as such, and must not be
                     25:  *    misrepresented as being the original software.  Since few users
                     26:  *    ever read sources, credits must appear in the documentation.
                     27:  *
                     28:  * 4. This notice may not be removed or altered.
                     29:  */
                     30:
                     31: #include <stdio.h>
                     32: #include <string.h>
                     33: #include <ctype.h>
                     34: #include <stdlib.h>
                     35: #include <unistd.h>
                     36: #include "file.h"
                     37: #include "names.h"
                     38:
                     39: #ifndef        lint
1.3     ! millert    40: static char *moduleid = "$OpenBSD: ascmagic.c,v 1.2 1996/06/26 05:32:54 deraadt Exp $";
1.1       deraadt    41: #endif /* lint */
                     42:
                     43:                        /* an optimisation over plain strcmp() */
                     44: #define        STREQ(a, b)     (*(a) == *(b) && strcmp((a), (b)) == 0)
                     45:
                     46: int
                     47: ascmagic(buf, nbytes)
                     48: unsigned char *buf;
                     49: int nbytes;    /* size actually read */
                     50: {
                     51:        int i, has_escapes = 0;
                     52:        unsigned char *s;
                     53:        char nbuf[HOWMANY+1];   /* one extra for terminating '\0' */
                     54:        char *token;
                     55:        register struct names *p;
                     56:
                     57:        /*
                     58:         * Do the tar test first, because if the first file in the tar
                     59:         * archive starts with a dot, we can confuse it with an nroff file.
                     60:         */
                     61:        switch (is_tar(buf, nbytes)) {
                     62:        case 1:
                     63:                ckfputs("tar archive", stdout);
                     64:                return 1;
                     65:        case 2:
                     66:                ckfputs("POSIX tar archive", stdout);
                     67:                return 1;
                     68:        }
                     69:
                     70:        /*
                     71:         * for troff, look for . + letter + letter or .\";
                     72:         * this must be done to disambiguate tar archives' ./file
                     73:         * and other trash from real troff input.
                     74:         */
                     75:        if (*buf == '.') {
                     76:                unsigned char *tp = buf + 1;
                     77:
                     78:                while (isascii(*tp) && isspace(*tp))
                     79:                        ++tp;   /* skip leading whitespace */
                     80:                if ((isascii(*tp) && (isalnum(*tp) || *tp=='\\') &&
                     81:                    isascii(tp[1]) && (isalnum(tp[1]) || tp[1] == '"'))) {
                     82:                        ckfputs("troff or preprocessor input text", stdout);
                     83:                        return 1;
                     84:                }
                     85:        }
                     86:        if ((*buf == 'c' || *buf == 'C') &&
                     87:            isascii(buf[1]) && isspace(buf[1])) {
                     88:                ckfputs("fortran program text", stdout);
                     89:                return 1;
                     90:        }
                     91:
1.3     ! millert    92:
        !            93:        /* Make sure we are dealing with ascii text before looking for tokens */
        !            94:        for (i = 0; i < nbytes; i++) {
        !            95:                if (!isascii(buf[i]))
        !            96:                        return 0;       /* not all ASCII */
        !            97:        }
        !            98:
1.1       deraadt    99:        /* look for tokens from names.h - this is expensive! */
                    100:        /* make a copy of the buffer here because strtok() will destroy it */
                    101:        s = (unsigned char*) memcpy(nbuf, buf, nbytes);
                    102:        s[nbytes] = '\0';
                    103:        has_escapes = (memchr(s, '\033', nbytes) != NULL);
                    104:        while ((token = strtok((char *) s, " \t\n\r\f")) != NULL) {
                    105:                s = NULL;       /* make strtok() keep on tokin' */
                    106:                for (p = names; p < names + NNAMES; p++) {
                    107:                        if (STREQ(p->name, token)) {
                    108:                                ckfputs(types[p->type], stdout);
                    109:                                if (has_escapes)
                    110:                                        ckfputs(" (with escape sequences)",
                    111:                                                stdout);
                    112:                                return 1;
                    113:                        }
                    114:                }
                    115:        }
                    116:
                    117:        /* all else fails, but it is ASCII... */
                    118:        ckfputs("ASCII text", stdout);
                    119:        if (has_escapes) {
                    120:                ckfputs(" (with escape sequences)", stdout);
                    121:        }
                    122:        return 1;
                    123: }
                    124:
                    125: