[BACK]Return to setup.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / games / phantasia

Annotation of src/games/phantasia/setup.c, Revision 1.21

1.21    ! dholland    1: /*     $NetBSD: setup.c,v 1.20 2009/05/27 17:44:38 dholland Exp $      */
1.3       cgd         2:
1.1       jtc         3: /*
                      4:  * setup.c - set up all files for Phantasia
1.17      dogcow      5:  * n.b.: this is used at build-time - i.e. during build.sh.
1.1       jtc         6:  */
1.18      lukem       7: #ifdef __NetBSD__
1.15      perry       8: #include <sys/cdefs.h>
1.18      lukem       9: #endif
                     10:
1.21    ! dholland   11: #include <sys/types.h>
1.1       jtc        12: #include <sys/stat.h>
1.10      jsm        13: #include <fcntl.h>
1.21    ! dholland   14: #include <setjmp.h>
        !            15: #include <stdio.h>
        !            16: #include <stdlib.h>
        !            17: #include <string.h>
        !            18: #include <unistd.h>
        !            19:
        !            20: #ifndef __dead /* Not NetBSD */
        !            21: #define __dead
        !            22: #endif
        !            23:
        !            24: #include "phantdefs.h"
        !            25: #include "phantstruct.h"
        !            26: #include "phantglobs.h"
        !            27: #include "pathnames.h"
1.10      jsm        28:
1.12      wiz        29: int main(int, char *[]);
1.16      perry      30: void Error(const char *, const char *) __dead;
1.12      wiz        31: double drandom(void);
1.10      jsm        32:
1.1       jtc        33: /**/
                     34: /************************************************************************
                     35: /
                     36: / FUNCTION NAME: main()
                     37: /
                     38: / FUNCTION: setup files for Phantasia 3.3.2
                     39: /
                     40: / AUTHOR: E. A. Estes, 12/4/85
                     41: /
                     42: / ARGUMENTS: none
                     43: /
                     44: / RETURN VALUE: none
                     45: /
                     46: / MODULES CALLED: time(), exit(), stat(), Error(), creat(), close(), fopen(),
                     47: /      fgets(), floor(), srandom(), umask(), drandom(), strcpy(), getuid(),
                     48: /      unlink(), fwrite(), fclose(), sscanf(), printf(), strlen(), fprintf()
                     49: /
                     50: / GLOBAL INPUTS: Curmonster, _iob[], Databuf[], *Monstfp, Enrgyvoid
                     51: /
                     52: / GLOBAL OUTPUTS: Curmonster, Databuf[], *Monstfp, Enrgyvoid
                     53: /
                     54: / DESCRIPTION:
                     55: /
                     56: /      This program tries to verify the parameters specified in
                     57: /      the Makefile.
                     58: /
                     59: /      Create all necessary files.  Note that nothing needs to be
                     60: /      put in these files.
                     61: /      Also, the monster binary data base is created here.
                     62: /
1.10      jsm        63: / ************************************************************************/
1.1       jtc        64:
1.9       jsm        65: static const char *const files[] = {           /* all files to create */
1.1       jtc        66:        _PATH_MONST,
                     67:        _PATH_PEOPLE,
                     68:        _PATH_MESS,
                     69:        _PATH_LASTDEAD,
                     70:        _PATH_MOTD,
                     71:        _PATH_GOLD,
                     72:        _PATH_VOID,
                     73:        _PATH_SCORE,
                     74:        NULL,
                     75: };
                     76:
1.9       jsm        77: const char *monsterfile = "monsters.asc";
1.1       jtc        78:
                     79: int
1.19      dholland   80: main(int argc, char *argv[])
1.1       jtc        81: {
1.11      simonb     82:        const char *const *filename; /* for pointing to file names */
                     83:        int             fd;             /* file descriptor */
                     84:        FILE            *fp;                    /* for opening files */
1.1       jtc        85:        struct stat     fbuf;           /* for getting files statistics */
                     86:        int ch;
1.11      simonb     87:        char *path;
1.1       jtc        88:
1.6       lukem      89:        while ((ch = getopt(argc, argv, "m:")) != -1)
1.1       jtc        90:                switch(ch) {
                     91:                case 'm':
                     92:                        monsterfile = optarg;
                     93:                        break;
                     94:                case '?':
                     95:                default:
                     96:                        break;
                     97:                }
                     98:        argc -= optind;
                     99:        argv += optind;
                    100:
1.4       cgd       101:     srandom((unsigned) time(NULL));    /* prime random numbers */
1.1       jtc       102:
                    103:     umask(0117);               /* only owner can read/write created files */
                    104:
                    105:     /* try to create data files */
                    106:     filename = &files[0];
                    107:     while (*filename != NULL)
                    108:        /* create each file */
                    109:        {
1.11      simonb    110:        path = strrchr(*filename, '/') + 1;
1.2       pk        111:        if (stat(path, &fbuf) == 0)
1.1       jtc       112:            /* file exists; remove it */
                    113:            {
1.2       pk        114:            if (unlink(path) < 0)
                    115:                Error("Cannot unlink %s.\n", path);
1.1       jtc       116:                /*NOTREACHED*/
                    117:            }
                    118:
1.2       pk        119:        if ((fd = creat(path, 0660)) < 0)
                    120:            Error("Cannot create %s.\n", path);
1.1       jtc       121:            /*NOTREACHED*/
                    122:
                    123:        close(fd);                      /* close newly created file */
                    124:
                    125:        ++filename;                     /* process next file */
                    126:        }
                    127:
1.14      jmc       128:     /* Initialize an empty file placeholder for the grail location. */
1.2       pk        129:     if ((fp = fopen(path, "w")) == NULL)
1.14      jmc       130:        Error("Cannot create %s.\n", path);
                    131:     fclose(fp);
1.1       jtc       132:
                    133:     /* create binary monster data base */
1.11      simonb    134:     path = strrchr(_PATH_MONST, '/') + 1;
1.2       pk        135:     if ((Monstfp = fopen(path, "w")) == NULL)
                    136:        Error("Cannot update %s.\n", path);
1.1       jtc       137:     else
                    138:        {
                    139:        if ((fp = fopen(monsterfile, "r")) == NULL)
                    140:            {
                    141:            fclose(Monstfp);
                    142:            Error("cannot open %s to create monster database.\n", "monsters.asc");
                    143:            }
                    144:        else
                    145:            {
                    146:            Curmonster.m_o_strength =
                    147:            Curmonster.m_o_speed =
                    148:            Curmonster.m_maxspeed =
                    149:            Curmonster.m_o_energy =
                    150:            Curmonster.m_melee =
                    151:            Curmonster.m_skirmish = 0.0;
                    152:
                    153:            while (fgets(Databuf, SZ_DATABUF, fp) != NULL)
                    154:                /* read in text file, convert to binary */
                    155:                {
                    156:                sscanf(&Databuf[24], "%lf%lf%lf%lf%lf%d%d%lf",
                    157:                    &Curmonster.m_strength, &Curmonster.m_brains,
                    158:                    &Curmonster.m_speed, &Curmonster.m_energy,
                    159:                    &Curmonster.m_experience, &Curmonster.m_treasuretype,
                    160:                    &Curmonster.m_type, &Curmonster.m_flock);
                    161:                Databuf[24] = '\0';
                    162:                strcpy(Curmonster.m_name, Databuf);
                    163:                fwrite((char *) &Curmonster, SZ_MONSTERSTRUCT, 1, Monstfp);
                    164:                }
                    165:            fclose(fp);
1.10      jsm       166:            fflush(Monstfp);
                    167:            if (ferror(Monstfp))
                    168:                Error("Writing %s.\n", path);
1.1       jtc       169:            fclose(Monstfp);
                    170:            }
                    171:        }
                    172:
                    173: #ifdef MAKE_INSTALLS_THIS_AND_DOESNT_WANT_TO_HEAR_ABOUT_IT
                    174:     /* write to motd file */
                    175:     printf("One line 'motd' ? ");
                    176:     if (fgets(Databuf, SZ_DATABUF, stdin) == NULL)
                    177:        Databuf[0] = '\0';
1.11      simonb    178:     path = strrchr(_PATH_MOTD, '/') + 1;
1.2       pk        179:     if ((fp = fopen(path, "w")) == NULL)
                    180:        Error("Cannot update %s.\n", path);
1.1       jtc       181:     else
                    182:        {
                    183:        fwrite(Databuf, sizeof(char), strlen(Databuf), fp);
                    184:        fclose(fp);
                    185:        }
                    186:
                    187:     /* report compile-time options */
                    188:     printf("Compiled options:\n\n");
                    189:     printf("Phantasia destination directory:  %s\n", _PATH_PHANTDIR);
                    190:     printf("Wizard: root UID: 0\n");
                    191:
                    192: #ifdef BSD41
                    193:     printf("Compiled for BSD 4.1\n");
                    194: #endif
                    195:
                    196: #ifdef BSD42
                    197:     printf("Compiled for BSD 4.2\n");
                    198: #endif
                    199:
                    200: #ifdef SYS3
                    201:     printf("Compiled for System III\n");
                    202: #endif
                    203:
                    204: #ifdef SYS5
                    205:     printf("Compiled for System V\n");
                    206: #endif
                    207: #endif
                    208:
                    209:     exit(0);
                    210:     /*NOTREACHED*/
                    211: }
                    212: /**/
                    213: /************************************************************************
                    214: /
                    215: / FUNCTION NAME: Error()
                    216: /
                    217: / FUNCTION: print an error message, and exit
                    218: /
                    219: / AUTHOR: E. A. Estes, 12/4/85
                    220: /
                    221: / ARGUMENTS:
                    222: /      char *str - format string for printf()
                    223: /      char *file - file which caused error
                    224: /
                    225: / RETURN VALUE: none
                    226: /
                    227: / MODULES CALLED: exit(), perror(), fprintf()
                    228: /
                    229: / GLOBAL INPUTS: _iob[]
                    230: /
                    231: / GLOBAL OUTPUTS: none
                    232: /
                    233: / DESCRIPTION:
                    234: /      Print an error message, then exit.
                    235: /
1.10      jsm       236: / ************************************************************************/
1.1       jtc       237:
1.10      jsm       238: void
1.19      dholland  239: Error(const char *str, const char *file)
1.1       jtc       240: {
                    241:     fprintf(stderr, "Error: ");
                    242:     fprintf(stderr, str, file);
                    243:     perror(file);
                    244:     exit(1);
                    245:     /*NOTREACHED*/
                    246: }
                    247: /**/
                    248: /************************************************************************
                    249: /
                    250: / FUNCTION NAME: drandom()
                    251: /
                    252: / FUNCTION: return a random number
                    253: /
                    254: / AUTHOR: E. A. Estes, 2/7/86
                    255: /
                    256: / ARGUMENTS: none
                    257: /
                    258: / RETURN VALUE: none
                    259: /
                    260: / MODULES CALLED: random()
                    261: /
                    262: / GLOBAL INPUTS: none
                    263: /
                    264: / GLOBAL OUTPUTS: none
                    265: /
                    266: / DESCRIPTION:
                    267: /
1.10      jsm       268: / ************************************************************************/
1.1       jtc       269:
                    270: double
1.19      dholland  271: drandom(void)
1.1       jtc       272: {
                    273:     if (sizeof(int) != 2)
                    274:        return((double) (random() & 0x7fff) / 32768.0);
                    275:     else
                    276:        return((double) random() / 32768.0);
                    277: }

CVSweb <webmaster@jp.NetBSD.org>