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 snprintf(filename, sizeof(filename), "%s/%s", infodir,
215 fp = fopen(filename, "r");
219 if (fstat(fileno(fp), &statb) < 0) {
220 logerror("Cannot stat for \"%s\" info file (%s)",
221 filename, strerror(errno));
225 if ((statb.st_mode & S_IFREG) == 0) {
226 logerror("The info file \"%s\" is not regular file\n",
231 while (fgets(fbuf, 256, fp)) {
232 lowerit(lbuf, 256, fbuf);
233 if (strstr(lbuf, lbp)) {
235 pr("*** %s ***\n", dp->d_name);
244 * break if too many lines
246 if ((nhl + nlhl) > lhitlim)
265 if ((nhl) > lhitlim) {
266 pr("Limit of %d lines exceeded\n", lhitlim);
268 pr("Found %s in %ld of %ld files and in %ld of %ld lines\n",
269 name, nhf, nf, nhl, nl);
288 name = player->argp[1];
291 * don't let sneaky people go outside the info directory
293 if (NULL != (tmp_name = strrchr(name, '/')))
295 if (NULL != (tmp_name = strrchr(name, '\\')))
297 if (NULL != (tmp_name = strrchr(name, ':')))
303 snprintf(filename, sizeof(filename) - 1, "%s\\%s", infodir, name);
304 fp = fopen(filename, "rb");
306 /* may be a "partial" request. */
308 WIN32_FIND_DATA fData;
309 strcat(filename, "*");
310 hDir = FindFirstFile(filename, &fData);
311 if (hDir == INVALID_HANDLE_VALUE) {
312 switch (GetLastError()) {
313 case ERROR_FILE_NOT_FOUND:
314 pr("Sorry, there is no info on %s\n", name);
317 case ERROR_PATH_NOT_FOUND:
318 pr("Can't open info dir\n");
319 logerror("Can't open info dir \"%s\"", infodir);
322 pr("Error reading info dir\n");
323 logerror("Error (%lu) reading info dir(%s)\\file(%s)",
324 GetLastError(), infodir, filename);
329 if ((fData.dwFileAttributes != (DWORD)-1) &&
330 ((fData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) ||
331 (fData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) ||
332 (fData.dwFileAttributes == FILE_ATTRIBUTE_READONLY)) &&
333 (strncasecmp(name, fData.cFileName, strlen(name)) == 0)) {
336 snprintf(last, sizeof(last), "%s", fData.cFileName);
339 pr("`%s' is ambiguous. The following topics match:\n%s",
341 width = strlen(last);
343 width += 2 + strlen(fData.cFileName);
347 width = strlen(fData.cFileName);
349 pr(",%c%s", sep, fData.cFileName);
352 } while (FindNextFile(hDir, &fData));
355 pr("Sorry, there is no info on %s\n", name);
357 } else if (nmatch > 1) {
361 snprintf(filename, sizeof(filename), "%s/%s",
363 fp = fopen(filename, "rb");
365 pr("Error reading info file for %s\n", name);
366 logerror("Cannot open for \"%s\" info file (%s)",
367 filename, strerror(errno));
372 DWORD fAttrib = GetFileAttributes(filename);
373 if ((fAttrib == (DWORD)-1) || /* INVALID_FILE_ATTRIBUTES */
374 ((fAttrib != FILE_ATTRIBUTE_NORMAL) &&
375 (fAttrib != FILE_ATTRIBUTE_ARCHIVE) &&
376 (fAttrib != FILE_ATTRIBUTE_READONLY))) {
377 pr("Error reading info file for %s\n", name);
378 logerror("The info file \"%s\" is not regular file\n",
385 pr("Information on: %s", name);
386 while (fgets(buf, sizeof(buf), fp) != 0)
396 WIN32_FIND_DATA fData;
402 long nf, nhf, nl, nlhl, nhl, nll;
406 if (player->argp[1] == 0 || !*player->argp[1]) {
407 pr("Apropos what?\n");
412 if (player->argp[2]) {
413 lhitlim = atoi(player->argp[2]);
418 snprintf(filename, sizeof(filename), "%s\\*",infodir);
419 hDir = FindFirstFile(filename, &fData);
420 if (hDir == INVALID_HANDLE_VALUE) {
421 if (GetLastError() == ERROR_PATH_NOT_FOUND) {
422 pr("Can't open info dir\n");
423 logerror("Can't open info dir \"%s\"", infodir);
425 pr("Error reading info dir\n");
426 logerror("Error (%lu) reading info dir(%s)\\file(%s)",
427 GetLastError(), infodir, filename);
437 * lower case search string into lbp
439 name = player->argp[1];
440 lowerit(lbp, 256, name);
445 nf = nhf = nl = nhl = 0;
447 if ((fData.dwFileAttributes != (DWORD)-1) &&
448 ((fData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) ||
449 (fData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) ||
450 (fData.dwFileAttributes == FILE_ATTRIBUTE_READONLY))) {
451 snprintf(filename, sizeof(filename), "%s\\%s", infodir,
453 fp = fopen(filename, "rb");
457 while (fgets(fbuf, 256, fp)) {
458 lowerit(lbuf, 256, fbuf);
459 if (strstr(lbuf, lbp)) {
461 pr("*** %s ***\n", fData.cFileName);
470 * break if too many lines
472 if ((nhl + nlhl) > lhitlim)
485 } while (FindNextFile(hDir, &fData));
492 if ((nhl) > lhitlim) {
493 pr("Limit of %d lines exceeded\n", lhitlim);
495 pr("Found %s in %ld of %ld files and in %ld of %ld lines\n",
496 name, nhf, nf, nhl, nl);