Annotation of researchv10dc/cmd/lcc/ph/clib.h, revision 1.1.1.1

1.1       root        1: /* As per your license agreement, your distribution is not to be moved or copied outside the Designated Site
                      2:  * without specific permission from Plum Hall Inc.
                      3:  */
                      4: 
                      5: /*
                      6:  * CLIB - portable replacements for various C library functions
                      7:  */
                      8: #ifndef CLIB_H
                      9: #define CLIB_H
                     10: 
                     11: /* assumes you have #include'd either  "defs.h"  or  "hodefs.h" */
                     12: 
                     13: /*
                     14:  * IS_DIGIT
                     15:  */
                     16: int is_digit FN_DEF1(c,
                     17:        int c)
                     18:        {
                     19:        return '0' <= c && c <= '9';
                     20:        }
                     21: 
                     22: /*
                     23:  * MEM_CMP
                     24:  */
                     25: int mem_cmp FN_DEF3(ps1, ps2, n,
                     26:        const void *ps1,
                     27:        const void *ps2,
                     28:        int n)
                     29:        {
                     30:        const char *s1 = (const char *)ps1;
                     31:        const char *s2 = (const char *)ps2;
                     32:        for (;n > 0; --n)
                     33:                {
                     34:                if (*s1 != *s2)
                     35:                        return (UCHAR)*s1 - (UCHAR)*s2;
                     36:                ++s1, ++s2;
                     37:                }
                     38:        return 0;
                     39:        }
                     40: 
                     41: /*
                     42:  * STR_CMP
                     43:  */
                     44: int str_cmp FN_DEF2(s1, s2,
                     45:        const char *s1,
                     46:        const char *s2)
                     47:        {
                     48:        for (; *s1 == *s2; ++s1, ++s2)
                     49:                if (*s1 == '\0')
                     50:                        return 0;
                     51:        return (UCHAR)*s1 - (UCHAR)*s2;
                     52:        }
                     53: 
                     54: /*
                     55:  * ST_NCMP
                     56:  *  Note: spelled  st_ncmp  to avoid 6-letter collision with st_ncpy
                     57:  */
                     58: int st_ncmp FN_DEF3(s1, s2, n,
                     59:        const char *s1,
                     60:        const char *s2,
                     61:        int n)
                     62:        {
                     63:        for (; n > 0 && *s1 == *s2; ++s1, ++s2, --n)
                     64:                if (*s1 == '\0')
                     65:                        return 0;
                     66:        if (n == 0)
                     67:                return 0;
                     68:        else
                     69:                return (UCHAR)*s1 - (UCHAR)*s2;
                     70:        }
                     71: 
                     72: /*
                     73:  * STR_CPYE - copy string s2 to s1 (not an exact replacement for strcpy)
                     74:  */
                     75: char *str_cpye FN_DEF2(s1, s2,
                     76:        char *s1,
                     77:        const char *s2)
                     78:        {
                     79:        while ((*s1 = *s2++) != '\0')
                     80:                ++s1;
                     81:        return s1;
                     82:        }
                     83: 
                     84: /*
                     85:  * STR_CAT - copy string s2 to end of s1
                     86:  */
                     87: char *str_cat FN_DEF2(s1, s2,
                     88:        char *s1,
                     89:        const char *s2)
                     90:        {
                     91:        char *s0 = s1;
                     92: 
                     93:        while (*s1 != '\0')
                     94:                ++s1;
                     95:        while ((*s1 = *s2++) != '\0')
                     96:                ++s1;
                     97:        return s0;
                     98:        }
                     99: 
                    100: /*
                    101:  * ST_NCPY - copy at most n chars of string s2 to s1
                    102:  */
                    103: char *st_ncpy FN_DEF3(s1, s2, n,
                    104:        char *s1,
                    105:        const char *s2,
                    106:        int n)
                    107:        {
                    108:        char *s0 = s1;
                    109:        while (n > 0 && (*s1 = *s2++) != '\0')
                    110:                --n, ++s1;
                    111:        while (n-- > 0)
                    112:                *s1++ = '\0';
                    113:        return s0;
                    114:        }
                    115: 
                    116: /*
                    117:  * STR_LEN - return the length of a string
                    118:  */
                    119: int str_len FN_DEF1(s,
                    120:        const char *s)
                    121:        {
                    122:        const char *t;
                    123: 
                    124:        for (t = s; *t != '\0'; ++t)
                    125:                ;
                    126:        return t - s;
                    127:        }
                    128: 
                    129: /*
                    130:  * STR_STR - find occurrence of s2 in s1
                    131:  */
                    132: const char *str_str FN_DEF2(s1, s2,
                    133:        const char *s1,
                    134:        const char *s2)
                    135:        {
                    136:        register const char *ps1, *ps2;
                    137: 
                    138:        if (*s2 == '\0')
                    139:                return s1;
                    140:        for ( ; *s1 != '\0'; ++s1)
                    141:                {
                    142:                if (*s1 == *s2)
                    143:                        {
                    144:                        for (ps1 = s1, ps2 = s2; *ps1 && *ps2 && *ps1 == *ps2; ++ps1, ++ps2)
                    145:                                ;
                    146:                        if (*ps2 == '\0')
                    147:                                return s1;
                    148:                        }
                    149:                }
                    150:        return 0;
                    151:        }
                    152: /*
                    153:  * STR_CSPN
                    154:  */
                    155: int str_cspn FN_DEF2(s1, s2,
                    156:        char *s1,
                    157:        char *s2)
                    158:        {
                    159:        register int i;
                    160:        register char *p;
                    161: 
                    162:        for (i = 0;  s1[i] != '\0';  ++i)
                    163:                {
                    164:                for (p = s2;  *p != '\0';  ++p)
                    165:                        {
                    166:                        if (*p == s1[i])
                    167:                                return (i);
                    168:                        }
                    169:                }
                    170:        return (i);
                    171:        }
                    172: #endif

unix.superglobalmegacorp.com

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