Annotation of researchv10no/cmd/prefer/pref/streams.c, revision 1.1.1.1

1.1       root        1: # include "stdio.h"
                      2: # include "streams.h"
                      3: # include "ctype.h"
                      4: 
                      5: char *strncpy();
                      6: void exit();
                      7: 
                      8: #define IWORD  20
                      9: #define        IMAX    5
                     10: char igntab[IWORD][IMAX];
                     11: int igcnt;
                     12: 
                     13: /*  getword(stream,p):
                     14:         read next sequence of alpha-numberic on current line into *p.
                     15:     null if no more words on current line.
                     16:     *p is a null terminated string (char p[maxstr]).
                     17: */
                     18: getword(stream,p)
                     19: FILE *stream;
                     20: char *p;
                     21: {   
                     22:        char c;
                     23:        char *oldp, *stop;
                     24:        register int i;
                     25:        static ignored = 0;
                     26: 
                     27:        oldp= p;
                     28:        stop= p+maxstr-1;
                     29:        do { 
                     30:                c= getc(stream);
                     31:                if(c == EOF) {
                     32:                        *p = NULL;
                     33:                        return;
                     34:                }
                     35:        }   while (!isalnum(c) && c!='%');
                     36: 
                     37:        do {
                     38:                *p= c;
                     39:                if (p < stop)  p++;
                     40:                c= getc(stream);
                     41:        } while(isalnum(c));
                     42:        *p= NULL;
                     43:        if (oldp[0]=='%') {   
                     44:                oldp[0]= NULL;
                     45:                ignored = 0;
                     46:                for(i=0;i<igcnt;i++) {
                     47:                        if(strncmp(&igntab[i][0],&oldp[1],IMAX-1)== 0) {
                     48:                                ignored = 1;
                     49:                                while(c != '\n' && c != EOF) c=getc(stream);
                     50:                                break;
                     51:                        }
                     52:                }
                     53:        }
                     54:        else if (ignored) {
                     55:                oldp[0] = NULL;
                     56:                while(c != '\n' && c != EOF) c=getc(stream);
                     57:        }
                     58: }
                     59: 
                     60: 
                     61: 
                     62: /*  recsize(stream,start):
                     63:     returns length of record beginning at start
                     64:     (record ends at blank line or eof)
                     65:     assumes and retains stream positioned at start
                     66: */
                     67: long int recsize(stream,start)
                     68: FILE *stream;
                     69: long int start;
                     70: {
                     71:        char line[256];
                     72:        long int length;
                     73: 
                     74:        while(fgets(line,256,stream)) {
                     75:                if(line[0] == '\n') {
                     76:                        length = ftell(stream)-start-1;
                     77:                        pos(start);
                     78:                        return(length);
                     79:                }
                     80:        }
                     81:        length = ftell(stream)-start;
                     82:        pos(start);
                     83:        return(length);
                     84: }
                     85: 
                     86: /*  nextrecord(stream,x): seeks in stream for first non-blank line
                     87:         at or after char x in stream. seeks to eof if x is past last record.
                     88:         x is the index of a character in the file (not eof).
                     89:     returns position in stream.  (returns EOF, if seeks to EOF)
                     90: */
                     91: long int nextrecord(stream,x)
                     92: FILE *stream;
                     93: long int x;
                     94: {   
                     95:        long int start;         /*  position of the beginning of the line  */
                     96:        char c;                 /*      containing c                       */
                     97: 
                     98:        pos(x);
                     99:        start= x;
                    100:        /*  find start of first non-blank record        */
                    101:        for(;;){   
                    102:                c= getc(stream);
                    103:                if      (c=='\n')           start= ftell(stream);
                    104:                else if (!isspace(c))       break;
                    105:        }
                    106: 
                    107:        if (feof(stream))  { 
                    108:                pos(start);  
                    109:                start= EOF;  
                    110:        }
                    111:        else pos(start);
                    112:        return(start);
                    113: }
                    114: 
                    115: /*  nextline(stream,x): seeks in stream after first newline at or after
                    116:         char x in stream. seeks to eof if x is in last line.
                    117:         x is the index of a character in the file (not eof).
                    118:     returns position in stream
                    119: */
                    120: long int nextline(stream,x)
                    121: FILE *stream;
                    122: long int x;
                    123: {   
                    124:        pos(x);
                    125:        while (getc(stream)!='\n') ;
                    126:        return(ftell(stream));
                    127: }
                    128: 
                    129: 
                    130: /*  printline(stream): copies stream up to a newline
                    131: */
                    132: printline(stream)
                    133: FILE *stream;
                    134: {   
                    135:        int c;
                    136:        while ((c=getc(stream)) != '\n' && c!=EOF)  putchar(c);
                    137:        putchar('\n');
                    138: }
                    139: 
                    140: /*  getline(stream,p):  store in *p next chars in stream up to \n
                    141:         advance stream past \n.
                    142:     limit of  maxstr-1 chars may be stored at p.
                    143: */
                    144: getline(stream,p)
                    145: FILE *stream;
                    146: char *p;
                    147: {   
                    148:        char *stop;
                    149:        int ci;
                    150: 
                    151:        stop= p+maxstr-1;
                    152:        while (((ci= getc(stream)) != '\n') && (ci!=EOF)) {
                    153:                if (ci == EOF)
                    154:                        break;
                    155:                *p = ci;
                    156:                if (p<stop)    p++;
                    157:        }
                    158:        *p= NULL;
                    159: }
                    160: 
                    161: 
                    162: #define MAXL   512
                    163: #define MIN(a,b)       (a<b?a:b)
                    164: load_ign(istring)
                    165: char *istring;
                    166: {
                    167:        FILE *fig;
                    168:        char line[MAXL];
                    169: 
                    170:        if((fig = fopen(istring,"r")) == NULL) {
                    171:                fprintf(stderr,"invert: error reading %s\n",istring);
                    172:                return(-1);
                    173:        }
                    174:        igcnt=0;
                    175:        while(fgets(line,MAXL,fig)) {
                    176:                if(igcnt == IWORD) {
                    177:                        fprintf(stderr,
                    178:                            "load_ign: warning:too many words in ignore file\n");
                    179:                        return(igcnt);
                    180:                }
                    181:                strncpy(&igntab[igcnt++][0],line+1,MIN(strlen(line+1)-1,IMAX-1));
                    182:        }
                    183:        return(igcnt);
                    184: }

unix.superglobalmegacorp.com

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