2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2008, 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 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.
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
35 * Ron Koenderink, 2004
53 lowerit(char *buf, int n, char *orig)
54 { /* converts a string to lower case */
55 /* lower case output buffer */
56 /* size of output buffer */
59 for (i = 0; i < n - 1 && orig[i]; i++)
60 buf[i] = tolower(orig[i]);
83 name = player->argp[1];
86 * don't let sneaky people go outside the info directory
88 if (NULL != (tmp_name = strrchr(name, '/')))
94 snprintf(filename, sizeof(filename), "%s/%s", infodir, name);
95 fp = fopen(filename, "r");
97 /* may be a "partial" request. */
98 info_dp = opendir(infodir);
100 pr("Can't open info dir\n");
101 logerror("Can't open info dir \"%s\"\n", infodir);
105 while ((dp = readdir(info_dp)) != 0) {
106 if (strncasecmp(name, dp->d_name, strlen(name)) != 0)
110 snprintf(last, sizeof(last), "%s", dp->d_name);
113 pr("`%s' is ambiguous. The following topics match:\n%s",
115 width = strlen(last);
117 width += 2 + strlen(dp->d_name);
121 width = strlen(dp->d_name);
123 pr(",%c%s", sep, dp->d_name);
128 pr("Sorry, there is no info on %s\n", name);
130 } else if (nmatch > 1) {
134 snprintf(filename, sizeof(filename), "%s/%s", infodir,
136 fp = fopen(filename, "r");
138 pr("Error reading info file for %s\n", name);
139 logerror("Cannot open for \"%s\" info file (%s)",
140 filename, strerror(errno));
144 if (fstat(fileno(fp), &statb) < 0) {
145 pr("Error reading info file for %s\n", name);
146 logerror("Cannot fstat for \"%s\" info file (%s)",
147 filename, strerror(errno));
151 if ((statb.st_mode & S_IFREG) == 0) {
152 pr("Error reading info file for %s\n", name);
153 logerror("The info file \"%s\" is not regular file\n", filename);
157 pr("Information on: %s Last modification date: %s",
158 name, ctime(&statb.st_mtime));
159 while (fgets(buf, sizeof(buf), fp) != 0)
175 long nf, nhf, nl, nlhl, nhl, nll;
180 if (player->argp[1] == 0 || !*player->argp[1]) {
181 pr("Apropos what?\n");
186 if (player->argp[2]) {
187 lhitlim = atoi(player->argp[2]);
192 info_dp = opendir(infodir);
193 if (info_dp == NULL) {
194 pr("Can't open info dir \n");
195 logerror("Can't open info dir \"%s\"", infodir);
204 * lower case search string into lbp
206 name = player->argp[1];
207 lowerit(lbp, 256, name);
212 nf = nhf = nl = nhl = 0;
213 while ((dp = readdir(info_dp)) != 0) {
214 if (dp->d_name[0] == '.')
216 snprintf(filename, sizeof(filename), "%s/%s", infodir,
218 fp = fopen(filename, "r");
222 if (fstat(fileno(fp), &statb) < 0) {
223 logerror("Cannot stat for \"%s\" info file (%s)",
224 filename, strerror(errno));
228 if ((statb.st_mode & S_IFREG) == 0) {
229 logerror("The info file \"%s\" is not regular file\n",
234 while (fgets(fbuf, 256, fp)) {
235 lowerit(lbuf, 256, fbuf);
236 if (strstr(lbuf, lbp)) {
238 pr("*** %s ***\n", dp->d_name);
247 * break if too many lines
249 if ((nhl + nlhl) > lhitlim)
268 if ((nhl) > lhitlim) {
269 pr("Limit of %d lines exceeded\n", lhitlim);
271 pr("Found %s in %ld of %ld files and in %ld of %ld lines\n",
272 name, nhf, nf, nhl, nl);
291 name = player->argp[1];
294 * don't let sneaky people go outside the info directory
296 if (NULL != (tmp_name = strrchr(name, '/')))
298 if (NULL != (tmp_name = strrchr(name, '\\')))
300 if (NULL != (tmp_name = strrchr(name, ':')))
306 snprintf(filename, sizeof(filename) - 1, "%s\\%s", infodir, name);
307 fp = fopen(filename, "rb");
309 /* may be a "partial" request. */
311 WIN32_FIND_DATA fData;
312 strcat(filename, "*");
313 hDir = FindFirstFile(filename, &fData);
314 if (hDir == INVALID_HANDLE_VALUE) {
315 switch (GetLastError()) {
316 case ERROR_FILE_NOT_FOUND:
317 pr("Sorry, there is no info on %s\n", name);
319 case ERROR_PATH_NOT_FOUND:
320 pr("Can't open info dir\n");
321 logerror("Can't open info dir \"%s\"", infodir);
324 pr("Error reading info dir\n");
325 logerror("Error (%lu) reading info dir(%s)\\file(%s)",
326 GetLastError(), infodir, filename);
331 if ((fData.dwFileAttributes != (DWORD)-1) &&
332 ((fData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) ||
333 (fData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) ||
334 (fData.dwFileAttributes == FILE_ATTRIBUTE_READONLY)) &&
335 (strncasecmp(name, fData.cFileName, strlen(name)) == 0)) {
338 snprintf(last, sizeof(last), "%s", fData.cFileName);
341 pr("`%s' is ambiguous. The following topics match:\n%s",
343 width = strlen(last);
345 width += 2 + strlen(fData.cFileName);
349 width = strlen(fData.cFileName);
351 pr(",%c%s", sep, fData.cFileName);
354 } while (FindNextFile(hDir, &fData));
357 pr("Sorry, there is no info on %s\n", name);
359 } else if (nmatch > 1) {
363 snprintf(filename, sizeof(filename), "%s/%s",
365 fp = fopen(filename, "rb");
367 pr("Error reading info file for %s\n", name);
368 logerror("Cannot open for \"%s\" info file (%s)",
369 filename, strerror(errno));
374 DWORD fAttrib = GetFileAttributes(filename);
375 if ((fAttrib == (DWORD)-1) || /* INVALID_FILE_ATTRIBUTES */
376 ((fAttrib != FILE_ATTRIBUTE_NORMAL) &&
377 (fAttrib != FILE_ATTRIBUTE_ARCHIVE) &&
378 (fAttrib != FILE_ATTRIBUTE_READONLY))) {
379 pr("Error reading info file for %s\n", name);
380 logerror("The info file \"%s\" is not regular file\n",
387 pr("Information on: %s", name);
388 while (fgets(buf, sizeof(buf), fp) != 0)
398 WIN32_FIND_DATA fData;
404 long nf, nhf, nl, nlhl, nhl, nll;
408 if (player->argp[1] == 0 || !*player->argp[1]) {
409 pr("Apropos what?\n");
414 if (player->argp[2]) {
415 lhitlim = atoi(player->argp[2]);
420 snprintf(filename, sizeof(filename), "%s\\*",infodir);
421 hDir = FindFirstFile(filename, &fData);
422 if (hDir == INVALID_HANDLE_VALUE) {
423 if (GetLastError() == ERROR_PATH_NOT_FOUND) {
424 pr("Can't open info dir\n");
425 logerror("Can't open info dir \"%s\"", infodir);
427 pr("Error reading info dir\n");
428 logerror("Error (%lu) reading info dir(%s)\\file(%s)",
429 GetLastError(), infodir, filename);
439 * lower case search string into lbp
441 name = player->argp[1];
442 lowerit(lbp, 256, name);
447 nf = nhf = nl = nhl = 0;
449 if ((fData.dwFileAttributes != (DWORD)-1) &&
450 ((fData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) ||
451 (fData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) ||
452 (fData.dwFileAttributes == FILE_ATTRIBUTE_READONLY))) {
453 snprintf(filename, sizeof(filename), "%s\\%s", infodir,
455 fp = fopen(filename, "rb");
459 while (fgets(fbuf, 256, fp)) {
460 lowerit(lbuf, 256, fbuf);
461 if (strstr(lbuf, lbp)) {
463 pr("*** %s ***\n", fData.cFileName);
472 * break if too many lines
474 if ((nhl + nlhl) > lhitlim)
487 } while (FindNextFile(hDir, &fData));
494 if ((nhl) > lhitlim) {
495 pr("Limit of %d lines exceeded\n", lhitlim);
497 pr("Found %s in %ld of %ld files and in %ld of %ld lines\n",
498 name, nhf, nf, nhl, nl);