Annotation of researchv10no/cmd/file.c, revision 1.1.1.1

1.1       root        1: static char sccsid[] = "@(#)file.c 4.1 10/1/80";
                      2: /*
                      3:  * file - determine type of file
                      4:  */
                      5: 
                      6: #include <pagsiz.h>
                      7: #include <sys/types.h>
                      8: #include <sys/stat.h>
                      9: #include <stdio.h>
                     10: #include <ctype.h>
                     11: #include <a.out.h>
                     12: int in;
                     13: int i  = 0;
                     14: unsigned char buf[BUFSIZ];
                     15: char *troff[] = {      /* new troff intermediate lang */
                     16:        "x","T","res","init","font","202","V0","p1",0};
                     17: char *fort[] = {
                     18:        "function","subroutine","common","dimension","block","integer",
                     19:        "real","data","double",0};
                     20: char *asc[] = {
                     21:        "chmk","mov","tst","clr","jmp",0};
                     22: char *c[] = {
                     23:        "int","char","float","double","struct",
                     24:        "extern","static","typedef",0};
                     25: char *as[] = {
                     26:        "globl","byte","align","text","data","comm",0};
                     27: int    ifile;
                     28: 
                     29: main(argc, argv)
                     30: char **argv;
                     31: {
                     32:        FILE *fl;
                     33:        register char *p;
                     34:        char ap[128];
                     35:        extern char _sobuf[];
                     36: 
                     37:        if (argc>1 && argv[1][0]=='-' && argv[1][1]=='f') {
                     38:                if ((fl = fopen(argv[2], "r")) == NULL) {
                     39:                        printf("Can't open %s\n", argv[2]);
                     40:                        exit(2);
                     41:                }
                     42:                while ((p = fgets(ap, 128, fl)) != NULL) {
                     43:                        int l = strlen(p);
                     44:                        if (l>0)
                     45:                                p[l-1] = '\0';
                     46:                        printf("%s:     ", p);
                     47:                        type(p);
                     48:                        if (ifile>=0)
                     49:                                close(ifile);
                     50:                }
                     51:                exit(1);
                     52:        }
                     53:        while(argc > 1) {
                     54:                printf("%s:     ", argv[1]);
                     55:                type(argv[1]);
                     56:                fflush(stdout);
                     57:                argc--;
                     58:                argv++;
                     59:                if (ifile >= 0)
                     60:                        close(ifile);
                     61:        }
                     62: }
                     63: 
                     64: type(file)
                     65: char *file;
                     66: {
                     67:        int j,nl;
                     68:        char ch;
                     69:        struct stat mbuf;
                     70: 
                     71:        ifile = -1;
                     72:        if(stat(file, &mbuf) < 0) {
                     73:                if(lstat(file,&mbuf)>=0 && (mbuf.st_mode&S_IFMT)==S_IFLNK)
                     74:                        printf("symbolic link, cannot stat\n");
                     75:                else
                     76:                        printf("cannot stat\n");
                     77:                return;
                     78:        }
                     79:        switch (mbuf.st_mode & S_IFMT) {
                     80: 
                     81:        case S_IFCHR:
                     82:                printf("character");
                     83:                goto spcl;
                     84: 
                     85:        case S_IFDIR:
                     86:                printf("directory\n");
                     87:                return;
                     88: 
                     89:        case S_IFBLK:
                     90:                printf("block");
                     91: 
                     92: spcl:
                     93:                printf(" special (%d/%d)\n", major(mbuf.st_rdev), minor(mbuf.st_rdev));
                     94:                return;
                     95:        }
                     96: 
                     97:        ifile = open(file, 0);
                     98:        if(ifile < 0) {
                     99:                printf("cannot open\n");
                    100:                return;
                    101:        }
                    102:        in = read(ifile, buf, BUFSIZ);
                    103:        if(in == 0){
                    104:                printf("empty\n");
                    105:                return;
                    106:        }
                    107: 
                    108:        if(in>=64){
                    109: 
                    110:                int i, bucket[8];
                    111:                float cs;
                    112: 
                    113:                for(i=0; i<8; i++) bucket[i] = 0;
                    114: 
                    115:                for(i=0; i<64; i++) bucket[(buf[i]>>5)&07] += 1;
                    116: 
                    117:                cs = 0.;
                    118:                for(i=0; i<8; i++) cs += (bucket[i]-8)*(bucket[i]-8);
                    119:                cs /= 8.;
                    120: 
                    121:                if(cs <= 24.322){
                    122: 
                    123:                        if(buf[0]==037 && buf[1]==0235) {
                    124:                                char * p = file + strlen(file);
                    125:                                if(p[-1]=='Z' && p[-2]=='.') {
                    126:                                        printf("compressed\n");
                    127:                                        return;
                    128:                                }
                    129:                        }
                    130:                        printf("encrypted\n");
                    131:                        return;
                    132:                }
                    133:        }
                    134: 
                    135:        switch (*(short *) buf)
                    136:        {
                    137:        case 070707:
                    138: cpio:
                    139:                printf("cpio archive\n");
                    140:                goto out;
                    141:        }
                    142: 
                    143:        switch(*(int *)buf)
                    144:        {
                    145:        case 0413:
                    146:                printf("demand paged ");
                    147: 
                    148:        case 0410:
                    149:                printf("pure ");
                    150:                goto exec;
                    151: 
                    152:        case 0411:
                    153:                printf("jfr 411 executable\n");
                    154:                return;
                    155:        
                    156:        case 0406:
                    157:                printf ("mpx 68000 ");
                    158:                goto exec;
                    159: 
                    160:        case 0407:
                    161: exec:
                    162:                printf("executable");
                    163:                if(((int *)buf)[4] != 0) {
                    164:                        printf(" not stripped");
                    165:                        if(oldo(buf))
                    166:                                printf(" (old format symbol table)");
                    167:                }
                    168:                if(*(int *)buf==0413 && ((int *)buf)[1]-((int *)buf)[5]==0x11F)
                    169:                        printf(" infected");
                    170:                printf("\n");
                    171:                goto out;
                    172: 
                    173:        case 0177555:
                    174:                printf("very old archive\n");
                    175:                goto out;
                    176: 
                    177:        case 0177545:
                    178:                printf("old archive\n");
                    179:                goto out;
                    180: 
                    181:        case 0135246:           /* andrew/ehg */
                    182:                printf("view2d input file\n");
                    183:                goto out;
                    184: 
                    185:        case 0135256:           /* andrew */
                    186:                printf("apl file\n");
                    187:                goto out;
                    188: 
                    189:        case 0164200:           /* td */
                    190:                printf("Lucasfilm picture\n");
                    191:                goto out;
                    192: 
                    193:        case 0600560:
                    194:                printf("mux downloadable file\n");
                    195:                goto out;
                    196: 
                    197:        default:
                    198:                switch (*(short *) buf+1) {
                    199:                case 0405:
                    200:                case 0407:
                    201:                case 0410:
                    202:                case 0411:
                    203:                        printf ("pdp-11 executable\n");
                    204:                        goto out;
                    205:                }
                    206:        }
                    207: 
                    208:        if (in >= 32 && buf[0] == 0x00 && buf[1] == 0x00) {
                    209:                if (buf[2] == 0x01 && buf[3] == 0x07) {
                    210:                        printf("68020 plan9 executable\n");
                    211:                        goto out;
                    212:                }
                    213:                if (buf[2] == 0x04 && buf[3] == 0x07) {
                    214:                        printf("mips plan9 executable\n");
                    215:                        goto out;
                    216:                }
                    217:        }
                    218:        if(strncmp(buf, "!<arch>\n__.SYMDEF", 17) == 0 ) {
                    219:                printf("archive random library\n");
                    220:                goto out;
                    221:        }
                    222:        if (strncmp(buf, "!<arch>\n", 8)==0) {
                    223:                printf("archive\n");
                    224:                goto out;
                    225:        }
                    226:        if (strncmp(buf, "070707", 6) == 0)
                    227:        {
                    228:                printf("ascii ");
                    229:                goto cpio;
                    230:        }
                    231:        if(strncmp(buf, "#!/bin/echo ", 12)==0){
                    232:                printf("cyntax object file\n");
                    233:                goto out;
                    234:        }
                    235:        if(strncmp(buf, "#!", 2)==0) {
                    236:                if(script(buf,in))
                    237:                        goto out;
                    238:        }
                    239:        if(strncmp(buf, "TYPE=", 5)==0){        /* td */
                    240:                for(i=5;i!=in && buf[i]!='\n';i++);
                    241:                printf("%.*s picture\n", i, buf);
                    242:                goto out;
                    243:        }
                    244:        if(strncmp(buf, "%!", 2)==0){
                    245:                printf("postscript\n");
                    246:                goto out;
                    247:        }
                    248:        if (strncmp(buf, "@document(", 10) == 0) {
                    249:                printf("imagen\n");
                    250:                goto out;
                    251:        }
                    252:        if (strncmp(buf, "\0334\033\037\013", 5) == 0) {
                    253:                printf("daisy\n");
                    254:                goto out;
                    255:        }
                    256:        if(buf[0] == '\0') {
                    257:                if(buf[1] == '\0'){
                    258:                        printf("bitmap\n");
                    259:                        goto out;
                    260:                }
                    261:                if(strncmp(buf+1,"S data\001", 7)==0){
                    262:                        printf("S data object\n");
                    263:                        goto out;
                    264:                }
                    265:        }
                    266:        if( (((unsigned char)buf[0])==247) && (buf[1]==2) ){
                    267:                printf("tex dvi\n");
                    268:                goto out;
                    269:        }
                    270: 
                    271:        i = 0;
                    272:        if(ccom() == 0)goto notc;
                    273:        while(buf[i] == '#'){
                    274:                j = i;
                    275:                while(buf[i++] != '\n'){
                    276:                        if(i - j > 255){
                    277:                                printf("data\n"); 
                    278:                                goto out;
                    279:                        }
                    280:                        if(i >= in)goto notc;
                    281:                }
                    282:                if(ccom() == 0)goto notc;
                    283:        }
                    284: check:
                    285:        if(lookup(c) == 1){
                    286:                while((ch = buf[i++]) != ';' && ch != '{')if(i >= in)goto notc;
                    287:                printf("c program text");
                    288:                goto outa;
                    289:        }
                    290:        nl = 0;
                    291:        while(buf[i] != '('){
                    292:                if(buf[i] <= 0)
                    293:                        goto notas;
                    294:                if(buf[i] == ';'){
                    295:                        i++; 
                    296:                        goto check; 
                    297:                }
                    298:                if(buf[i++] == '\n')
                    299:                        if(nl++ > 6)goto notc;
                    300:                if(i >= in)goto notc;
                    301:        }
                    302:        while(buf[i] != ')'){
                    303:                if(buf[i++] == '\n')
                    304:                        if(nl++ > 6)goto notc;
                    305:                if(i >= in)goto notc;
                    306:        }
                    307:        while(buf[i] != '{'){
                    308:                if(buf[i++] == '\n')
                    309:                        if(nl++ > 6)goto notc;
                    310:                if(i >= in)goto notc;
                    311:        }
                    312:        printf("c program text");
                    313:        goto outa;
                    314: notc:
                    315:        i = 0;
                    316:        while(buf[i] == 'c' || buf[i] == '#'){
                    317:                while(buf[i++] != '\n')if(i >= in)goto notfort;
                    318:        }
                    319:        if(lookup(fort) == 1){
                    320:                printf("fortran program text");
                    321:                goto outa;
                    322:        }
                    323: notfort:
                    324:        i=0;
                    325:        if(ascom() == 0)goto notas;
                    326:        j = i-1;
                    327:        if(buf[i] == '.'){
                    328:                i++;
                    329:                if(lookup(as) == 1){
                    330:                        printf("assembler program text"); 
                    331:                        goto outa;
                    332:                }
                    333:                else if(buf[j] == '\n' && isalpha(buf[j+2])){
                    334:                        printf("roff, nroff, or eqn input text");
                    335:                        goto outa;
                    336:                }
                    337:        }
                    338:        while(lookup(asc) == 0){
                    339:                if(ascom() == 0)goto notas;
                    340:                while(buf[i] != '\n' && buf[i++] != ':')
                    341:                        if(i >= in)goto notas;
                    342:                while(buf[i] == '\n' || buf[i] == ' ' || buf[i] == '\t')if(i++ >= in)goto notas;
                    343:                j = i-1;
                    344:                if(buf[i] == '.'){
                    345:                        i++;
                    346:                        if(lookup(as) == 1){
                    347:                                printf("assembler program text"); 
                    348:                                goto outa; 
                    349:                        }
                    350:                        else if(buf[j] == '\n' && isalpha(buf[j+2])){
                    351:                                printf("roff, nroff, or eqn input text");
                    352:                                goto outa;
                    353:                        }
                    354:                }
                    355:        }
                    356:        printf("assembler program text");
                    357:        goto outa;
                    358: notas:
                    359:        for(i=0; i < in; i++)if(buf[i]&0200){
                    360:                if (buf[0]=='\100' && buf[1]=='\357') {
                    361:                        printf("troff (CAT) output\n");
                    362:                        goto out;
                    363:                }
                    364:                if(buf[0] == 037 && buf[1] == 036){
                    365:                        char *p = file;
                    366:                        while(*p++);
                    367:                        if(*(p-2) != 'z' || *(p-3) != '.')
                    368:                                printf("probably ");
                    369:                        printf("packed\n");
                    370:                        goto out;
                    371:                }
                    372:                if(buf[0] == 037 && buf[1] == 0235) {
                    373:                        char *p = file;
                    374:                        while(*p++);
                    375:                        if(*(p-2) != 'Z' || *(p-3) != '.')
                    376:                                printf("probably ");
                    377:                        printf("compressed\n");
                    378:                        goto out;
                    379:                }
                    380:                printf("data\n"); 
                    381:                goto out; 
                    382:        }
                    383:        if (mbuf.st_mode&((S_IEXEC)|(S_IEXEC>>3)|(S_IEXEC>>6)))
                    384:                printf("shell script text");
                    385:        else if (troffint(buf, in))
                    386:                ;
                    387:        else if (english(buf, in))
                    388:                printf("English text");
                    389:        else
                    390:                printf("ascii text");
                    391: outa:
                    392:        while(i < in)
                    393:                if((buf[i++]&0377) > 127){
                    394:                        printf(" with garbage\n");
                    395:                        goto out;
                    396:                }
                    397:        /* if next few lines in then read whole file looking for nulls ...
                    398:                while((in = read(ifile,buf,BUFSIZ)) > 0)
                    399:                        for(i = 0; i < in; i++)
                    400:                                if((buf[i]&0377) > 127){
                    401:                                        printf(" with garbage\n");
                    402:                                        goto out;
                    403:                                }
                    404:                /*.... */
                    405:        printf("\n");
                    406: out:;
                    407: }
                    408: 
                    409: oldo(cp)
                    410: char *cp;
                    411: {
                    412:        struct exec ex;
                    413:        struct stat stb;
                    414: 
                    415:        ex = *(struct exec *)cp;
                    416:        if (fstat(ifile, &stb) < 0)
                    417:                return(0);
                    418:        if (N_STROFF(ex)+sizeof(off_t) > stb.st_size)
                    419:                return (1);
                    420:        return (0);
                    421: }
                    422: 
                    423: script(buf,n)
                    424: char *buf;
                    425: {
                    426:        int i,j;
                    427:        int text = 1;
                    428:        for(j=2; j<n; j++)
                    429:                if(buf[j]!=' '&&buf[j]!='\t')
                    430:                        break;
                    431:        for(i=j; i<n; i++)
                    432:                if(buf[i]==' ' || buf[i]=='\n')
                    433:                        goto found;
                    434:        return 0;
                    435: found:
                    436:        buf[i] = 0;
                    437:        for(i=0; i<n; i++)
                    438:                if(buf[i]>=0177)
                    439:                        text = 0;
                    440:        printf("%s script%s\n",buf+j, text?" text":"");
                    441:        return 1;
                    442: }      
                    443: 
                    444: troffint(bp, n)
                    445: char *bp;
                    446: int n;
                    447: {
                    448:        int k, l;
                    449:        char s[14];
                    450: 
                    451:        i = 0;
                    452:        for (k = 0; k < 8; k++) {
                    453:                if (buf[i] == 'x' && buf[i+1] == ' ') { /* x [T|init|font|...] */
                    454:                        if (strncmp(&buf[i+2], "T ", 2) == 0) {
                    455:                                l = i;
                    456:                                while (l < n && buf[l++] != '\n');
                    457:                                strncpy(s, &buf[i+4], l - (i+4) - 1);
                    458:                                printf("troff intermediate output text for %s",s);
                    459:                                i = l;
                    460:                                return 1;
                    461:                        }
                    462:                        while (i < n && buf[i++] != '\n')
                    463:                                ;
                    464:                } else if ((buf[i] == 'f' || buf[i] == 's' ||
                    465:                          buf[i] == 'p' || buf[i] == 'V' || buf[i] == 'H')
                    466:                                && isdigit(buf[i+1]))   /* p1 | f2 | s3 | V4... */
                    467:                        while (i < n && buf[i++] != '\n')
                    468:                                ;
                    469:                else
                    470:                        return 0;
                    471:        }
                    472:        printf("troff intermediate output text");
                    473:        return 1;
                    474: }
                    475: 
                    476: lookup(tab)
                    477: char *tab[];
                    478: {
                    479:        char r;
                    480:        int k,j,l;
                    481:        while(buf[i] == ' ' || buf[i] == '\t' || buf[i] == '\n')i++;
                    482:        for(j=0; tab[j] != 0; j++){
                    483:                l=0;
                    484:                for(k=i; ((r=tab[j][l++]) == buf[k] && r != '\0');k++);
                    485:                if(r == '\0')
                    486:                        if(buf[k] == ' ' || buf[k] == '\n' || buf[k] == '\t'
                    487:                            || buf[k] == '{' || buf[k] == '/'){
                    488:                                i=k;
                    489:                                return(1);
                    490:                        }
                    491:        }
                    492:        return(0);
                    493: }
                    494: ccom(){
                    495:        char cc;
                    496:        while((cc = buf[i]) == ' ' || cc == '\t' || cc == '\n')if(i++ >= in)return(0);
                    497:        if(buf[i] == '/' && buf[i+1] == '*'){
                    498:                i += 2;
                    499:                while(buf[i] != '*' || buf[i+1] != '/'){
                    500:                        if(buf[i] == '\\')i += 2;
                    501:                        else i++;
                    502:                        if(i >= in)return(0);
                    503:                }
                    504:                if((i += 2) >= in)return(0);
                    505:        }
                    506:        if(buf[i] == '\n')if(ccom() == 0)return(0);
                    507:        return(1);
                    508: }
                    509: ascom(){
                    510:        while(buf[i] == '/'){
                    511:                i++;
                    512:                while(buf[i++] != '\n')if(i >= in)return(0);
                    513:                while(buf[i] == '\n')if(i++ >= in)return(0);
                    514:        }
                    515:        return(1);
                    516: }
                    517: 
                    518: english (bp, n)
                    519: char *bp;
                    520: {
                    521: # define NASC 128
                    522:        int ct[NASC], j, vow, freq, rare;
                    523:        int badpun = 0, punct = 0;
                    524:        if (n<50) return(0); /* no point in statistics on squibs */
                    525:        for(j=0; j<NASC; j++)
                    526:                ct[j]=0;
                    527:        for(j=0; j<n; j++)
                    528:        {
                    529:                if (bp[j]<NASC)
                    530:                        ct[bp[j]|040]++;
                    531:                switch (bp[j])
                    532:                {
                    533:                case '.': 
                    534:                case ',': 
                    535:                case ')': 
                    536:                case '%':
                    537:                case ';': 
                    538:                case ':': 
                    539:                case '?':
                    540:                        punct++;
                    541:                        if ( j < n-1 &&
                    542:                            bp[j+1] != ' ' &&
                    543:                            bp[j+1] != '\n')
                    544:                                badpun++;
                    545:                }
                    546:        }
                    547:        if (badpun*5 > punct)
                    548:                return(0);
                    549:        vow = ct['a'] + ct['e'] + ct['i'] + ct['o'] + ct['u'];
                    550:        freq = ct['e'] + ct['t'] + ct['a'] + ct['i'] + ct['o'] + ct['n'];
                    551:        rare = ct['v'] + ct['j'] + ct['k'] + ct['q'] + ct['x'] + ct['z'];
                    552:        if (2*ct[';'] > ct['e']) return(0);
                    553:        if ( (ct['>']+ct['<']+ct['/'])>ct['e']) return(0); /* shell file test */
                    554:        return (vow*5 >= n-ct[' '] && freq >= 10*rare);
                    555: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.