2 * Empire - A multi-player, client/server Internet based war game.
3 * Copyright (C) 1986-2006, 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
52 lowerit(char *buf, int n, char *orig)
53 { /* converts a string to lower case */
54 /* lower case output buffer */
55 /* size of output buffer */
58 for (i = 0; i < n - 1 && orig[i]; i++)
59 buf[i] = tolower(orig[i]);
82 name = player->argp[1];
85 * don't let sneaky people go outside the info directory
87 if (NULL != (tmp_name = strrchr(name, '/')))
93 snprintf(filename, sizeof(filename), "%s/%s", infodir, name);
94 fp = fopen(filename, "r");
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) {
105 if (strncasecmp(name, dp->d_name, strlen(name)) != 0)
109 snprintf(last, sizeof(last), "%s", dp->d_name);
112 pr("`%s' is ambiguous. The following topics match:\n%s",
114 width = strlen(last);
116 width += 2 + strlen(dp->d_name);
120 width = strlen(dp->d_name);
122 pr(",%c%s", sep, dp->d_name);
127 pr("Sorry, there is no info on %s\n", name);
129 } else if (nmatch > 1) {
133 snprintf(filename, sizeof(filename), "%s/%s", infodir,
135 fp = fopen(filename, "r");
137 pr("Error reading info file for %s\n", name);
138 logerror("Cannot open for \"%s\" info file (%s)",
139 filename, strerror(errno));
143 if (fstat(fileno(fp), &statb) < 0) {
144 pr("Error reading info file for %s\n", name);
145 logerror("Cannot fstat for \"%s\" info file (%s)",
146 filename, strerror(errno));
150 if ((statb.st_mode & S_IFREG) == 0) {
151 pr("Error reading info file for %s\n", name);
152 logerror("The info file \"%s\" is not regular file\n", filename);
156 pr("Information on: %s Last modification date: %s",
157 name, ctime(&statb.st_mtime));
158 while (fgets(buf, sizeof(buf), fp) != 0)
174 long nf, nhf, nl, nlhl, nhl, nll;
179 if (player->argp[1] == 0 || !*player->argp[1]) {
180 pr("Apropos what?\n");
185 if (player->argp[2]) {
186 lhitlim = atoi(player->argp[2]);
191 info_dp = opendir(infodir);
192 if (info_dp == NULL) {
193 pr("Can't open info dir \n");
194 logerror("Can't open info dir \"%s\"", infodir);
203 * lower case search string into lbp
205 name = player->argp[1];
206 lowerit(lbp, 256, name);
211 nf = nhf = nl = nhl = 0;
212 while ((dp = readdir(info_dp)) != 0) {
213 if (dp->d_name[0] == '.')
215 snprintf(filename, sizeof(filename), "%s/%s", infodir,
217 fp = fopen(filename, "r");
221 if (fstat(fileno(fp), &statb) < 0) {
222 logerror("Cannot stat for \"%s\" info file (%s)",
223 filename, strerror(errno));
227 if ((statb.st_mode & S_IFREG) == 0) {
228 logerror("The info file \"%s\" is not regular file\n",
233 while (fgets(fbuf, 256, fp)) {
234 lowerit(lbuf, 256, fbuf);
235 if (strstr(lbuf, lbp)) {
237 pr("*** %s ***\n", dp->d_name);
246 * break if too many lines
248 if ((nhl + nlhl) > lhitlim)
267 if ((nhl) > lhitlim) {
268 pr("Limit of %d lines exceeded\n", lhitlim);
270 pr("Found %s in %ld of %ld files and in %ld of %ld lines\n",
271 name, nhf, nf, nhl, nl);
290 name = player->argp[1];
293 * don't let sneaky people go outside the info directory
295 if (NULL != (tmp_name = strrchr(name, '/')))
297 if (NULL != (tmp_name = strrchr(name, '\\')))
299 if (NULL != (tmp_name = strrchr(name, ':')))
305 snprintf(filename, sizeof(filename) - 1, "%s\\%s", infodir, name);
306 fp = fopen(filename, "rb");
308 /* may be a "partial" request. */
310 WIN32_FIND_DATA fData;
311 strcat(filename, "*");
312 hDir = FindFirstFile(filename, &fData);
313 if (hDir == INVALID_HANDLE_VALUE) {
314 switch (GetLastError()) {
315 case ERROR_FILE_NOT_FOUND:
316 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);