]> git.pond.sub.org Git - empserver/blobdiff - src/util/files.c
Update copyright notice
[empserver] / src / util / files.c
index c4436977e1a81190442ba469e9c29e82983308b2..a2ae277cf5eba860309969dd9489c1e8dd125b6b 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2013, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
@@ -14,8 +14,7 @@
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
  *  ---
  *
  *  files.c: Create all the misc files
- * 
+ *
  *  Known contributors to this file:
  *     Thomas Ruschak
  *     Ken Stevens, 1995
  *     Steve McClure, 1998
+ *     Markus Armbruster, 2004-2011
  */
 
 #include <config.h>
 
-#include <fcntl.h>
 #include <errno.h>
-#if defined(_WIN32)
-#include <direct.h>
-#include <io.h>
-#include "../lib/gen/getopt.h"
-#else
 #include <sys/stat.h>
 #include <unistd.h>
-#endif
-
 #include "file.h"
-#include "land.h"
-#include "misc.h"
 #include "nat.h"
-#include "nuke.h"
 #include "optlist.h"
-#include "plane.h"
-#include "power.h"
 #include "prototypes.h"
-#include "sect.h"
-#include "ship.h"
 #include "tel.h"
-#include "trade.h"
 #include "version.h"
 
-static void file_sct_init(coord, coord, struct sctstr *ptr,
-                         time_t timestamp);
-
 static void
 print_usage(char *program_name)
 {
@@ -80,17 +61,11 @@ int
 main(int argc, char *argv[])
 {
     char buf[255];
-    char *filename;
-    int x, y;
     struct natstr nat;
-    struct realmstr realm;
-    struct sctstr sct;
-    int i, j;
-    char *map;
+    int i;
     int opt;
     char *config_file = NULL;
     int force = 0;
-    time_t current_time = time(NULL);
 
     while ((opt = getopt(argc, argv, "e:fhv")) != EOF) {
        switch (opt) {
@@ -107,97 +82,64 @@ main(int argc, char *argv[])
            printf("%s\n\n%s", version, legal);
            exit(0);
        default:
-           print_usage(argv[0]);
+           fprintf(stderr, "Try -h for help.\n");
            exit(1);
        }
     }
 
-    if (emp_config(config_file) < 0)
+    if (argv[optind]) {
+       fprintf(stderr, "%s: does not take operands\n", argv[0]);
+       fprintf(stderr, "Try -h for help.\n");
        exit(1);
+    }
 
-    ef_init();
+    empfile_init();
+    if (emp_config(config_file) < 0)
+       exit(1);
+    empfile_fixup();
 
-    if (mkdir(gamedir, 0770) < 0 && errno != EEXIST) {
-       perror(gamedir);
-       printf("Can't make game directory\n");
+    if (mkdir(gamedir, S_IRWXU | S_IRWXG) < 0 && errno != EEXIST) {
+       fprintf(stderr, "Can't make game directory %s (%s)\n",
+               gamedir, strerror(errno));
        exit(1);
     }
     if (chdir(gamedir)) {
-       fprintf(stderr, "Can't chdir to %s (%s)\n", gamedir, strerror(errno));
+       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",
+       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);
+       printf("continue? ");
+       if (!fgets(buf, sizeof(buf), stdin) || (*buf != 'y' && *buf != 'Y'))
+           exit(1);
     }
+
     for (i = 0; i < EF_MAX; i++) {
        if (!EF_IS_GAME_STATE(i))
            continue;
-       if (!ef_open(i, EFF_CREATE)) {
-           perror("ef_open");
+       if (!ef_open(i, EFF_CREATE | EFF_NOTIME))
            exit(1);
-       }
     }
-    memset(&nat, 0, sizeof(nat));
-    nat.ef_type = EF_NATION;
-    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;
-    nat.nat_flags |= NF_FLASH;
-    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;
-       putnat((&nat));
+
+    if (mkdir(teldir, S_IRWXU | S_IRWXG) < 0 && errno != EEXIST) {
+       fprintf(stderr, "Can't make telegram directory %s (%s)\n",
+               teldir, strerror(errno));
+       exit(1);
     }
-    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 (mailbox_create(mailbox(buf, i)) < 0)
+           exit(1);
     }
-    if (mkdir(teldir, 0770) < 0 && errno != EEXIST) {
-       perror(teldir);
-       printf("Can't make telegram directory\n");
+    if (mailbox_create(annfil) < 0)
        exit(1);
-    }
-    for (x = MAXNOC - 1; x >= 0; x--) {
-       filename = mailbox(buf, x);
-       close(creat(filename, 0660));
-    }
-    close(creat(timestampfil, 0660));
-    close(creat(annfil, 0660));
 
-    /* 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 & 1), y, &sct, current_time);
-           putsect(&sct);
-       }
-    }
-    map = calloc(WORLD_X * WORLD_Y / 2, sizeof(*map));
-    for (i = 0; i < MAXNOC; i++) {
-       ef_write(EF_MAP, i, map);
-    }
-    for (i = 0; i < MAXNOC; i++) {
-       ef_write(EF_BMAP, i, map);
-    }
+    nat_reset(&nat, 0, "POGO", "peter", STAT_GOD);
+    putnat(&nat);
+    printf("All praise to %s!\n", nat.nat_cnam);
+
     for (i = 0; i < EF_MAX; i++) {
        if (!EF_IS_GAME_STATE(i))
            continue;
@@ -206,18 +148,3 @@ main(int argc, char *argv[])
 
     exit(0);
 }
-
-static void
-file_sct_init(coord x, coord y, struct sctstr *ptr, time_t timestamp)
-{
-    struct sctstr *sp = (struct sctstr *)ptr;
-
-    sp->ef_type = EF_SECTOR;
-    sp->sct_x = x;
-    sp->sct_y = y;
-    sp->sct_dist_x = x;
-    sp->sct_dist_y = y;
-    sp->sct_timestamp = timestamp;
-    sp->sct_newtype = sp->sct_type = SCT_WATER;
-    sp->sct_coastal = 1;
-}