Annotation of 43BSDReno/games/chess/Xchess/std.c, revision 1.1.1.1

1.1       root        1: 
                      2: /* This file contains code for X-CHESS.
                      3:    Copyright (C) 1986 Free Software Foundation, Inc.
                      4: 
                      5: This file is part of X-CHESS.
                      6: 
                      7: X-CHESS is distributed in the hope that it will be useful,
                      8: but WITHOUT ANY WARRANTY.  No author or distributor
                      9: accepts responsibility to anyone for the consequences of using it
                     10: or for whether it serves any particular purpose or works at all,
                     11: unless he says so in writing.  Refer to the X-CHESS General Public
                     12: License for full details.
                     13: 
                     14: Everyone is granted permission to copy, modify and redistribute
                     15: X-CHESS, but only under the conditions described in the
                     16: X-CHESS General Public License.   A copy of this license is
                     17: supposed to have been given to you along with X-CHESS so you
                     18: can know your rights and responsibilities.  It should be in a
                     19: file named COPYING.  Among other things, the copyright notice
                     20: and this notice must be preserved on all copies.  */
                     21: 
                     22: 
                     23: /* RCS Info: $Revision: 1.1 $ on $Date: 86/11/01 17:08:40 $
                     24:  *           $Source: /users/faustus/xchess/RCS/std.c,v $
                     25:  * Copyright (c) 1985 Wayne A. Christopher, U. C. Berkeley CAD Group
                     26:  *
                     27:  * Utility routines.
                     28:  */
                     29: 
                     30: #include "std.h"
                     31: 
                     32: #ifndef IBMPC
                     33: #include <sys/types.h>
                     34: #endif not IBMPC
                     35: #ifdef UNIX
                     36: #include <signal.h>
                     37: #include <pwd.h>
                     38: #endif UNIX
                     39: #ifdef BSD
                     40: #include <sys/time.h>
                     41: #include <sys/resource.h>
                     42: #endif BSD
                     43: 
                     44: extern char **environ;
                     45: 
                     46: bool
                     47: prefix(p, s)
                     48:        register char *p, *s;
                     49: {
                     50:        while (*p && (*p == *s))
                     51:                p++, s++;
                     52:        if (!*p)
                     53:                return (true);
                     54:        else
                     55:                return (false);
                     56: }
                     57: 
                     58: /* Create a copy of a string. */
                     59: 
                     60: char *
                     61: copy(str)
                     62:        char *str;
                     63: {
                     64:        char *p, *tmalloc();
                     65:        
                     66:        p = tmalloc(strlen(str) + 1);
                     67:        strcpy(p, str);
                     68:        return(p);
                     69: }
                     70: 
                     71: /* Determine whether sub is a substring of str. */
                     72: 
                     73: bool
                     74: substring(sub, str)
                     75:        register char *str, *sub;
                     76: {
                     77:        register char *s;
                     78: 
                     79:        while(*str) {
                     80:                if(*str == *sub) {
                     81:                        for(s = sub; *s; s++)
                     82:                                if(*s != *str++)
                     83:                                        break;
                     84:                        if(*s == '\0')
                     85:                                return (true);
                     86:                }
                     87:                str++;
                     88:        }
                     89:        return (false);
                     90: }
                     91: 
                     92: /* Malloc num bytes and initialize to zero. Fatal error if the space can't
                     93:  * be malloc'd. 
                     94:  */
                     95: 
                     96: char *
                     97: tmalloc(num)
                     98:        register int num;
                     99: {
                    100:        register char *s;
                    101:        char *malloc();
                    102: 
                    103:        s = malloc((unsigned) num);
                    104:        if (!s) {
                    105:                fatal("malloc: can't allocate %d bytes", num);
                    106:        }
                    107:        bzero(s, num);
                    108:        return(s);
                    109: }
                    110: 
                    111: char *
                    112: trealloc(ptr, num)
                    113:        char *ptr;
                    114:        int num;
                    115: {
                    116:        register char *s;
                    117:        char *realloc();
                    118: 
                    119:        s = realloc(ptr, (unsigned) num);
                    120:        if (!s) {
                    121:                fatal("realloc: can't allocate %d bytes", num);
                    122:        }
                    123:        /* Well, this won't be zeroed... Too bad... */
                    124:        return(s);
                    125: }
                    126: 
                    127: /* Append one character to a string. Don't check for overflow. */
                    128: 
                    129: void
                    130: appendc(s, c)
                    131:        char *s, c;
                    132: {
                    133:        while (*s)
                    134:                s++;
                    135:        *s++ = c;
                    136:        *s = '\0';
                    137:        return;
                    138: }
                    139: 
                    140: int
                    141: scannum(str)
                    142:        char *str;
                    143: {
                    144:        int i = 0;
                    145: 
                    146:        while(isdigit(*str))
                    147:                i = i * 10 + *(str++) - '0';
                    148:        return(i);
                    149: }
                    150: 
                    151: /* Case insensitive prefix. */
                    152: 
                    153: bool
                    154: ciprefix(p, s)
                    155:        register char *p, *s;
                    156: {
                    157:        while (*p) {
                    158:                if ((isupper(*p) ? tolower(*p) : *p) !=
                    159:                    (isupper(*s) ? tolower(*s) : *s))
                    160:                        return(false);
                    161:                p++;
                    162:                s++;
                    163:        }
                    164:        return (true);
                    165: }
                    166: 
                    167: /* Case insensitive strcmp... */
                    168: 
                    169: bool
                    170: cieq(p, s)
                    171:        register char *p, *s;
                    172: {
                    173:        while (*p) {
                    174:                if ((isupper(*p) ? tolower(*p) : *p) !=
                    175:                    (isupper(*s) ? tolower(*s) : *s))
                    176:                        return(false);
                    177:                p++;
                    178:                s++;
                    179:        }
                    180:        return (!*s);
                    181: }
                    182: 
                    183: #ifdef BSD
                    184: 
                    185: /* Return the date. Return value is static data. */
                    186: 
                    187: char *
                    188: datestring()
                    189: {
                    190:        register char *tzn;
                    191:        struct tm *tp;
                    192:        static char tbuf[40];
                    193:        char *ap;
                    194:        struct timeval tv;
                    195:        struct timezone tz;
                    196:        char *timezone(), *asctime();
                    197:        int i;
                    198:        struct tm *localtime();
                    199: 
                    200:        (void) gettimeofday(&tv, &tz);
                    201:        tp = localtime((time_t *) &tv.tv_sec);
                    202:        ap = asctime(tp);
                    203:        tzn = timezone(tz.tz_minuteswest, tp->tm_isdst);
                    204:        sprintf(tbuf, "%.20s", ap);
                    205:        if (tzn)
                    206:                strcat(tbuf, tzn);
                    207:        strcat(tbuf, ap + 19);
                    208:        i = strlen(tbuf);
                    209:        tbuf[i - 1] = '\0';
                    210:        return (tbuf);
                    211: }
                    212: 
                    213: #else BSD
                    214: 
                    215: /* Give it a try... */
                    216: 
                    217: char *
                    218: datestring()
                    219: {
                    220:        long i;
                    221:        static char buf[64];
                    222: 
                    223:        i = time(0);
                    224:        strcpy(buf, ctime(&i));
                    225:        buf[strlen(buf) - 1] = '\0';    /* Kill the nl. */
                    226:        return (buf);
                    227: }
                    228: 
                    229: #endif
                    230: 
                    231: /* How many seconds have elapsed in running time. */
                    232: 
                    233: int
                    234: seconds()
                    235: {
                    236: #ifdef BSD
                    237:        struct rusage ruse;
                    238: 
                    239:        getrusage(RUSAGE_SELF, &ruse);
                    240:        return (ruse.ru_utime.tv_sec);
                    241: #else BSD
                    242: #endif BSD
                    243: }
                    244: 
                    245: /* A few things that may not exist on non-unix systems. */
                    246: 
                    247: #ifndef BSD
                    248: 
                    249: #ifndef index
                    250: 
                    251: char *
                    252: index(s, c)
                    253:        register char *s;
                    254:        register char c;
                    255: {
                    256:        while ((*s != c) && (*s != '\0'))
                    257:                s++;
                    258:        if (*s == '\0')
                    259:                return ((char *) 0);
                    260:        else
                    261:                return (s);
                    262: }
                    263: 
                    264: #endif not index
                    265: 
                    266: #ifndef rindex
                    267: 
                    268: char *
                    269: rindex(s, c)
                    270:        register char *s;
                    271:        register char c;
                    272: {
                    273:        register char *t;
                    274: 
                    275:        for (t = s; *t != '\0'; t++);
                    276:        while ((*t != c) && (t != s))
                    277:                t--;
                    278:        if (t == s)
                    279:                return ((char *) 0);
                    280:        else
                    281:                return (t);
                    282: }
                    283: 
                    284: #endif not rindex
                    285: 
                    286: #ifndef bcopy
                    287: 
                    288: void
                    289: bcopy(from, to, num)
                    290:        register char *from, *to;
                    291:        register int num;
                    292: {
                    293:        while (num-- > 0)
                    294:                *to++ = *from++;
                    295:        return;
                    296: }
                    297: 
                    298: #endif not bcopy
                    299: 
                    300: #ifndef bzero
                    301: 
                    302: void
                    303: bzero(ptr, num)
                    304:        register char *ptr;
                    305:        register int num;
                    306: {
                    307:        while (num-- > 0)
                    308:                *ptr++ = '\0';
                    309:        return;
                    310: }
                    311: 
                    312: #endif not bzero
                    313: 
                    314: /* This might not be around... If not then forget about sorting... */
                    315: 
                    316: void qsort() {}
                    317: 
                    318: #endif BSD
                    319: 
                    320: char *
                    321: gettok(s)
                    322:        char **s;
                    323: {
                    324:        char buf[BSIZE];
                    325:        int i = 0;
                    326: 
                    327:        while (isspace(**s))
                    328:                (*s)++;
                    329:        if (!**s)
                    330:                return (NULL);
                    331:        while (**s && !isspace(**s))
                    332:                buf[i++] = *(*s)++;
                    333:        buf[i] = '\0';
                    334:        while (isspace(**s))
                    335:                (*s)++;
                    336:        return (copy(buf));
                    337: }
                    338: 
                    339: /* Die horribly. */
                    340: 
                    341: /* VARARGS1 */
                    342: void
                    343: fatal(s, args)
                    344:         char *s;
                    345: {
                    346:        fputs("Internal Error: ", stderr);
                    347:        _doprnt(s, &args, stderr);
                    348:        putc('\n', stderr);
                    349: 
                    350:        kill(getpid(), SIGIOT);
                    351:        /* NOTREACHED */
                    352: }
                    353: 
                    354: void
                    355: setenv(name, value)
                    356:        char *name, *value;
                    357: {
                    358:        int i;
                    359:        char **xx, *s;
                    360: 
                    361:        s = tmalloc(strlen(name) + 2);
                    362:        sprintf(s, "%s=", name);
                    363: 
                    364:        /* Copy the old environment... */
                    365:        for (i = 0; environ[i]; i++)
                    366:                if (prefix(s, environ[i]))
                    367:                        break;
                    368:        if (!environ[i]) {
                    369:                xx = (char **) tmalloc((i + 2) * sizeof (char *));
                    370:                for (i = 0; environ[i]; i++)
                    371:                        xx[i] = environ[i];
                    372:                xx[i + 1] = NULL;
                    373:                environ = xx;
                    374:        } else
                    375:                xx = environ;
                    376:        
                    377:        xx[i] = tmalloc(strlen(name) + strlen(value) + 2);
                    378:        sprintf(xx[i], "%s=%s", name, value);
                    379:        return;
                    380: }
                    381: 
                    382: char *
                    383: getusername()
                    384: {
                    385:        int i = getuid();
                    386:        struct passwd *pw = getpwuid(i);
                    387: 
                    388:        return (pw ? pw->pw_name : NULL);
                    389: }
                    390: 
                    391: char *
                    392: gethome()
                    393: {
                    394:        int i = getuid();
                    395:        struct passwd *pw = getpwuid(i);
                    396: 
                    397:        return (pw ? pw->pw_dir : "/strange");
                    398: }
                    399: 
                    400: char *
                    401: tildexpand(s)
                    402:        char *s;
                    403: {
                    404:        struct passwd *pw;
                    405:        char *n, buf[64];
                    406:        int i;
                    407: 
                    408:        if (*s != '~')
                    409:                return (copy(s));
                    410: 
                    411:        for (s++, i = 0; *s != '/'; s++, i++)
                    412:                buf[i] = *s;
                    413:        buf[i] = '\0';
                    414:        if (!i)
                    415:                pw = getpwuid(getuid());
                    416:        else
                    417:                pw = getpwnam(buf);
                    418:        if (!pw)
                    419:                return (s);
                    420:        n = tmalloc(strlen(s) + strlen(pw->pw_dir) + 1);
                    421:        strcpy(n, pw->pw_dir);
                    422:        strcat(n, s);
                    423:        return (n);
                    424: }
                    425: 

unix.superglobalmegacorp.com

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