]> git.pond.sub.org Git - empserver/blob - src/util/empsched.c
New utility program empsched.
[empserver] / src / util / empsched.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  empsched.c: Show the update schedule
29  * 
30  *  Known contributors to this file:
31  *     Markus Armbruster, 2007
32  */
33
34 #include <config.h>
35
36 #if defined(_WIN32)
37 #include "../lib/gen/getopt.h"
38 #else
39 #include <unistd.h>
40 #endif
41 #include <stdio.h>
42 #include "optlist.h"
43 #include "prototypes.h"
44 #include "version.h"
45
46 #define DFLT_N 16
47 #define MAX_N 1000
48
49 static void
50 print_usage(char *program_name)
51 {
52     printf("Usage: %s [OPTION]... [FILE]\n"
53            "Print the Empire update schedule.\n\n"
54            "  -e CONFIG-FILE  configuration file\n"
55            "                  (default %s)\n"
56            "  -n NUMBER       print at most NUMBER updates (default %d)\n"
57            "  -h              display this help and exit\n"
58            "  -v              display version information and exit\n\n"
59            "If FILE is given, print the schedule defined there instead of\n"
60            "the current schedule.\n",
61            program_name, dflt_econfig, DFLT_N);
62 }
63
64 int
65 main(int argc, char *argv[])
66 {
67     char *config_file = NULL;
68     char *in_file;
69     unsigned long n = DFLT_N;
70     time_t sched[MAX_N + 1];
71     int opt, i;
72
73     while ((opt = getopt(argc, argv, "e:n:hv")) != EOF) {
74         switch (opt) {
75         case 'e':
76             config_file = optarg;
77             break;
78         case 'n':
79             n = strtoul(optarg, NULL, 10);
80             if (n > MAX_N) {
81                 fprintf(stderr, "%s: can't print more than %d updates",
82                         argv[0], MAX_N);
83                 exit(1);
84             }
85             break;
86         case 'h':
87             print_usage(argv[0]);
88             exit(0);
89         case 'v':
90             printf("%s\n\n%s", version, legal);
91             exit(0);
92         default:
93             print_usage(argv[0]);
94             exit(1);
95         }
96     }
97
98     if (emp_config(config_file) < 0)
99         exit(1);
100
101     if (!argv[optind])
102         in_file = schedulefil;
103     else if (!strcmp(argv[optind], "-"))
104         in_file = NULL;
105     else
106         in_file = argv[optind];
107
108     read_schedule(in_file, sched, n + 1, 0, 0/* FIXME */);
109
110     for (i = 0; sched[i]; i++)
111         printf("%s", ctime(&sched[i]));
112
113     return 0;
114 }