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++);
68 strnccmp(s_char *s1, s_char *s2, int n)
72 for (i = 0; i < n && *s1 && s2; i++) {
93 s_char filename[1024];
96 if (player->argp[1] == 0 || !*player->argp[1])
99 * don't let sneaky people go outside the info directory
101 else if (NULL != (bp = strrchr(player->argp[1], '/')))
104 bp = player->argp[1];
105 sprintf(filename, "%s/%s", infodir, bp);
106 fp = fopen(filename, "r");
108 int len = strlen(bp);
109 /* may be a "partial" request. */
110 info_dp = opendir(infodir);
112 pr("Can't open info dir \"%s\"\n", infodir);
116 while ((dp = readdir(info_dp)) != 0 && fp == 0) {
117 if (strnccmp(bp, dp->d_name, len) != 0)
119 sprintf(filename, "%s/%s", infodir, dp->d_name);
120 fp = fopen(filename, "r");
124 pr("Sorry, there is no info on %s\n", bp);
128 if (fstat(fileno(fp), &statb) < 0) {
129 pr("Cannot read info page for \"%s\" (%s)\n",
130 dp->d_name, strerror(errno));
134 if ((statb.st_mode & S_IFREG) == 0) {
135 pr("There is no available information on \"%s\"\n", dp->d_name);
139 pr("Information on: %s Last modification date: %s",
140 bp, ctime(&statb.st_mtime));
141 while (fgets(buf, sizeof(buf), fp) != 0)
155 s_char filename[1024];
157 long nf, nhf, nl, nlhl, nhl, nll;
161 if (player->argp[1] == 0 || !*player->argp[1]) {
162 pr("Apropos what?\n");
167 if (player->argp[2]) {
168 lhitlim = atoi(player->argp[2]);
173 info_dp = opendir(infodir);
175 pr("Can't open info dir \"%s\"\n", infodir);
179 fbuf = (s_char *)malloc(256);
180 lbuf = (s_char *)malloc(256);
181 lbp = (s_char *)malloc(256);
184 * lower case search string into lbp
186 bp = player->argp[1];
187 lowerit(lbp, 256, bp);
192 nf = nhf = nl = nhl = 0;
194 while ((dp = readdir(info_dp)) != 0) {
195 sprintf(filename, "%s/%s", infodir, dp->d_name);
196 fp = fopen(filename, "r");
200 while (fgets(fbuf, 256, fp)) {
201 lowerit(lbuf, 256, fbuf);
202 if (strstr(lbuf, lbp)) {
204 pr("*** %s ***\n", dp->d_name);
213 * break if too many lines
215 if ((nhl + nlhl) > lhitlim)
234 if ((nhl) > lhitlim) {
235 pr("Limit of %d lines exceeded\n", lhitlim);
237 pr("Found %s in %ld of %ld files and in %ld of %ld lines\n",
238 bp, nhf, nf, nhl, nl);
251 s_char filename[1024];
253 if (player->argp[1] == 0 || !*player->argp[1])
257 * don't let sneaky people go outside the info directory
259 bp = player->argp[1];
260 if (NULL != (bp2 = strrchr(bp, '/')))
262 if (NULL != (bp2 = strrchr(bp, '\\')))
264 if (NULL != (bp2 = strrchr(bp, ':')))
270 strncpy(filename, infodir, sizeof(filename) - 2);
271 strcat(filename, "//");
272 strncat(filename, bp, sizeof(filename) - 1 - strlen(filename));
273 fp = fopen(filename, "r");
275 /* may be a "partial" request. */
277 WIN32_FIND_DATA fData;
278 int len = strlen(bp);
279 strncat(filename, "*", sizeof(filename) - 1 - strlen(filename));
280 hDir = FindFirstFile(filename, &fData);
281 if (hDir == INVALID_HANDLE_VALUE) {
282 switch (GetLastError()) {
283 case ERROR_FILE_NOT_FOUND:
284 pr("Sorry, there is no info on %s\n", bp);
286 case ERROR_PATH_NOT_FOUND:
287 pr("Can't open info dir \"%s\"\n", infodir);
290 pr("Error getting info file \"%s\"\n", filename);
295 if (((fData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) ||
296 (fData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) ||
297 (fData.dwFileAttributes == FILE_ATTRIBUTE_READONLY)) &&
298 (strnccmp(bp, fData.cFileName, len) == 0)) {
299 strncpy(filename, infodir, sizeof(filename) - 2);
300 strcat(filename, "//");
301 strncat(filename, fData.cFileName,
302 sizeof(filename) - 1 - strlen(filename));
303 fp = fopen(filename, "r");
305 } while (!fp && FindNextFile(hDir, &fData));
308 pr("Sorry, there is no info on %s\n", bp);
312 pr("Information on: %s", bp);
313 while (fgets(buf, sizeof(buf), fp) != 0)
323 WIN32_FIND_DATA fData;
328 s_char filename[1024];
329 long nf, nhf, nl, nlhl, nhl, nll;
333 if (player->argp[1] == 0 || !*player->argp[1]) {
334 pr("Apropos what?\n");
339 if (player->argp[2]) {
340 lhitlim = atoi(player->argp[2]);
345 strncpy(filename, infodir, sizeof(filename) - 3);
346 strcat(filename, "//*");
347 hDir = FindFirstFile(filename, &fData);
348 if (hDir == INVALID_HANDLE_VALUE) {
352 fbuf = (s_char *)malloc(256);
353 lbuf = (s_char *)malloc(256);
354 lbp = (s_char *)malloc(256);
357 * lower case search string into lbp
359 bp = player->argp[1];
360 lowerit(lbp, 256, bp);
365 nf = nhf = nl = nhl = 0;
367 strncpy(filename, infodir, sizeof(filename) - 3);
368 strcat(filename, "//");
369 strncat(filename, fData.cFileName,
370 sizeof(filename) - 1 - strlen(filename));
371 fp = fopen(filename, "r");
375 while (fgets(fbuf, 256, fp)) {
376 lowerit(lbuf, 256, fbuf);
377 if (strstr(lbuf, lbp)) {
379 pr("*** %s ***\n", fData.cFileName);
388 * break if too many lines
390 if ((nhl + nlhl) > lhitlim)
402 } while (FindNextFile(hDir, &fData));
409 if ((nhl) > lhitlim) {
410 pr("Limit of %ld lines exceeded\n", lhitlim);
412 pr("Found %s in %ld of %ld files and in %ld of %ld lines\n",
413 bp, nhf, nf, nhl, nl);