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

unix.superglobalmegacorp.com

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