Annotation of researchv9/libc/gen/getfields.c, revision 1.1

1.1     ! root        1: #include       <libc.h>
        !             2: 
        !             3: static char is_sep[256], is_field[256];
        !             4: static init = 0;
        !             5: 
        !             6: void
        !             7: setfields(arg)
        !             8:        char *arg;
        !             9: {
        !            10:        register unsigned char *s = (unsigned char *)arg;
        !            11: 
        !            12:        memset(is_sep, 0, sizeof is_sep);
        !            13:        memset(is_field, 1, sizeof is_field);
        !            14:        while(*s){
        !            15:                is_sep[*s] = 1;
        !            16:                is_field[*s++] = 0;
        !            17:        }
        !            18:        is_field[0] = 0;
        !            19:        init = 1;
        !            20: }
        !            21: 
        !            22: getfields(ss, sp, nptrs)
        !            23:        char *ss;
        !            24:        char **sp;
        !            25: {
        !            26:        register unsigned char *s = (unsigned char *)ss;
        !            27:        register unsigned char **p = (unsigned char **)sp;
        !            28:        register unsigned c;
        !            29: 
        !            30:        if(init == 0)
        !            31:                setfields(" \t");
        !            32:        for(;;){
        !            33:                if(*s == 0) break;
        !            34:                if(--nptrs < 0) break;
        !            35:                *p++ = s;
        !            36:                while(is_field[c = *s++]);
        !            37:                if(c == 0) break;
        !            38:                s[-1] = 0;
        !            39:        }
        !            40:        if(nptrs > 0)
        !            41:                *p = 0;
        !            42:        else if(--s >= (unsigned char *)ss)
        !            43:                *s = c;
        !            44:        return(p - (unsigned char **)sp);
        !            45: }
        !            46: 
        !            47: getmfields(ss, sp, nptrs)
        !            48:        char *ss;
        !            49:        char **sp;
        !            50: {
        !            51:        register unsigned char *s = (unsigned char *)ss;
        !            52:        register unsigned char **p = (unsigned char **)sp;
        !            53:        register unsigned c;
        !            54:        unsigned char *goo;
        !            55: 
        !            56:        if(init == 0)
        !            57:                setfields(" \t");
        !            58:        if(*s){
        !            59:                while(nptrs-- > 0){
        !            60:                        *p++ = s;
        !            61:                        while(is_field[*s++]);
        !            62:                        goo = s-1;
        !            63:                        if((c = *goo) == 0)
        !            64:                                break;
        !            65:                        *goo = 0;
        !            66:                        while(is_sep[*s]) s++;
        !            67:                        if(*s == 0) break;
        !            68:                }
        !            69:        }
        !            70:        if(nptrs > 0)   /* plenty of room */
        !            71:                *p = 0;
        !            72:        else if(*s)     /* no room and we found a trailing non-is_seper */
        !            73:                *goo = c;
        !            74:        return(p - (unsigned char **)sp);
        !            75: }
        !            76: 
        !            77: #ifdef MAIN
        !            78: #include       <fio.h>
        !            79: 
        !            80: main()
        !            81: {
        !            82:        char *fields[256];
        !            83:        char *s;
        !            84:        int n, i;
        !            85: 
        !            86:        print("go:\n");
        !            87:        while(s = Frdline(0)){
        !            88:                n = getmfields(s, fields, 3);
        !            89:                for(i = 0; i < n; i++)
        !            90:                        Fprint(1, ">%s< ", fields[i]);
        !            91:                Fputc(1, '\n');
        !            92:                Fflush(1);
        !            93:        }
        !            94:        exit(0);
        !            95: }
        !            96: #endif

unix.superglobalmegacorp.com

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