Annotation of MiNT/src/util.c, revision 1.1.1.3

1.1       root        1: /*
                      2: 
1.1.1.3 ! root        3: Copyright 1990,1991,1992 Eric R. Smith.
        !             4: 
        !             5: Copyright 1992 Atari Corporation.
        !             6: 
        !             7: All rights reserved.
1.1       root        8: 
                      9: */
                     10: 
                     11: 
                     12: 
                     13: /*
                     14: 
                     15:  * misc. utility routines
                     16: 
                     17:  */
                     18: 
                     19: 
                     20: 
                     21: #include "mint.h"
                     22: 
                     23: 
                     24: 
                     25: /*
                     26: 
                     27:  * given an address, find the corresponding memory region in this program's
                     28: 
                     29:  * memory map
                     30: 
                     31:  */
                     32: 
                     33: 
                     34: 
                     35: MEMREGION *
                     36: 
                     37: addr2mem(a)
                     38: 
                     39:        virtaddr a;
                     40: 
                     41: {
                     42: 
                     43:        int i;
                     44: 
                     45: 
                     46: 
                     47:        for (i = 0; i < curproc->num_reg; i++) {
                     48: 
                     49:                if (a == curproc->addr[i])
                     50: 
                     51:                        return curproc->mem[i];
                     52: 
                     53:        }
                     54: 
                     55:        return 0;
                     56: 
                     57: }
                     58: 
                     59: 
                     60: 
                     61: /*
                     62: 
                     63:  * given a pid, return the corresponding process
                     64: 
                     65:  */
                     66: 
                     67: 
                     68: 
                     69: PROC *
                     70: 
                     71: pid2proc(pid)
                     72: 
                     73:        int pid;
                     74: 
                     75: {
                     76: 
                     77:        PROC *p;
                     78: 
                     79: 
                     80: 
                     81:        for (p = proclist; p; p = p->gl_next) {
                     82: 
                     83:                if (p->pid == pid)
                     84: 
                     85:                        return p;
                     86: 
                     87:        }
                     88: 
                     89:        return 0;
                     90: 
                     91: }
                     92: 
                     93: 
                     94: 
                     95: /*
                     96: 
                     97:  * return a new pid
                     98: 
                     99:  */
                    100: 
                    101: 
                    102: 
                    103: int
                    104: 
                    105: newpid()
                    106: 
                    107: {
                    108: 
                    109:        static int _maxpid = 1;
                    110: 
                    111:        int i;
                    112: 
                    113: #ifndef NDEBUG
                    114: 
                    115:        int j = 0;
                    116: 
                    117: #endif
                    118: 
                    119: 
                    120: 
                    121:        do {
                    122: 
                    123:                i = _maxpid++;
                    124: 
                    125:                if (_maxpid >= 1000) _maxpid = 1;
                    126: 
                    127:                assert(j++ < 1000);
                    128: 
                    129:        } while (pid2proc(i));
                    130: 
                    131: 
                    132: 
                    133:        return i;
                    134: 
                    135: }
                    136: 
                    137: 
                    138: 
                    139: /*
                    140: 
                    141:  * zero out a block of memory, quickly; the block must be word-aligned,
                    142: 
                    143:  * and should be long-aligned for speed reasons
                    144: 
                    145:  */
                    146: 
                    147: 
                    148: 
                    149: void
                    150: 
                    151: zero(place, size)
                    152: 
                    153:        char *place;
                    154: 
                    155:        long size;
                    156: 
                    157: {
                    158: 
1.1.1.2   root      159:        long cruft;
1.1       root      160: 
                    161: 
                    162: 
                    163:        cruft = size % 256;     /* quickzero does 256 byte blocks */
                    164: 
                    165:        size = size / 256;
                    166: 
                    167:        while (cruft > 0) {
                    168: 
                    169:                *place++ = 0;
                    170: 
                    171:                cruft--;
                    172: 
                    173:        }
                    174: 
                    175:        if (size > 0) {
                    176: 
                    177:                quickzero(place, size);
                    178: 
                    179:        }
                    180: 
                    181: }
                    182: 
                    183: 
                    184: 
                    185: #ifdef JUNK_MEM
                    186: 
                    187: void
                    188: 
                    189: fillwjunk(place, size)
                    190: 
                    191:        long *place;
                    192: 
                    193:        long size;
                    194: 
                    195: {
                    196: 
                    197:        while (size > 0) {
                    198: 
                    199:                *place++ = size;
                    200: 
                    201:                size -= 4;
                    202: 
                    203:        }
                    204: 
                    205: }
                    206: 
                    207: #endif
                    208: 
                    209: 
                    210: 
                    211: /*
                    212: 
                    213:  * kernel memory allocation routines
                    214: 
                    215:  */
                    216: 
                    217: 
                    218: 
1.1.1.3 ! root      219: #define KERMEM_THRESHOLD QUANTUM-8
        !           220: 
1.1.1.2   root      221: #define KMAGIC ((MEMREGION *)0x87654321L)
1.1       root      222: 
1.1.1.3 ! root      223: #define NKMAGIC 0x19870425L
1.1       root      224: 
                    225: 
                    226: 
1.1.1.2   root      227: void * ARGS_ON_STACK 
1.1       root      228: 
                    229: kmalloc(size)
                    230: 
                    231:        long size;
                    232: 
                    233: {
                    234: 
1.1.1.3 ! root      235:        MEMREGION *m = 0;
1.1       root      236: 
                    237:        MEMREGION **p;
                    238: 
1.1.1.3 ! root      239:        long *lp;
1.1       root      240: 
                    241: 
1.1.1.3 ! root      242: 
        !           243:        /*
        !           244: 
        !           245:         * increase size by two pointers' worth: the first contains
        !           246: 
        !           247:         * a pointer to the region descriptor for this block, and the
        !           248: 
        !           249:         * second contains KMAGIC.  If the block came from nalloc,
        !           250: 
        !           251:         * then they both contain NKMAGIC.
        !           252: 
        !           253:         */
        !           254: 
        !           255:        size += sizeof(m) + sizeof(m);
1.1       root      256: 
                    257: /*
                    258: 
1.1.1.3 ! root      259:  * for small requests, we use nalloc first
1.1       root      260: 
                    261:  */
                    262: 
1.1.1.3 ! root      263: tryagain:
        !           264: 
        !           265:        if (size < KERMEM_THRESHOLD) {
        !           266: 
        !           267:            lp = nalloc(size);
1.1       root      268: 
1.1.1.3 ! root      269:            if (lp) {
1.1       root      270: 
1.1.1.3 ! root      271:                *lp++ = NKMAGIC;
1.1       root      272: 
1.1.1.3 ! root      273:                *lp++ = NKMAGIC;
1.1       root      274: 
1.1.1.3 ! root      275:                TRACELOW(("kmalloc(%lx) -> (nalloc) %lx",size,lp));
1.1       root      276: 
1.1.1.3 ! root      277:                return lp;
1.1       root      278: 
1.1.1.3 ! root      279:            }
1.1       root      280: 
1.1.1.3 ! root      281:            else {
1.1       root      282: 
1.1.1.3 ! root      283:                DEBUG(("kmalloc(%lx): nalloc is out of memory",size));
1.1       root      284: 
                    285: 
                    286: 
1.1.1.3 ! root      287:        /* If this is commented out, then we fall through to try_getregion */
        !           288: 
        !           289:                if (0 == (m = get_region(alt, QUANTUM, PROT_S))) {
        !           290: 
        !           291:                    if (0 == (m = get_region(core, QUANTUM, PROT_S))) {
        !           292: 
        !           293:                        DEBUG(("No memory for another arena"));
        !           294: 
        !           295:                        goto try_getregion;
        !           296: 
        !           297:                    }
        !           298: 
        !           299:                }
        !           300: 
        !           301:                nalloc_arena_add((void *)m->loc,QUANTUM);
        !           302: 
        !           303:                m = 0;
        !           304: 
        !           305:                goto tryagain;
        !           306: 
        !           307:            }
1.1       root      308: 
                    309:        }
                    310: 
1.1.1.3 ! root      311: 
        !           312: 
        !           313: try_getregion:
        !           314: 
        !           315:        m = get_region(alt, size, PROT_S);
        !           316: 
        !           317: 
        !           318: 
        !           319:        if (!m) m = get_region(core, size, PROT_S);
        !           320: 
        !           321: 
        !           322: 
1.1       root      323:        if (m) {
                    324: 
                    325:                p = (MEMREGION **)m->loc;
                    326: 
                    327:                *p++ = KMAGIC;
                    328: 
                    329:                *p++ = m;
                    330: 
1.1.1.3 ! root      331:                TRACELOW(("kmalloc(%lx) -> (get_region) %lx",size,p));
        !           332: 
1.1       root      333:                return (void *)p;
                    334: 
                    335:        }
                    336: 
                    337:        else {
                    338: 
1.1.1.3 ! root      339:                TRACELOW(("kmalloc(%lx) -> (fail)",size));
        !           340: 
        !           341: #if 0
        !           342: 
        !           343:                /* this is a serious offense; I want to hear about it */
        !           344: 
        !           345:                /* maybe Allan wanted to hear about it, but ordinary users
        !           346: 
        !           347:                 * won't! -- ERS
        !           348: 
        !           349:                 */
        !           350: 
        !           351:                NALLOC_DUMP();
        !           352: 
        !           353:                BIG_MEM_DUMP(0,0);
        !           354: 
        !           355: #endif
        !           356: 
1.1       root      357:                return 0;
                    358: 
                    359:        }
                    360: 
                    361: }
                    362: 
                    363: 
                    364: 
                    365: /* allocate from ST memory only */
                    366: 
                    367: 
                    368: 
                    369: void *
                    370: 
                    371: kcore(size)
                    372: 
                    373:        long size;
                    374: 
                    375: {
                    376: 
1.1.1.2   root      377:        MEMREGION *m;
1.1       root      378: 
                    379:        MEMREGION **p;
                    380: 
                    381: 
                    382: 
                    383:        size += sizeof(m) + sizeof (m);
                    384: 
1.1.1.3 ! root      385:        m = get_region(core, size, PROT_S);
1.1       root      386: 
                    387: 
                    388: 
                    389:        if (m) {
                    390: 
                    391:                p = (MEMREGION **)m->loc;
                    392: 
                    393:                *p++ = KMAGIC;
                    394: 
                    395:                *p++ = m;
                    396: 
                    397:                return (void *)p;
                    398: 
                    399:        }
                    400: 
                    401:        else {
                    402: 
                    403:                return 0;
                    404: 
                    405:        }
                    406: 
                    407: }
                    408: 
                    409: 
                    410: 
1.1.1.2   root      411: void ARGS_ON_STACK 
1.1       root      412: 
                    413: kfree(place)
                    414: 
                    415:        void *place;
                    416: 
                    417: {
                    418: 
                    419:        MEMREGION **p;
                    420: 
                    421:        MEMREGION *m;
                    422: 
                    423: 
                    424: 
1.1.1.3 ! root      425:        TRACELOW(("kfree(%lx)",place));
        !           426: 
        !           427: 
        !           428: 
1.1       root      429:        if (!place) return;
                    430: 
                    431:        p = place;
                    432: 
                    433:        p -= 2;
                    434: 
1.1.1.3 ! root      435:        if (*p == (MEMREGION *)NKMAGIC) {
        !           436: 
        !           437:            nfree(p);
        !           438: 
        !           439:            return;
        !           440: 
        !           441:        }
        !           442: 
        !           443:        else if (*p++ != KMAGIC) {
1.1       root      444: 
                    445:                FATAL("kfree: memory not allocated by kmalloc");
                    446: 
                    447:        }
                    448: 
1.1.1.2   root      449:        m = *p;
1.1       root      450: 
                    451:        if (--m->links != 0) {
                    452: 
                    453:                FATAL("kfree: block has %d links", m->links);
                    454: 
                    455:        }
                    456: 
                    457:        free_region(m);
                    458: 
                    459: }
                    460: 
                    461: 
                    462: 
                    463: /*
                    464: 
                    465:  * "user" memory allocation routines; the kernel can use these to
                    466: 
                    467:  * allocate/free memory that will be attached in some way to a process
                    468: 
                    469:  * (and freed automatically when the process exits)
                    470: 
                    471:  */
                    472: 
1.1.1.2   root      473: void * ARGS_ON_STACK 
1.1       root      474: 
                    475: umalloc(size)
                    476: 
                    477:        long size;
                    478: 
                    479: {
                    480: 
                    481:        return (void *)m_xalloc(size, 3);
                    482: 
                    483: }
                    484: 
                    485: 
                    486: 
1.1.1.2   root      487: void ARGS_ON_STACK 
1.1       root      488: 
                    489: ufree(block)
                    490: 
                    491:        void *block;
                    492: 
                    493: {
                    494: 
                    495:        (void)m_free((virtaddr)block);
                    496: 
                    497: }
                    498: 
                    499: 
                    500: 
                    501: /*
                    502: 
                    503:  * convert a time in milliseconds to a GEMDOS style date/time
                    504: 
                    505:  * timeptr[0] gets the time, timeptr[1] the date.
                    506: 
                    507:  * BUG/FEATURE: in the conversion, it is assumed that all months have
                    508: 
                    509:  * 30 days and all years have 360 days.
                    510: 
                    511:  */
                    512: 
                    513: 
                    514: 
1.1.1.2   root      515: void ARGS_ON_STACK 
1.1       root      516: 
                    517: ms_time(ms, timeptr)
                    518: 
                    519:        ulong ms;
                    520: 
                    521:        short *timeptr;
                    522: 
                    523: {
                    524: 
                    525:        ulong secs;
                    526: 
                    527:        short tsec, tmin, thour;
                    528: 
                    529:        short tday, tmonth, tyear;
                    530: 
                    531: 
                    532: 
                    533:        secs = ms / 1000;
                    534: 
                    535:        tsec = secs % 60;
                    536: 
                    537:        secs /= 60;             /* secs now contains # of minutes */
                    538: 
                    539:        tmin = secs % 60;
                    540: 
                    541:        secs /= 60;             /* secs now contains # of hours */
                    542: 
                    543:        thour = secs % 24;
                    544: 
                    545:        secs /= 24;             /* secs now contains # of days */
                    546: 
                    547:        tday = secs % 30;
                    548: 
                    549:        secs /= 30;
                    550: 
                    551:        tmonth = secs % 12;
                    552: 
                    553:        tyear = secs / 12;
                    554: 
                    555:        *timeptr++ = (thour << 11) | (tmin << 5) | (tsec >> 1);
                    556: 
                    557:        *timeptr = (tyear << 9) | ((tmonth + 1) << 5) | (tday+1);
                    558: 
                    559: }
                    560: 
                    561: 
                    562: 
                    563: /*
                    564: 
                    565:  * unixtim(time, date): convert a Dos style (time, date) pair into
                    566: 
                    567:  * a Unix time (seconds from midnight Jan 1., 1970)
                    568: 
                    569:  */
                    570: 
                    571: 
                    572: 
                    573: static int
                    574: 
                    575: mth_start[13] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
                    576: 
                    577: 
                    578: 
1.1.1.2   root      579: long ARGS_ON_STACK 
1.1       root      580: 
                    581: unixtim(time, date)
                    582: 
                    583:        unsigned time, date;
                    584: 
                    585: {
                    586: 
                    587:        int sec, min, hour;
                    588: 
                    589:        int mday, mon, year;
                    590: 
                    591:        long y, s;
                    592: 
                    593: 
                    594: 
                    595:        sec = (time & 31) << 1;
                    596: 
                    597:        min = (time >> 5) & 63;
                    598: 
                    599:        hour = (time >> 11) & 31;
                    600: 
                    601:        mday = date & 31;
                    602: 
                    603:        mon = ((date >> 5) & 15) - 1;
                    604: 
                    605:        year = 80 + ((date >> 9) & 255);
                    606: 
                    607: 
                    608: 
                    609: /* calculate tm_yday here */
                    610: 
                    611:        y = (mday - 1) + mth_start[mon] + /* leap year correction */
                    612: 
                    613:                ( ( (year % 4) != 0 ) ? 0 : (mon > 1) );
                    614: 
                    615: 
                    616: 
                    617:        s = (sec) + (min * 60L) + (hour * 3600L) +
                    618: 
                    619:                (y * 86400L) + ((year - 70) * 31536000L) +
                    620: 
                    621:                ((year - 69)/4) * 86400L;
                    622: 
                    623: 
                    624: 
                    625:        return s;
                    626: 
                    627: }
                    628: 
                    629: 
                    630: 
                    631: /* convert a Unix time into a DOS time. The longword returned contains
                    632: 
                    633:    the time word first, then the date word.
                    634: 
                    635:    BUG: we completely ignore any time zone information.
                    636: 
                    637: */
                    638: 
                    639: #define SECS_PER_MIN    (60L)
                    640: 
                    641: #define SECS_PER_HOUR   (3600L)
                    642: 
                    643: #define SECS_PER_DAY    (86400L)
                    644: 
                    645: #define SECS_PER_YEAR   (31536000L)
                    646: 
                    647: #define SECS_PER_LEAPYEAR (SECS_PER_DAY + SECS_PER_YEAR)
                    648: 
                    649: 
                    650: 
                    651: static int
                    652: 
                    653: days_per_mth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
                    654: 
                    655: 
                    656: 
1.1.1.2   root      657: long ARGS_ON_STACK 
1.1       root      658: 
                    659: dostim(t)
                    660: 
                    661:        long t;
                    662: 
                    663: {
                    664: 
                    665:         unsigned long time, date;
                    666: 
                    667:        int tm_hour, tm_min, tm_sec;
                    668: 
                    669:        int tm_year, tm_mon, tm_mday;
                    670: 
                    671:        int i;
                    672: 
                    673: 
                    674: 
                    675:        if (t <= 0) return 0;
                    676: 
                    677: 
                    678: 
                    679:        tm_year = 70;
                    680: 
                    681:        while (t >= SECS_PER_YEAR) {
                    682: 
                    683:                if ((tm_year & 0x3) == 0) {
                    684: 
                    685:                        if (t < SECS_PER_LEAPYEAR)
                    686: 
                    687:                                break;
                    688: 
                    689:                        t -= SECS_PER_LEAPYEAR;
                    690: 
                    691:                } else {
                    692: 
                    693:                        t -= SECS_PER_YEAR;
                    694: 
                    695:                }
                    696: 
                    697:                tm_year++;
                    698: 
                    699:        }
                    700: 
1.1.1.2   root      701:        tm_mday = (int)(t/SECS_PER_DAY);
1.1       root      702: 
                    703:         days_per_mth[1] = (tm_year & 0x3) ? 28 : 29;
                    704: 
                    705:         for (i = 0; tm_mday >= days_per_mth[i]; i++)
                    706: 
                    707:                 tm_mday -= days_per_mth[i];
                    708: 
                    709:         tm_mon = i+1;
                    710: 
                    711:        tm_mday++;
                    712: 
                    713:         t = t % SECS_PER_DAY;
                    714: 
1.1.1.2   root      715:         tm_hour = (int)(t/SECS_PER_HOUR);
1.1       root      716: 
                    717:         t = t % SECS_PER_HOUR;
                    718: 
1.1.1.2   root      719:         tm_min = (int)(t/SECS_PER_MIN);
1.1       root      720: 
1.1.1.2   root      721:         tm_sec = (int)(t % SECS_PER_MIN);
1.1       root      722: 
                    723: 
                    724: 
                    725:        if (tm_year < 80) {
                    726: 
                    727:                tm_year = 80;
                    728: 
                    729:                tm_mon = tm_mday = 1;
                    730: 
                    731:                tm_hour = tm_min = tm_sec = 0;
                    732: 
                    733:        }
                    734: 
                    735: 
                    736: 
                    737:        time = (tm_hour << 11) | (tm_min << 5) | (tm_sec >> 1);
                    738: 
                    739:        date = ((tm_year - 80) & 0x7f) << 9;
                    740: 
                    741:        date |= ((tm_mon) << 5) | (tm_mday);
                    742: 
                    743:        return (time << 16) | date;
                    744: 
                    745: }
                    746: 
                    747: 
                    748: 
                    749: /*
                    750: 
                    751:  * Case insensitive string comparison. note that this only returns
                    752: 
                    753:  * 0 (match) or nonzero (no match), and that the returned value
                    754: 
                    755:  * is not a reliable indicator of any "order".
                    756: 
                    757:  */
                    758: 
                    759: 
                    760: 
1.1.1.2   root      761: int ARGS_ON_STACK 
1.1       root      762: 
                    763: strnicmp(str1, str2, len)
                    764: 
                    765:        register const char *str1, *str2;
                    766: 
                    767:        register int len;
                    768: 
                    769: {
                    770: 
                    771:        register char c1, c2;
                    772: 
                    773: 
                    774: 
                    775:        do {
                    776: 
                    777:                c1 = *str1++; if (isupper(c1)) c1 = tolower(c1);
                    778: 
                    779:                c2 = *str2++; if (isupper(c2)) c2 = tolower(c2);
                    780: 
                    781:        } while (--len >= 0 && c1 && c1 == c2);
                    782: 
                    783: 
                    784: 
                    785:        if (len < 0 || c1 == c2)
                    786: 
                    787:                return 0;
                    788: 
                    789:        return c1 - c2;
                    790: 
                    791: }
                    792: 
                    793: 
                    794: 
1.1.1.2   root      795: int ARGS_ON_STACK 
1.1       root      796: 
                    797: stricmp(str1, str2)
                    798: 
                    799:        const char *str1, *str2;
                    800: 
                    801: {
                    802: 
                    803:        return strnicmp(str1, str2, 0x7fff);
                    804: 
                    805: }
                    806: 
                    807: 
                    808: 
                    809: 
                    810: 
                    811: /*
                    812: 
                    813:  * string utilities: strlwr() converts a string to lower case, strupr()
                    814: 
                    815:  * converts it to upper case
                    816: 
                    817:  */
                    818: 
                    819: 
                    820: 
1.1.1.2   root      821: char * ARGS_ON_STACK 
1.1       root      822: 
                    823: strlwr(s)
                    824: 
                    825:        char *s;
                    826: 
                    827: {
                    828: 
                    829:        char c;
                    830: 
                    831:        char *old = s;
                    832: 
                    833: 
                    834: 
                    835:        while ((c = *s) != 0) {
                    836: 
                    837:                if (isupper(c)) {
                    838: 
                    839:                        *s = _tolower(c);
                    840: 
                    841:                }
                    842: 
                    843:                s++;
                    844: 
                    845:        }
                    846: 
                    847:        return old;
                    848: 
                    849: }
                    850: 
                    851: 
                    852: 
1.1.1.2   root      853: char * ARGS_ON_STACK 
1.1       root      854: 
                    855: strupr(s)
                    856: 
                    857:        char *s;
                    858: 
                    859: {
                    860: 
                    861:        char c;
                    862: 
                    863:        char *old = s;
                    864: 
                    865: 
                    866: 
                    867:        while ((c = *s) != 0) {
                    868: 
                    869:                if (islower(c)) {
                    870: 
                    871:                        *s = _toupper(c);
                    872: 
                    873:                }
                    874: 
                    875:                s++;
                    876: 
                    877:        }
                    878: 
                    879:        return old;
                    880: 
                    881: }
                    882: 
                    883: 
                    884: 
                    885: #ifdef OWN_LIB
                    886: 
                    887: 
                    888: 
                    889: /*
                    890: 
                    891:  * Case sensitive comparison functions.
                    892: 
                    893:  */
                    894: 
                    895: 
                    896: 
                    897: int
                    898: 
                    899: strncmp(str1, str2, len)
                    900: 
                    901:        register const char *str1, *str2;
                    902: 
                    903:        register int len;
                    904: 
                    905: {
                    906: 
                    907:        register char c1, c2;
                    908: 
                    909: 
                    910: 
                    911:        do {
                    912: 
                    913:                c1 = *str1++;
                    914: 
                    915:                c2 = *str2++;
                    916: 
                    917:        } while (--len >= 0 && c1 && c1 == c2);
                    918: 
                    919: 
                    920: 
                    921:        if (len < 0) return 0;
                    922: 
                    923: 
                    924: 
                    925:        return c1 - c2;
                    926: 
                    927: }
                    928: 
                    929: 
                    930: 
                    931: int
                    932: 
                    933: strcmp(str1, str2)
                    934: 
                    935:        const char *str1, *str2;
                    936: 
                    937: {
                    938: 
                    939:        register char c1, c2;
                    940: 
                    941: 
                    942: 
                    943:        do {
                    944: 
                    945:                c1 = *str1++;
                    946: 
                    947:                c2 = *str2++;
                    948: 
                    949:        } while (c1 && c1 == c2);
                    950: 
                    951: 
                    952: 
                    953:        return c1 - c2;
                    954: 
                    955: }
                    956: 
                    957: 
                    958: 
                    959: 
                    960: 
                    961: /*
                    962: 
                    963:  * some standard string functions
                    964: 
                    965:  */
                    966: 
                    967: 
                    968: 
                    969: char *
                    970: 
                    971: strcat(dst, src)
                    972: 
                    973:        char *dst;
                    974: 
                    975:        const char *src;
                    976: 
                    977: {
                    978: 
                    979:        register char *_dscan;
                    980: 
                    981: 
                    982: 
                    983:        for (_dscan = dst; *_dscan; _dscan++) ;
                    984: 
1.1.1.2   root      985:        while ((*_dscan++ = *src++) != 0) ;
1.1       root      986: 
                    987:        return dst;
                    988: 
                    989: }
                    990: 
                    991: 
                    992: 
                    993: char *
                    994: 
                    995: strcpy(dst, src)
                    996: 
                    997:        char *dst;
                    998: 
                    999:        const char *src;
                   1000: 
                   1001: {
                   1002: 
                   1003:        register char *_dscan = dst;
                   1004: 
1.1.1.2   root     1005:        while ((*_dscan++ = *src++) != 0) ;
1.1       root     1006: 
                   1007:        return dst;
                   1008: 
                   1009: }
                   1010: 
                   1011: 
                   1012: 
                   1013: char *
                   1014: 
                   1015: strncpy(dst, src, len)
                   1016: 
                   1017:        char *dst;
                   1018: 
                   1019:        const char *src;
                   1020: 
                   1021:        int len;
                   1022: 
                   1023: {
                   1024: 
                   1025:        register char *_dscan = dst;
                   1026: 
1.1.1.2   root     1027:        while (--len >= 0 && (*_dscan++ = *src++) != 0)
1.1       root     1028: 
                   1029:                continue;
                   1030: 
                   1031:        while (--len >= 0)
                   1032: 
                   1033:                *_dscan++ = 0;
                   1034: 
                   1035:        return dst;
                   1036: 
                   1037: }
                   1038: 
                   1039: 
                   1040: 
                   1041: int
                   1042: 
                   1043: strlen(scan)
                   1044: 
                   1045:        const char *scan;
                   1046: 
                   1047: {
                   1048: 
                   1049:        register const char *_start = scan+1;
                   1050: 
                   1051:        while (*scan++) ;
                   1052: 
                   1053:        return (int)((long)scan - (long)_start);
                   1054: 
                   1055: }
                   1056: 
                   1057: 
                   1058: 
                   1059: /*
                   1060: 
                   1061:  * strrchr: find the last occurence of a character in a string
                   1062: 
                   1063:  */
                   1064: 
                   1065: char *
                   1066: 
                   1067: strrchr(str, which)
                   1068: 
                   1069:        const char *str;
                   1070: 
                   1071:        register int which;
                   1072: 
                   1073: {
                   1074: 
                   1075:        register unsigned char c, *s;
                   1076: 
                   1077:        register char *place;
                   1078: 
                   1079: 
                   1080: 
                   1081:        s = (unsigned char *)str;
                   1082: 
                   1083:        place = 0;
                   1084: 
                   1085:        do {
                   1086: 
                   1087:                c = *s++;
                   1088: 
                   1089:                if (c == which)
                   1090: 
                   1091:                        place = (char *)s-1;
                   1092: 
                   1093:        } while (c);
                   1094: 
                   1095:        return place;
                   1096: 
                   1097: }
                   1098: 
                   1099: 
                   1100: 
                   1101: unsigned char _ctype[256] =
                   1102: 
                   1103: {
                   1104: 
                   1105:        _CTc, _CTc, _CTc, _CTc,                         /* 0x00..0x03 */
                   1106: 
                   1107:        _CTc, _CTc, _CTc, _CTc,                         /* 0x04..0x07 */
                   1108: 
                   1109:        _CTc, _CTc|_CTs, _CTc|_CTs, _CTc|_CTs,          /* 0x08..0x0B */
                   1110: 
                   1111:        _CTc|_CTs, _CTc|_CTs, _CTc, _CTc,               /* 0x0C..0x0F */
                   1112: 
                   1113: 
                   1114: 
                   1115:        _CTc, _CTc, _CTc, _CTc,                         /* 0x10..0x13 */
                   1116: 
                   1117:        _CTc, _CTc, _CTc, _CTc,                         /* 0x14..0x17 */
                   1118: 
                   1119:        _CTc, _CTc, _CTc, _CTc,                         /* 0x18..0x1B */
                   1120: 
                   1121:        _CTc, _CTc, _CTc, _CTc,                         /* 0x1C..0x1F */
                   1122: 
                   1123: 
                   1124: 
                   1125:        _CTs, _CTp, _CTp, _CTp,                         /* 0x20..0x23 */
                   1126: 
                   1127:        _CTp, _CTp, _CTp, _CTp,                         /* 0x24..0x27 */
                   1128: 
                   1129:        _CTp, _CTp, _CTp, _CTp,                         /* 0x28..0x2B */
                   1130: 
                   1131:        _CTp, _CTp, _CTp, _CTp,                         /* 0x2C..0x2F */
                   1132: 
                   1133: 
                   1134: 
                   1135:        _CTd|_CTx, _CTd|_CTx, _CTd|_CTx, _CTd|_CTx,     /* 0x30..0x33 */
                   1136: 
                   1137:        _CTd|_CTx, _CTd|_CTx, _CTd|_CTx, _CTd|_CTx,     /* 0x34..0x37 */
                   1138: 
                   1139:        _CTd|_CTx, _CTd|_CTx, _CTp, _CTp,               /* 0x38..0x3B */
                   1140: 
                   1141:        _CTp, _CTp, _CTp, _CTp,                         /* 0x3C..0x3F */
                   1142: 
                   1143: 
                   1144: 
                   1145:        _CTp, _CTu|_CTx, _CTu|_CTx, _CTu|_CTx,          /* 0x40..0x43 */
                   1146: 
                   1147:        _CTu|_CTx, _CTu|_CTx, _CTu|_CTx, _CTu,          /* 0x44..0x47 */
                   1148: 
                   1149:        _CTu, _CTu, _CTu, _CTu,                         /* 0x48..0x4B */
                   1150: 
                   1151:        _CTu, _CTu, _CTu, _CTu,                         /* 0x4C..0x4F */
                   1152: 
                   1153: 
                   1154: 
                   1155:        _CTu, _CTu, _CTu, _CTu,                         /* 0x50..0x53 */
                   1156: 
                   1157:        _CTu, _CTu, _CTu, _CTu,                         /* 0x54..0x57 */
                   1158: 
                   1159:        _CTu, _CTu, _CTu, _CTp,                         /* 0x58..0x5B */
                   1160: 
                   1161:        _CTp, _CTp, _CTp, _CTp,                         /* 0x5C..0x5F */
                   1162: 
                   1163: 
                   1164: 
                   1165:        _CTp, _CTl|_CTx, _CTl|_CTx, _CTl|_CTx,          /* 0x60..0x63 */
                   1166: 
                   1167:        _CTl|_CTx, _CTl|_CTx, _CTl|_CTx, _CTl,          /* 0x64..0x67 */
                   1168: 
                   1169:        _CTl, _CTl, _CTl, _CTl,                         /* 0x68..0x6B */
                   1170: 
                   1171:        _CTl, _CTl, _CTl, _CTl,                         /* 0x6C..0x6F */
                   1172: 
                   1173: 
                   1174: 
                   1175:        _CTl, _CTl, _CTl, _CTl,                         /* 0x70..0x73 */
                   1176: 
                   1177:        _CTl, _CTl, _CTl, _CTl,                         /* 0x74..0x77 */
                   1178: 
                   1179:        _CTl, _CTl, _CTl, _CTp,                         /* 0x78..0x7B */
                   1180: 
                   1181:        _CTp, _CTp, _CTp, _CTc,                         /* 0x7C..0x7F */
                   1182: 
                   1183: 
                   1184: 
                   1185:        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80..0x8F */
                   1186: 
                   1187:        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90..0x9F */
                   1188: 
                   1189:        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xA0..0xAF */
                   1190: 
                   1191:        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xB0..0xBF */
                   1192: 
                   1193:        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xC0..0xCF */
                   1194: 
                   1195:        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xD0..0xDF */
                   1196: 
                   1197:        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xE0..0xEF */
                   1198: 
                   1199:        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0  /* 0xF0..0xFF */
                   1200: 
                   1201: };
                   1202: 
                   1203: 
                   1204: 
                   1205: int toupper(c)
                   1206: 
                   1207:        int c;
                   1208: 
                   1209: {
                   1210: 
                   1211:        return(islower(c) ? (c ^ 0x20) : (c));
                   1212: 
                   1213: }
                   1214: 
                   1215: 
                   1216: 
                   1217: int tolower(c)
                   1218: 
                   1219:        int c;
                   1220: 
                   1221: {
                   1222: 
                   1223:        return(isupper(c) ? (c ^ 0x20) : (c));
                   1224: 
                   1225: }
                   1226: 
                   1227: 
                   1228: 
                   1229: /*
                   1230: 
                   1231:  * converts a decimal string to an integer
                   1232: 
                   1233:  */
                   1234: 
                   1235: 
                   1236: 
1.1.1.2   root     1237: long
1.1       root     1238: 
1.1.1.2   root     1239: atol(s)
1.1       root     1240: 
                   1241:        const char *s;
                   1242: 
                   1243: {
                   1244: 
1.1.1.2   root     1245:        long d = 0;
1.1       root     1246: 
                   1247:        int negflag = 0;
                   1248: 
                   1249:        int c;
                   1250: 
                   1251: 
                   1252: 
                   1253:        while (*s && isspace(*s)) s++;
                   1254: 
                   1255:        while (*s == '-' || *s == '+') {
                   1256: 
                   1257:                if (*s == '-')
                   1258: 
                   1259:                        negflag ^= 1;
                   1260: 
                   1261:                s++;
                   1262: 
                   1263:        }
                   1264: 
1.1.1.2   root     1265:        while ((c = *s++) != 0 && isdigit(c)) {
1.1       root     1266: 
                   1267:                d = 10 * d + (c - '0');
                   1268: 
                   1269:        }
                   1270: 
                   1271:        if (negflag) d = -d;
                   1272: 
                   1273:        return d;
                   1274: 
                   1275: }
                   1276: 
                   1277: 
                   1278: 
                   1279: #endif /* OWN_LIB */
                   1280: 

unix.superglobalmegacorp.com

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