]> git.pond.sub.org Git - empserver/blobdiff - src/util/files.c
COPYING duplicates information from README. Remove. Move GPL from
[empserver] / src / util / files.c
index 9faa4009daecf9f18a99a6d7912a24d9b1a0c1bb..0f495e720462ba6fcaa4d64dc13c5ef1fb489f62 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -19,9 +19,9 @@
  *
  *  ---
  *
- *  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.
  *
  *  ---
  *
@@ -33,9 +33,7 @@
  *     Steve McClure, 1998
  */
 
-#if defined(aix) || defined(linux) || defined(solaris)
-#include <unistd.h>
-#endif /* aix or linux */
+#include <config.h>
 
 #include <sys/types.h>
 #include <fcntl.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 "options.h"
 #include "misc.h"
-#include "var.h"
 #include "sect.h"
 #include "nat.h"
 #include "ship.h"
 #include "prototypes.h"
 #include "optlist.h"
 
-static void file_sct_init(coord x, coord y, s_char *ptr);
+static void file_sct_init(coord x, coord y, s_char *ptr,
+                         time_t timestamp);
+
+static void
+print_usage(char *program_name)
+{
+    printf("Usage: %s -f -e econfig_file\n", program_name);
+    printf("-f force mode\n");
+}
 
 int
 main(int argc, char *argv[])
 {
     s_char buf[255];
-    s_char tbuf[512];
     s_char *filename;
     int x, y;
     struct natstr nat;
+    struct realmstr realm;
     struct sctstr sct;
-    int i;
+    int i, j;
     s_char *map;
-    extern char *optarg;
     int opt;
     char *config_file = NULL;
     int force = 0;
-    
-#if !defined(_WIN32)
+    time_t current_time = time(NULL);
+
     while ((opt = getopt(argc, argv, "e:f")) != EOF) {
        switch (opt) {
        case 'e':
@@ -91,49 +96,48 @@ main(int argc, char *argv[])
        case 'f':
            force = 1;
            break;
+       default:
+           print_usage(argv[0]);
+           exit(-1);
        }
     }
-#endif
 
-    /* Try to use the existing data directory */
-    if (config_file == NULL) {
-       sprintf(tbuf, "%s/econfig", datadir);
-       config_file = tbuf;
-    }
-    emp_config(config_file);
-    empfile[EF_MAP].size = (WORLD_X * WORLD_Y) / 2;
-    empfile[EF_BMAP].size = (WORLD_X * WORLD_Y) / 2;
+    if (emp_config(config_file) < 0)
+       exit(1);
+
+    ef_init();
 
-#if !defined(_WIN32)
     if (access(datadir, F_OK) < 0 && mkdir(datadir, 0750) < 0) {
-#else
-    if (_access(datadir, 06) < 0 && _mkdir(datadir) < 0) {
-#endif
        perror(datadir);
        printf("Can't make game directory\n");
        exit(1);
     }
+    if (chdir(datadir)) {
+       fprintf(stderr, "Can't chdir to %s (%s)\n", datadir, 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) - 1, stdin);
+       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) < 0) {
+       if (!EF_IS_GAME_STATE(i))
+           continue;
+       if (!ef_open(i, EFF_CREATE)) {
            perror("ef_open");
            exit(1);
        }
     }
     memset(&nat, 0, sizeof(nat));
     nat.ef_type = EF_NATION;
-    if (nat.nat_cnam[0] == 0)
-       strcpy(nat.nat_cnam, "POGO");
-    if (nat.nat_pnam[0] == 0)
-       strcpy(nat.nat_pnam, "peter");
-    nat.nat_stat = STAT_INUSE | STAT_NORM | STAT_GOD | STAT_ABS;
+    strcpy(nat.nat_cnam, "POGO");
+    strcpy(nat.nat_pnam, "peter");
+    nat.nat_stat = STAT_GOD;
     nat.nat_btu = 255;
     nat.nat_money = 123456789;
     nat.nat_cnum = 0;
@@ -148,12 +152,18 @@ main(int argc, char *argv[])
        nat.nat_cnum = i;
        putnat((&nat));
     }
-    ef_close(EF_NATION);
-#if !defined(_WIN32)
+    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_timestamp = current_time;
+           putrealm(&realm);
+       }
+    }
     if (access(teldir, F_OK) < 0 && mkdir(teldir, 0750) < 0) {
-#else
-    if (access(teldir, 06) < 0 && _mkdir(teldir) < 0) {
-#endif
        perror(teldir);
        printf("Can't make telegram directory\n");
        exit(1);
@@ -173,19 +183,22 @@ main(int argc, char *argv[])
     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 & 01), y, (s_char *)&sct,
+               current_time);
            putsect(&sct);
        }
     }
-    map = (s_char *)calloc(WORLD_X * WORLD_Y / 2, sizeof(*map));
+    map = calloc(WORLD_X * WORLD_Y / 2, sizeof(*map));
     for (i = 0; i < MAXNOC; i++) {
        ef_write(EF_MAP, i, map);
     }
-    map = (s_char *)calloc(WORLD_X * WORLD_Y / 2, sizeof(*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);
     }
 
@@ -193,7 +206,7 @@ main(int argc, char *argv[])
 }
 
 static void
-file_sct_init(coord x, coord y, s_char *ptr)
+file_sct_init(coord x, coord y, s_char *ptr, time_t timestamp)
 {
     struct sctstr *sp = (struct sctstr *)ptr;
 
@@ -202,19 +215,7 @@ file_sct_init(coord x, coord y, s_char *ptr)
     sp->sct_y = y;
     sp->sct_dist_x = x;
     sp->sct_dist_y = y;
-}
-
-void
-logerror(s_char *format, ...)
-{
-    va_list ap;
-
-    va_start(ap, format);
-    vfprintf(stderr, format, ap);
-    va_end(ap);
-}
-
-void
-log_last_commands(void)
-{
+    sp->sct_timestamp = timestamp;
+    sp->sct_newtype = sp->sct_type = SCT_WATER;
+    sp->sct_coastal = 1;
 }