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

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

unix.superglobalmegacorp.com

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