Annotation of coherent/d/bin/sh/atoi.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Non-floating ASCII to int conversion
        !             3:  * int atoi(cp)
        !             4:  * char *cp;
        !             5:  * Modified by rec 22.Apr.82 to allow octal and hexadecimal.
        !             6:  */
        !             7: 
        !             8: atoi(cp)
        !             9: register char *cp;
        !            10: {
        !            11:        register val;
        !            12:        register c;
        !            13:        register base;
        !            14:        int sign;
        !            15: 
        !            16:        val = sign = 0;
        !            17:        while ((c = *cp)==' ' || c=='\t')
        !            18:                cp++;
        !            19:        if (c == '-') {
        !            20:                sign = 1;
        !            21:                cp++;
        !            22:        } else if (c == '+')
        !            23:                cp++;
        !            24:        base = 10;
        !            25:        if ((c = *cp) == '0') {
        !            26:                cp++;
        !            27:                if ((c = *cp) == 'x' || c == 'X') {
        !            28:                        cp++;
        !            29:                        base = 16;
        !            30:                } else
        !            31:                        base = 8;
        !            32:        }
        !            33:        for (;;) {
        !            34:                c = *cp++;
        !            35:                if ((c -= '0') >= 0 && c <= 9) {
        !            36:                        val = val*base - c;
        !            37:                        continue;
        !            38:                }
        !            39:                if (base == 16) {
        !            40:                        if ((c += 10 + '0' - 'A') >= 0 && c < base) {
        !            41:                                val = val*base - c;
        !            42:                                continue;
        !            43:                        }
        !            44:                        if ((c += 'A' - 'a') >= 0 && c < base) {
        !            45:                                val = val*base - c;
        !            46:                                continue;
        !            47:                        }
        !            48:                }
        !            49:                break;
        !            50:        }
        !            51:        if (!sign)
        !            52:                val = -val;
        !            53:        return (val);
        !            54: }

unix.superglobalmegacorp.com

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