2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2004, Dave Pare, Jeff Bailey, Thomas Ruschak,
4 * Ken Stevens, Steve McClure
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.
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.
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
22 * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
23 * related information and legal notices. It is expected that any future
24 * projects/authors will amend these files as needed.
28 * info.c: display an info page
30 * Known contributors to this file:
32 * Mike Wise, 1997 - added apropos and case insensitivity
34 * Steve McClure, 1998-2000
52 lowerit(s_char *buf, int n, s_char *orig)
53 { /* converts a string to lower case */
54 /* lower case output buffer */
55 /* size of output buffer */
61 for (i = 0; i < n && *orig; i++) {
62 *tmp++ = tolower(*orig++);
78 s_char filename[1024];
81 name = player->argp[1];
84 * don't let sneaky people go outside the info directory
86 if (NULL != (tmp_name = strrchr(name, '/')))
92 snprintf(filename, sizeof(filename), "%s/%s", infodir, name);
93 fp = fopen(filename, "r");
95 int len = strlen(name);
96 /* may be a "partial" request. */
97 info_dp = opendir(infodir);
99 pr("Can't open info dir\n");
100 logerror("Can't open info dir \"%s\"\n", infodir);
104 while ((dp = readdir(info_dp)) != 0 && fp == 0) {
105 if (strncasecmp(name, dp->d_name, strlen(name)) != 0)
107 sprintf(filename, "%s/%s", infodir, dp->d_name);
108 fp = fopen(filename, "r");
112 pr("Sorry, there is no info on %s\n", name);
116 if (fstat(fileno(fp), &statb) < 0) {
117 pr("Error reading info file for %s\n", name);
118 logerror("Cannot fstat for \"%s\" info file (%s)",
119 filename, strerror(errno));
123 if ((statb.st_mode & S_IFREG) == 0) {
124 pr("Error reading info file for %s\n", name);
125 logerror("The info file \"%s\" is not regular file\n", filename);
129 pr("Information on: %s Last modification date: %s",
130 name, ctime(&statb.st_mtime));
131 while (fgets(buf, sizeof(buf), fp) != 0)
145 s_char filename[1024];
147 long nf, nhf, nl, nlhl, nhl, nll;
152 if (player->argp[1] == 0 || !*player->argp[1]) {
153 pr("Apropos what?\n");
158 if (player->argp[2]) {
159 lhitlim = atoi(player->argp[2]);
164 info_dp = opendir(infodir);
165 if (info_dp == NULL) {
166 pr("Can't open info dir \n");
167 logerror("Can't open info dir \"%s\"", infodir);
171 fbuf = (s_char *)malloc(256);
172 lbuf = (s_char *)malloc(256);
173 lbp = (s_char *)malloc(256);
176 * lower case search string into lbp
178 name = player->argp[1];
179 lowerit(lbp, 256, name);
184 nf = nhf = nl = nhl = 0;
185 while ((dp = readdir(info_dp)) != 0) {
186 snprintf(filename, sizeof(filename), "%s/%s", infodir,
188 fp = fopen(filename, "r");
192 if (fstat(fileno(fp), &statb) < 0) {
193 logerror("Cannot stat for \"%s\" info file (%s)",
194 filename, strerror(errno));
198 if ((statb.st_mode & S_IFREG) == 0) {
199 logerror("The info file \"%s\" is not regular file\n",
204 while (fgets(fbuf, 256, fp)) {
205 lowerit(lbuf, 256, fbuf);
206 if (strstr(lbuf, lbp)) {
208 pr("*** %s ***\n", dp->d_name);
217 * break if too many lines
219 if ((nhl + nlhl) > lhitlim)
238 if ((nhl) > lhitlim) {
239 pr("Limit of %d lines exceeded\n", lhitlim);
241 pr("Found %s in %ld of %ld files and in %ld of %ld lines\n",
242 name, nhf, nf, nhl, nl);
255 s_char filename[1024];
258 name = player->argp[1];
261 * don't let sneaky people go outside the info directory
263 if (NULL != (tmp_name = strrchr(name, '/')))
265 if (NULL != (tmp_name = strrchr(name, '\\')))
267 if (NULL != (tmp_name = strrchr(name, ':')))
273 _snprintf(filename, sizeof(filename) - 1, "%s\\%s", infodir, name);
274 fp = fopen(filename, "r");
276 /* may be a "partial" request. */
278 WIN32_FIND_DATA fData;
279 int len = strlen(name);
280 strcat(filename, "*");
281 hDir = FindFirstFile(filename, &fData);
282 if (hDir == INVALID_HANDLE_VALUE) {
283 switch (GetLastError()) {
284 case ERROR_FILE_NOT_FOUND:
285 pr("Sorry, there is no info on %s\n", name);
288 case ERROR_PATH_NOT_FOUND:
289 pr("Can't open info dir\n");
290 logerror("Can't open info dir \"%s\"", infodir);
293 pr("Error reading info dir\n");
294 logerror("Error (%d) reading info dir(%s)\\file(%s)",
295 infodir, filename, GetLastError());
300 if ((fData.dwFileAttributes != (DWORD)-1) &&
301 ((fData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) ||
302 (fData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) ||
303 (fData.dwFileAttributes == FILE_ATTRIBUTE_READONLY)) &&
304 (strncasecmp(name, fData.cFileName, strlen(name)) == 0)) {
305 _snprintf(filename, sizeof(filename), "%s\\%s", infodir, fData.cFileName);
306 fp = fopen(filename, "r");
308 } while (!fp && FindNextFile(hDir, &fData));
311 pr("Sorry, there is no info on %s\n", name);
316 DWORD fAttrib = GetFileAttributes(filename);
317 if ((fAttrib == (DWORD)-1) || //INVALID_FILE_ATTRIBUTES
318 (fAttrib != FILE_ATTRIBUTE_NORMAL) &&
319 (fAttrib != FILE_ATTRIBUTE_ARCHIVE) &&
320 (fAttrib != FILE_ATTRIBUTE_READONLY)) {
321 pr("Error reading info file for %s\n", name);
322 logerror("The info file \"%s\" is not regular file\n",
329 pr("Information on: %s", name);
330 while (fgets(buf, sizeof(buf), fp) != 0)
340 WIN32_FIND_DATA fData;
345 s_char filename[1024];
346 long nf, nhf, nl, nlhl, nhl, nll;
350 if (player->argp[1] == 0 || !*player->argp[1]) {
351 pr("Apropos what?\n");
356 if (player->argp[2]) {
357 lhitlim = atoi(player->argp[2]);
362 _snprintf(filename, sizeof(filename), "%s\\*",infodir);
363 hDir = FindFirstFile(filename, &fData);
364 if (hDir == INVALID_HANDLE_VALUE) {
365 if (GetLastError() == ERROR_PATH_NOT_FOUND) {
366 pr("Can't open info dir\n");
367 logerror("Can't open info dir \"%s\"", infodir);
369 pr("Error reading info dir\n");
370 logerror("Error (%d) reading info dir(%s)\\file(%s)",
371 infodir, filename, GetLastError());
376 fbuf = (s_char *)malloc(256);
377 lbuf = (s_char *)malloc(256);
378 lbp = (s_char *)malloc(256);
381 * lower case search string into lbp
383 name = player->argp[1];
384 lowerit(lbp, 256, name);
389 nf = nhf = nl = nhl = 0;
391 if ((fData.dwFileAttributes != (DWORD)-1) &&
392 ((fData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) ||
393 (fData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) ||
394 (fData.dwFileAttributes == FILE_ATTRIBUTE_READONLY))) {
395 _snprintf(filename, sizeof(filename), "%s\\%s", infodir,
397 fp = fopen(filename, "r");
401 while (fgets(fbuf, 256, fp)) {
402 lowerit(lbuf, 256, fbuf);
403 if (strstr(lbuf, lbp)) {
405 pr("*** %s ***\n", fData.cFileName);
414 * break if too many lines
416 if ((nhl + nlhl) > lhitlim)
429 } while (FindNextFile(hDir, &fData));
436 if ((nhl) > lhitlim) {
437 pr("Limit of %ld lines exceeded\n", lhitlim);
439 pr("Found %s in %ld of %ld files and in %ld of %ld lines\n",
440 name, nhf, nf, nhl, nl);