Annotation of 43BSD/contrib/dipress/src/bin/ipf/ipf.c.ucb, revision 1.1.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 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: # include "ipnames.h"
                     18: #else
                     19: # include <stdio.h>
                     20: # include <setjmp.h>
                     21: # include <ctype.h>
                     22: # include <sys/types.h>
                     23: # include <sys/time.h>
                     24: # include "iptokens.h"
                     25: # include "ipnames.h"
                     26: #endif
                     27: 
                     28: jmp_buf next_file;
                     29: 
                     30: extern int errno;
                     31: 
                     32: main(argc, argv)
                     33: 
                     34: int  argc;
                     35: char *argv[];
                     36: 
                     37: {
                     38:     FILE *acctFile = stdout;
                     39:     int c,
                     40:        pageCount;
                     41:     char *login,
                     42:         *host;
                     43:     extern int optind;
                     44:     extern char *optarg;
                     45:     char *getdate(), *date, *xtra;
                     46: 
                     47:     login = "";
                     48:     host = "";
                     49:     date = "";
                     50:     xtra = "";
                     51: 
                     52:     while ((c = getopt(argc, argv, "cdw:l:i:n:h:x:")) != EOF)
                     53:        switch (c) {
                     54:                case 'c':
                     55:                case 'w':
                     56:                case 'l':
                     57:                case 'i':
                     58:                        break;
                     59: 
                     60:                case 'n':
                     61:                        login = optarg;
                     62:                        break;
                     63: 
                     64:                case 'h':
                     65:                        host = optarg;
                     66:                        break;
                     67: 
                     68:                case 'x':
                     69:                        xtra = optarg;
                     70:                        break;
                     71: 
                     72:                case 'd':
                     73:                        date = getdate();
                     74:                        break;
                     75: 
                     76:                default:
                     77:                        printf("option '%c' not allowed\n");
                     78:        }
                     79: 
                     80:     pageCount = do_file(stdin);
                     81: 
                     82:     if( pageCount < 0 )
                     83:        exit(2);
                     84: 
                     85:     if (argc - optind == 1)
                     86:     {
                     87: 
                     88:        if( (acctFile = fopen(argv[optind], "a")) == NULL ) {
                     89:            fprintf(stderr, "ipf: can't open acct file: %s\n", argv[optind]);
                     90:            exit(2);
                     91:        }
                     92:     }
                     93: 
                     94:     fprintf(acctFile, "%s %s %d%s%s\n",
                     95:                host, login, pageCount + 1, date, xtra);
                     96: 
                     97:     exit(0);
                     98: 
                     99: }
                    100: 
                    101: 
                    102: /*
                    103:  * process one file
                    104:  */
                    105: 
                    106: do_file(file)
                    107: FILE *file;
                    108: {
                    109: # define  Buffsize     256
                    110:     char buff[Buffsize];
                    111:     char *ptr;
                    112:     int len;
                    113:     int bodyDepth;     /* how many bodies down we are */
                    114:     register int bodyCount,    /* how many bodies we have seen so far */
                    115:                 val,
                    116:                 byte;          /* has to be "int" for stdio EOF detection */
                    117:                                /* stdio is a pile! */
                    118:     int hlen;
                    119: 
                    120:     hlen = strlen(IP_Header);
                    121: 
                    122:     /* for error recovery */
                    123:     if (setjmp(next_file) != 0)
                    124:     {
                    125:        return -1;
                    126:     }
                    127: 
                    128:     /* get the header */
                    129:     for (hlen = 0, ptr = buff; hlen < Buffsize; hlen++)
                    130:     {
                    131:        if ((*ptr++ = getnoeofc(file)) == ' ')
                    132:            break;
                    133:     }
                    134:     *ptr = '\0';
                    135: 
                    136:     /* check the validity of the header */
                    137:     if (strcmp(buff, IP_Header) != 0)
                    138:     {
                    139:        fprintf(stderr, " (INVALID HEADER!)");
                    140:     }
                    141: 
                    142:     bodyDepth = 0;
                    143:     bodyCount = 0;
                    144: 
                    145:     /* main loop */
                    146:     while ((byte = getc(file)) != EOF)
                    147:     {
                    148:        if ((byte & 0200) == 0)
                    149:        {
                    150:            /* a short number */
                    151:            val = (byte << 8) + getnoeofc(file) - INTEGER_ZERO;
                    152:        }
                    153:        else
                    154:        {
                    155:            /* something else */
                    156:            switch(byte >> 5)
                    157:            {
                    158:                case (SHORT_OP >> 5):
                    159:                    break;
                    160: 
                    161:                case (LONG_OP >> 5):
                    162:                    val = ((byte & 037) << 8) + getnoeofc(file);
                    163:                    if( val == OP_beginBody )
                    164:                    {
                    165:                        bodyDepth++;
                    166:                    } 
                    167:                    else if( val == OP_endBody )
                    168:                    {
                    169:                        bodyDepth--;
                    170: 
                    171:                        /* is this a top level body? */
                    172:                        if( bodyDepth == 0 )
                    173:                            bodyCount++;
                    174:                    }
                    175:                    break;
                    176: 
                    177:                case (SHORT_SEQUENCE >> 5):
                    178:                    len = getnoeofc(file);
                    179:                    eatBytes(file, len);
                    180:                    break;
                    181: 
                    182:                case (LONG_SEQUENCE >> 5):
                    183:                    len  =  getnoeofc(file) << 16;
                    184:                    len += (getnoeofc(file) << 8);
                    185:                    len += getnoeofc(file);
                    186:                    eatBytes(file, len);
                    187:                    break;
                    188:            }
                    189:        }
                    190:     }
                    191: 
                    192:     return bodyCount - 1;      /* the preamble is an extra body */
                    193: }
                    194: 
                    195: 
                    196: /*
                    197:  * get a character
                    198:  */
                    199: 
                    200: getnoeofc(file)
                    201: 
                    202: FILE *file;
                    203: 
                    204: {
                    205:     register int val;
                    206: 
                    207: #ifdef vax11c
                    208:     val= getc(file);
                    209:     if ( feof(file) )
                    210: #else
                    211:     if ((val = getc(file)) == EOF)
                    212: #endif
                    213:     {
                    214:        fprintf(stderr, "Unexpected EOF!");
                    215:        longjmp(next_file, 1);
                    216:     }
                    217:     return(val);
                    218: }
                    219: 
                    220: 
                    221: /*
                    222:  * read some bytes from the input stream
                    223:  */
                    224: 
                    225: eatBytes(file, length)
                    226: FILE *file;
                    227: int length;
                    228: 
                    229: {
                    230:     register int count;
                    231: 
                    232:     count = length;
                    233: 
                    234:     while(count-- > 0)
                    235:     {
                    236:        (void) getnoeofc(file);
                    237:     }
                    238: }
                    239: 
                    240: char *
                    241: getdate()
                    242: {
                    243:        static char buf[30];
                    244: #ifdef vax11c
                    245:        *buf = 0;
                    246: #else
                    247:        struct timeval tv;
                    248:        struct timezone tz;
                    249:        struct tm *tmp;
                    250:         
                    251:        gettimeofday(&tv, &tz);
                    252:        tmp = localtime(&tv.tv_sec);
                    253:        sprintf(buf, " %d/%d-%02d:%02d",
                    254:                tmp->tm_mon+1, tmp->tm_mday, tmp->tm_hour, tmp->tm_min);
                    255: #endif
                    256:        return buf;
                    257: }

unix.superglobalmegacorp.com

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