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
51 lowerit(char *buf, int n, char *orig)
52 { /* converts a string to lower case */
53 /* lower case output buffer */
54 /* size of output buffer */
57 for (i = 0; i < n - 1 && orig[i]; i++)
58 buf[i] = tolower(orig[i]);
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 /* may be a "partial" request. */
96 info_dp = opendir(infodir);
98 pr("Can't open info dir\n");
99 logerror("Can't open info dir \"%s\"\n", infodir);
103 while ((dp = readdir(info_dp)) != 0) {
104 if (strncasecmp(name, dp->d_name, strlen(name)) != 0)
108 snprintf(last, sizeof(last), "%s", dp->d_name);
111 pr("`%s' is ambiguous. The following topics match:\n%s",
113 width = strlen(last);
115 width += 2 + strlen(dp->d_name);
119 width = strlen(dp->d_name);
121 pr(",%c%s", sep, dp->d_name);
126 pr("Sorry, there is no info on %s\n", name);
128 } else if (nmatch > 1) {
132 snprintf(filename, sizeof(filename), "%s/%s", infodir,
134 fp = fopen(filename, "r");
136 pr("Error reading info file for %s\n", name);
137 logerror("Cannot open for \"%s\" info file (%s)",
138 filename, strerror(errno));
142 if (fstat(fileno(fp), &statb) < 0) {
143 pr("Error reading info file for %s\n", name);
144 logerror("Cannot fstat for \"%s\" info file (%s)",
145 filename, strerror(errno));
149 if ((statb.st_mode & S_IFREG) == 0) {
150 pr("Error reading info file for %s\n", name);
151 logerror("The info file \"%s\" is not regular file\n", filename);
156 while (fgets(buf, sizeof(buf), fp) != 0)
172 long nf, nhf, nl, nlhl, nhl, nll;
177 if (player->argp[1] == 0 || !*player->argp[1]) {
178 pr("Apropos what?\n");
183 if (player->argp[2]) {
184 lhitlim = atoi(player->argp[2]);
189 info_dp = opendir(infodir);
190 if (info_dp == NULL) {
191 pr("Can't open info dir \n");
192 logerror("Can't open info dir \"%s\"", infodir);
201 * lower case search string into lbp
203 name = player->argp[1];
204 lowerit(lbp, 256, name);
209 nf = nhf = nl = nhl = 0;
210 while ((dp = readdir(info_dp)) != 0) {
211 if (dp->d_name[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);
316 case ERROR_PATH_NOT_FOUND:
317 pr("Can't open info dir\n");
318 logerror("Can't open info dir \"%s\"", infodir);
321 pr("Error reading info dir\n");
322 logerror("Error (%lu) reading info dir(%s)\\file(%s)",
323 GetLastError(), infodir, filename);
328 if ((fData.dwFileAttributes != (DWORD)-1) &&
329 ((fData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) ||
330 (fData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) ||
331 (fData.dwFileAttributes == FILE_ATTRIBUTE_READONLY)) &&
332 (strncasecmp(name, fData.cFileName, strlen(name)) == 0)) {
335 snprintf(last, sizeof(last), "%s", fData.cFileName);
338 pr("`%s' is ambiguous. The following topics match:\n%s",
340 width = strlen(last);
342 width += 2 + strlen(fData.cFileName);
346 width = strlen(fData.cFileName);
348 pr(",%c%s", sep, fData.cFileName);
351 } while (FindNextFile(hDir, &fData));
354 pr("Sorry, there is no info on %s\n", name);
356 } else if (nmatch > 1) {
360 snprintf(filename, sizeof(filename), "%s/%s",
362 fp = fopen(filename, "rb");
364 pr("Error reading info file for %s\n", name);
365 logerror("Cannot open for \"%s\" info file (%s)",
366 filename, strerror(errno));
370 DWORD fAttrib = GetFileAttributes(filename);
371 if ((fAttrib == (DWORD)-1) || /* INVALID_FILE_ATTRIBUTES */
372 ((fAttrib != FILE_ATTRIBUTE_NORMAL) &&
373 (fAttrib != FILE_ATTRIBUTE_ARCHIVE) &&
374 (fAttrib != FILE_ATTRIBUTE_READONLY))) {
375 pr("Error reading info file for %s\n", name);
376 logerror("The info file \"%s\" is not regular file\n",
383 while (fgets(buf, sizeof(buf), fp) != 0)
393 WIN32_FIND_DATA fData;
399 long nf, nhf, nl, nlhl, nhl, nll;
403 if (player->argp[1] == 0 || !*player->argp[1]) {
404 pr("Apropos what?\n");
409 if (player->argp[2]) {
410 lhitlim = atoi(player->argp[2]);
415 snprintf(filename, sizeof(filename), "%s\\*",infodir);
416 hDir = FindFirstFile(filename, &fData);
417 if (hDir == INVALID_HANDLE_VALUE) {
418 if (GetLastError() == ERROR_PATH_NOT_FOUND) {
419 pr("Can't open info dir\n");
420 logerror("Can't open info dir \"%s\"", infodir);
422 pr("Error reading info dir\n");
423 logerror("Error (%lu) reading info dir(%s)\\file(%s)",
424 GetLastError(), infodir, filename);
434 * lower case search string into lbp
436 name = player->argp[1];
437 lowerit(lbp, 256, name);
442 nf = nhf = nl = nhl = 0;
444 if ((fData.dwFileAttributes != (DWORD)-1) &&
445 ((fData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) ||
446 (fData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) ||
447 (fData.dwFileAttributes == FILE_ATTRIBUTE_READONLY))) {
448 snprintf(filename, sizeof(filename), "%s\\%s", infodir,
450 fp = fopen(filename, "rb");
454 while (fgets(fbuf, 256, fp)) {
455 lowerit(lbuf, 256, fbuf);
456 if (strstr(lbuf, lbp)) {
458 pr("*** %s ***\n", fData.cFileName);
467 * break if too many lines
469 if ((nhl + nlhl) > lhitlim)
482 } while (FindNextFile(hDir, &fData));
489 if ((nhl) > lhitlim) {
490 pr("Limit of %d lines exceeded\n", lhitlim);
492 pr("Found %s in %ld of %ld files and in %ld of %ld lines\n",
493 name, nhf, nf, nhl, nl);