]> git.pond.sub.org Git - empserver/blobdiff - src/util/files.c
Fix trailing whitespace
[empserver] / src / util / files.c
index 189ef1a701d1160100e7d7635f098a65d925f039..b68fc06b3d5b4d0e70576f9927c6d147bf71ea20 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
  *  files.c: Create all the misc files
- * 
+ *
  *  Known contributors to this file:
  *     Thomas Ruschak
  *     Ken Stevens, 1995
  *     Steve McClure, 1998
  */
 
-#include <sys/types.h>
-#include <fcntl.h>
-#if !defined(_WIN32)
-#include <sys/uio.h>
-#include <sys/file.h>
-#else
-#include <direct.h>
-#include "../lib/gen/getopt.h"
-#endif
-#include <errno.h>
-#include <stdarg.h>
-#include <stdio.h>
+#include <config.h>
 
-#include "options.h"
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include "file.h"
+#include "game.h"
+#include "land.h"
 #include "misc.h"
-#include "sect.h"
 #include "nat.h"
-#include "ship.h"
-#include "land.h"
-#include "plane.h"
 #include "nuke.h"
+#include "optlist.h"
+#include "plane.h"
 #include "power.h"
-#include "trade.h"
-#include "file.h"
-#include "tel.h"
 #include "prototypes.h"
-#include "optlist.h"
+#include "sect.h"
+#include "ship.h"
+#include "tel.h"
+#include "trade.h"
+#include "version.h"
 
-static void file_sct_init(coord x, coord y, s_char *ptr);
+static void file_sct_init(coord, coord, struct sctstr *ptr);
 
 static void
 print_usage(char *program_name)
 {
-    printf("Usage: %s -f -e econfig_file\n", program_name);
-    printf("-f force mode\n");
+    printf("Usage: %s [OPTION]...\n"
+          "  -e CONFIG-FILE  configuration file\n"
+          "                  (default %s)\n"
+          "  -f              force overwrite of existing game\n"
+          "  -h              display this help and exit\n"
+          "  -v              display version information and exit\n",
+          program_name, dflt_econfig);
 }
 
 int
 main(int argc, char *argv[])
 {
-    s_char buf[255];
-    s_char *filename;
+    char buf[255];
+    char *filename;
     int x, y;
+    struct gamestr game;
     struct natstr nat;
+    struct realmstr realm;
     struct sctstr sct;
-    int i;
-    s_char *map;
+    int i, j;
+    char *map;
     int opt;
     char *config_file = NULL;
     int force = 0;
 
-    while ((opt = getopt(argc, argv, "e:f")) != EOF) {
+    while ((opt = getopt(argc, argv, "e:fhv")) != EOF) {
        switch (opt) {
        case 'e':
            config_file = optarg;
@@ -92,95 +93,111 @@ main(int argc, char *argv[])
        case 'f':
            force = 1;
            break;
+       case 'h':
+           print_usage(argv[0]);
+           exit(0);
+       case 'v':
+           printf("%s\n\n%s", version, legal);
+           exit(0);
        default:
            print_usage(argv[0]);
-           exit(-1);
+           exit(1);
        }
     }
 
+    empfile_init();
     if (emp_config(config_file) < 0)
        exit(1);
+    empfile_fixup();
 
-    empfile[EF_MAP].size = (WORLD_X * WORLD_Y) / 2;
-    empfile[EF_BMAP].size = (WORLD_X * WORLD_Y) / 2;
-
-    if (access(datadir, F_OK) < 0 && mkdir(datadir, 0750) < 0) {
-       perror(datadir);
+    if (mkdir(gamedir, S_IRWXU | S_IRWXG) < 0 && errno != EEXIST) {
+       perror(gamedir);
        printf("Can't make game directory\n");
        exit(1);
     }
-    if (chdir(datadir)) {
-       fprintf(stderr, "Can't chdir to %s (%s)\n", datadir, strerror(errno));
+    if (chdir(gamedir)) {
+       fprintf(stderr, "Can't chdir to %s (%s)\n", gamedir, strerror(errno));
        exit(EXIT_FAILURE);
     }
 
     if (!force) {
-       printf("WARNING: this blasts the existing game in %s (if any)\n",
-          datadir);
-       printf("continue? ");
-       fgets(buf, sizeof(buf), stdin);
-       if (*buf != 'y' && *buf != 'Y')
-           exit(1);
+       printf("WARNING: this blasts the existing game in %s (if any)\n",
+              gamedir);
+       printf("continue? ");
+       fgets(buf, sizeof(buf), stdin);
+       if (*buf != 'y' && *buf != 'Y')
+           exit(1);
     }
     for (i = 0; i < EF_MAX; i++) {
-       if (!ef_open(i, O_RDWR | O_CREAT | O_TRUNC, 0)) {
+       if (!EF_IS_GAME_STATE(i))
+           continue;
+       if (!ef_open(i, EFF_CREATE | EFF_NOTIME, -1)) {
            perror("ef_open");
            exit(1);
        }
     }
+    memset(&game, 0, sizeof(game));
+    game.ef_type = EF_GAME;
+    game.game_turn = 1;
+    ef_write(EF_GAME, 0, &game);
     memset(&nat, 0, sizeof(nat));
     nat.ef_type = EF_NATION;
     strcpy(nat.nat_cnam, "POGO");
     strcpy(nat.nat_pnam, "peter");
-    nat.nat_stat = STAT_INUSE | STAT_NORM | STAT_GOD | STAT_ABS;
+    nat.nat_stat = STAT_GOD;
     nat.nat_btu = 255;
     nat.nat_money = 123456789;
     nat.nat_cnum = 0;
     nat.nat_flags |= NF_FLASH;
-    for (i = 0; i <= PRI_MAX; i++)
-       nat.nat_priorities[i] = -1;
     putnat((&nat));
     printf("All praise to %s!\n", nat.nat_cnam);
     memset(&nat, 0, sizeof(nat));
     for (i = 1; i < MAXNOC; i++) {
        nat.ef_type = EF_NATION;
-       nat.nat_cnum = i;
+       nat.nat_cnum = nat.nat_uid = i;
+       nat.nat_seqno = 0;
        putnat((&nat));
     }
-    ef_close(EF_NATION);
-    if (access(teldir, F_OK) < 0 && mkdir(teldir, 0750) < 0) {
+    memset(&realm, 0, sizeof(realm));
+    realm.ef_type = EF_REALM;
+    for (i = 0; i < MAXNOC; i++) {
+       realm.r_cnum = i;
+       for (j = 0; j < MAXNOR; j++) {
+           realm.r_realm = j;
+           realm.r_uid = (i * MAXNOR) + j;
+           realm.r_seqno = 0;
+           putrealm(&realm);
+       }
+    }
+    if (mkdir(teldir, S_IRWXU | S_IRWXG) < 0 && errno != EEXIST) {
        perror(teldir);
        printf("Can't make telegram directory\n");
        exit(1);
     }
     for (x = MAXNOC - 1; x >= 0; x--) {
        filename = mailbox(buf, x);
-       close(creat(filename, 0600));
-       chmod(filename, 0600);
+       close(creat(filename, S_IRWUG));
     }
-    close(creat(timestampfil, 0600));
-    close(creat(annfil, 0600));
-    chmod(infodir, 0750);
-    chmod(datadir, 0770);
-    chmod(teldir, 0770);
+    close(creat(annfil, S_IRWUG));
 
     /* create a zero-filled sector file */
     memset(&sct, 0, sizeof(sct));
     for (y = 0; y < WORLD_Y; y++) {
        for (x = 0; x < WORLD_X / 2; x++) {
-           file_sct_init(x * 2 + (y & 01), y, (s_char *)&sct);
+           file_sct_init(x * 2 + (y & 1), y, &sct);
            putsect(&sct);
        }
     }
-    map = calloc(WORLD_X * WORLD_Y / 2, sizeof(*map));
+    map = calloc(WORLD_SZ(), sizeof(*map));
     for (i = 0; i < MAXNOC; i++) {
        ef_write(EF_MAP, i, map);
     }
-    map = calloc(WORLD_X * WORLD_Y / 2, sizeof(*map));
     for (i = 0; i < MAXNOC; i++) {
        ef_write(EF_BMAP, i, map);
     }
     for (i = 0; i < EF_MAX; i++) {
+       if (!EF_IS_GAME_STATE(i))
+           continue;
        ef_close(i);
     }
 
@@ -188,13 +205,17 @@ main(int argc, char *argv[])
 }
 
 static void
-file_sct_init(coord x, coord y, s_char *ptr)
+file_sct_init(coord x, coord y, struct sctstr *ptr)
 {
     struct sctstr *sp = (struct sctstr *)ptr;
 
     sp->ef_type = EF_SECTOR;
+    sp->sct_uid = XYOFFSET(x, y);
+    sp->sct_seqno = 0;
     sp->sct_x = x;
     sp->sct_y = y;
     sp->sct_dist_x = x;
     sp->sct_dist_y = y;
+    sp->sct_newtype = sp->sct_type = SCT_WATER;
+    sp->sct_coastal = 1;
 }