Annotation of 43BSDTahoe/new/dipress/src/bin/ipf/ipf.c, revision 1.1

1.1     ! root        1: /*
        !             2:  *  Interpress utility - count the number of pages in a interpress file
        !             3:  *
        !             4:  *  Written for Xerox Corporation by Lee Moore & William LeFebvre
        !             5:  *
        !             6:  * Copyright (c) 1984, 1985, 1986 Xerox Corp.
        !             7:  *
        !             8:  * History:
        !             9:  *      2-sep-85 lee moore     created out of iptotext.c
        !            10:  */
        !            11: 
        !            12: #ifdef vax11c
        !            13: # include stdio
        !            14: # include setjmp
        !            15: # include ctype
        !            16: # include "iptokens.h"
        !            17: #else
        !            18: # include <stdio.h>
        !            19: # include <setjmp.h>
        !            20: # include <ctype.h>
        !            21: # include "iptokens.h"
        !            22: #endif
        !            23: 
        !            24: jmp_buf next_file;
        !            25: 
        !            26: extern int errno;
        !            27: 
        !            28: main(argc, argv)
        !            29: 
        !            30: int  argc;
        !            31: char *argv[];
        !            32: 
        !            33: {
        !            34:     FILE *acctFile;
        !            35:     int c,
        !            36:        pageCount;
        !            37:     char *login,
        !            38:         *host;
        !            39:     extern int optind;
        !            40:     extern char *optarg;
        !            41: 
        !            42:     login = NULL;
        !            43:     host = NULL;
        !            44: 
        !            45:     while ((c = getopt(argc, argv, "cw:l:i:n:h:")) != EOF)
        !            46:        switch (c) {
        !            47:                case 'c':
        !            48:                case 'w':
        !            49:                case 'l':
        !            50:                case 'i':
        !            51:                        break;
        !            52: 
        !            53:                case 'n':
        !            54:                        login = optarg;
        !            55:                        break;
        !            56: 
        !            57:                case 'h':
        !            58:                        host = optarg;
        !            59:                        break;
        !            60: 
        !            61:                default:
        !            62:                        printf("option '%c' not allowed\n");
        !            63:        }
        !            64: 
        !            65:     if (argc - optind == 1)
        !            66:     {
        !            67:        pageCount = do_file(stdin);
        !            68: 
        !            69:        if( pageCount < 0 )
        !            70:            exit(2);
        !            71: 
        !            72:        if( (acctFile = fopen(argv[optind], "a")) == NULL ) {
        !            73:            fprintf(stderr, "ipf: can't open acct file: %s\n", argv[optind]);
        !            74:            exit(2);
        !            75:        }
        !            76: 
        !            77:        if( login )
        !            78:            if( host )
        !            79:                fprintf(acctFile, "%d\t%s:%s\n", pageCount, host, login);
        !            80:            else
        !            81:                fprintf(acctFile, "%d\t%s\n", pageCount, login);
        !            82: 
        !            83:        exit(0);
        !            84:     }
        !            85: 
        !            86: }
        !            87: 
        !            88: 
        !            89: /*
        !            90:  * process one file
        !            91:  */
        !            92: 
        !            93: do_file(file)
        !            94: FILE *file;
        !            95: {
        !            96: # define  Buffsize     256
        !            97:     char buff[Buffsize];
        !            98:     char *ptr;
        !            99:     int len;
        !           100:     int bodyDepth;     /* how many bodies down we are */
        !           101:     register int bodyCount,    /* how many bodies we have seen so far */
        !           102:                 val,
        !           103:                 byte;          /* has to be "int" for stdio EOF detection */
        !           104:                                /* stdio is a pile! */
        !           105:     int hlen;
        !           106: 
        !           107:     hlen = strlen(IP_Header);
        !           108: 
        !           109:     /* for error recovery */
        !           110:     if (setjmp(next_file) != 0)
        !           111:     {
        !           112:        return -1;
        !           113:     }
        !           114: 
        !           115:     /* get the header */
        !           116:     for (hlen = 0, ptr = buff; hlen < Buffsize; hlen++)
        !           117:     {
        !           118:        if ((*ptr++ = getnoeofc(file)) == ' ')
        !           119:            break;
        !           120:     }
        !           121:     *ptr = '\0';
        !           122: 
        !           123:     /* check the validity of the header */
        !           124:     if (strcmp(buff, IP_Header) != 0)
        !           125:     {
        !           126:        fprintf(stderr, " (INVALID HEADER!)");
        !           127:     }
        !           128: 
        !           129:     bodyDepth = 0;
        !           130:     bodyCount = 0;
        !           131: 
        !           132:     /* main loop */
        !           133:     while ((byte = getc(file)) != EOF)
        !           134:     {
        !           135:        if ((byte & 0200) == 0)
        !           136:        {
        !           137:            /* a short number */
        !           138:            val = (byte << 8) + getnoeofc(file) - INTEGER_ZERO;
        !           139:        }
        !           140:        else
        !           141:        {
        !           142:            /* something else */
        !           143:            switch(byte >> 5)
        !           144:            {
        !           145:                case (SHORT_OP >> 5):
        !           146:                    break;
        !           147: 
        !           148:                case (LONG_OP >> 5):
        !           149:                    val = ((byte & 037) << 8) + getnoeofc(file);
        !           150:                    if( val == OP_beginBody )
        !           151:                    {
        !           152:                        bodyDepth++;
        !           153:                    } 
        !           154:                    else if( val == OP_endBody )
        !           155:                    {
        !           156:                        bodyDepth--;
        !           157: 
        !           158:                        /* is this a top level body? */
        !           159:                        if( bodyDepth == 0 )
        !           160:                            bodyCount++;
        !           161:                    }
        !           162:                    break;
        !           163: 
        !           164:                case (SHORT_SEQUENCE >> 5):
        !           165:                    len = getnoeofc(file);
        !           166:                    eatBytes(file, len);
        !           167:                    break;
        !           168: 
        !           169:                case (LONG_SEQUENCE >> 5):
        !           170:                    len  =  getnoeofc(file) << 16;
        !           171:                    len += (getnoeofc(file) << 8);
        !           172:                    len += getnoeofc(file);
        !           173:                    eatBytes(file, len);
        !           174:                    break;
        !           175:            }
        !           176:        }
        !           177:     }
        !           178: 
        !           179:     return bodyCount - 1;      /* the preamble is an extra body */
        !           180: }
        !           181: 
        !           182: 
        !           183: /*
        !           184:  * get a character
        !           185:  */
        !           186: 
        !           187: getnoeofc(file)
        !           188: 
        !           189: FILE *file;
        !           190: 
        !           191: {
        !           192:     register int val;
        !           193: 
        !           194: #ifdef vax11c
        !           195:     val= getc(file);
        !           196:     if ( feof(file) )
        !           197: #else
        !           198:     if ((val = getc(file)) == EOF)
        !           199: #endif
        !           200:     {
        !           201:        fprintf(stderr, "Unexpected EOF!");
        !           202:        longjmp(next_file, 1);
        !           203:     }
        !           204:     return(val);
        !           205: }
        !           206: 
        !           207: 
        !           208: /*
        !           209:  * read some bytes from the input stream
        !           210:  */
        !           211: 
        !           212: eatBytes(file, length)
        !           213: FILE *file;
        !           214: int length;
        !           215: 
        !           216: {
        !           217:     register int count;
        !           218: 
        !           219:     count = length;
        !           220: 
        !           221:     while(count-- > 0)
        !           222:     {
        !           223:        (void) getnoeofc(file);
        !           224:     }
        !           225: }

unix.superglobalmegacorp.com

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