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

unix.superglobalmegacorp.com

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