(linenum→info "unix/slp.c:2238")

bsd-games/2.17/phantasia/setup.c

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