]> git.pond.sub.org Git - empserver/blob - src/util/empsched.c
Update copyright notice
[empserver] / src / util / empsched.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2016, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  empsched.c: Show the update schedule
28  *
29  *  Known contributors to this file:
30  *     Markus Armbruster, 2007-2010
31  */
32
33 #include <config.h>
34
35 #include <unistd.h>
36 #include <stdio.h>
37 #include <time.h>
38 #include "optlist.h"
39 #include "prototypes.h"
40 #include "version.h"
41
42 #define DFLT_N 16
43 #define MAX_N 1000
44
45 static void
46 print_usage(char *program_name)
47 {
48     printf("Usage: %s [OPTION]... [FILE]\n"
49            "Print the Empire update schedule.\n\n"
50            "  -e CONFIG-FILE  configuration file\n"
51            "                  (default %s)\n"
52            "  -n NUMBER       print at most NUMBER updates (default %d)\n"
53            "  -h              display this help and exit\n"
54            "  -v              display version information and exit\n\n"
55            "If FILE is given, print the schedule defined there instead of\n"
56            "the current schedule.\n",
57            program_name, dflt_econfig, DFLT_N);
58 }
59
60 int
61 main(int argc, char *argv[])
62 {
63     char *config_file = NULL;
64     char *in_file;
65     unsigned long n = DFLT_N;
66     time_t sched[MAX_N + 1], anchor;
67     int opt, i;
68
69     while ((opt = getopt(argc, argv, "e:n:hv")) != EOF) {
70         switch (opt) {
71         case 'e':
72             config_file = optarg;
73             break;
74         case 'n':
75             n = strtoul(optarg, NULL, 10);
76             if (n > MAX_N) {
77                 fprintf(stderr, "%s: can't print more than %d updates",
78                         argv[0], MAX_N);
79                 exit(1);
80             }
81             break;
82         case 'h':
83             print_usage(argv[0]);
84             exit(0);
85         case 'v':
86             printf("%s\n\n%s", version, legal);
87             exit(0);
88         default:
89             fprintf(stderr, "Try -h for help.\n");
90             exit(1);
91         }
92     }
93
94     if (argc - optind > 1) {
95         fprintf(stderr, "%s: extra operand %s\n", argv[0], argv[optind+1]);
96         fprintf(stderr, "Try -h for help.\n");
97         exit(1);
98     }
99
100     if (emp_config(config_file) < 0)
101         exit(1);
102
103     if (!argv[optind])
104         in_file = schedulefil;
105     else if (!strcmp(argv[optind], "-"))
106         in_file = NULL;
107     else
108         in_file = argv[optind];
109
110     anchor = (time(NULL) + 59) / 60 * 60;
111     if (read_schedule(in_file, sched, n + 1, 0, anchor) < 0)
112         exit(1);
113
114     for (i = 0; sched[i]; i++)
115         printf("%s", ctime(&sched[i]));
116
117     return 0;
118 }