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\n");
113 logerror("Can't open info dir \"%s\"\n", infodir);
117 while ((dp = readdir(info_dp)) != 0 && fp == 0) {
118 if (strnccmp(bp, dp->d_name, len) != 0)
120 sprintf(filename, "%s/%s", infodir, dp->d_name);
121 fp = fopen(filename, "r");
125 pr("Sorry, there is no info on %s\n", bp);
129 if (fstat(fileno(fp), &statb) < 0) {
130 pr("Error reading info file for %s\n", bp);
131 logerror("Cannot fstat for \"%s\" info file (%s)",
132 filename, strerror(errno));
136 if ((statb.st_mode & S_IFREG) == 0) {
137 pr("Error reading info file for %s\n", bp);
138 logerror("The info file \"%s\" is not regular file\n", filename);
142 pr("Information on: %s Last modification date: %s",
143 bp, ctime(&statb.st_mtime));
144 while (fgets(buf, sizeof(buf), fp) != 0)
158 s_char filename[1024];
160 long nf, nhf, nl, nlhl, nhl, nll;
164 if (player->argp[1] == 0 || !*player->argp[1]) {
165 pr("Apropos what?\n");
170 if (player->argp[2]) {
171 lhitlim = atoi(player->argp[2]);
176 info_dp = opendir(infodir);
178 pr("Can't open info dir \"%s\"\n", infodir);
182 fbuf = (s_char *)malloc(256);
183 lbuf = (s_char *)malloc(256);
184 lbp = (s_char *)malloc(256);
187 * lower case search string into lbp
189 bp = player->argp[1];
190 lowerit(lbp, 256, bp);
195 nf = nhf = nl = nhl = 0;
197 while ((dp = readdir(info_dp)) != 0) {
198 sprintf(filename, "%s/%s", infodir, dp->d_name);
199 fp = fopen(filename, "r");
203 while (fgets(fbuf, 256, fp)) {
204 lowerit(lbuf, 256, fbuf);
205 if (strstr(lbuf, lbp)) {
207 pr("*** %s ***\n", dp->d_name);
216 * break if too many lines
218 if ((nhl + nlhl) > lhitlim)
237 if ((nhl) > lhitlim) {
238 pr("Limit of %d lines exceeded\n", lhitlim);
240 pr("Found %s in %ld of %ld files and in %ld of %ld lines\n",
241 bp, nhf, nf, nhl, nl);
254 s_char filename[1024];
256 if (player->argp[1] == 0 || !*player->argp[1])
260 * don't let sneaky people go outside the info directory
262 bp = player->argp[1];
263 if (NULL != (bp2 = strrchr(bp, '/')))
265 if (NULL != (bp2 = strrchr(bp, '\\')))
267 if (NULL != (bp2 = strrchr(bp, ':')))
273 strncpy(filename, infodir, sizeof(filename) - 2);
274 strcat(filename, "\\");
275 strncat(filename, bp, sizeof(filename) - 1 - strlen(filename));
276 fp = fopen(filename, "r");
278 /* may be a "partial" request. */
280 WIN32_FIND_DATA fData;
281 int len = strlen(bp);
282 strncat(filename, "*", sizeof(filename) - 1 - strlen(filename));
283 hDir = FindFirstFile(filename, &fData);
284 if (hDir == INVALID_HANDLE_VALUE) {
285 switch (GetLastError()) {
286 case ERROR_FILE_NOT_FOUND:
287 pr("Sorry, there is no info on %s\n", bp);
290 case ERROR_PATH_NOT_FOUND:
291 pr("Can't open info dir\n");
292 logerror("Can't open info dir \"%s\"", infodir);
295 pr("Error reading info dir\n");
296 logerror("Error (%d) reading info dir(%s)/file(%s)",
297 infodir, filename, GetLastError());
302 if (((fData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) ||
303 (fData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) ||
304 (fData.dwFileAttributes == FILE_ATTRIBUTE_READONLY)) &&
305 (strnccmp(bp, fData.cFileName, len) == 0)) {
306 strncpy(filename, infodir, sizeof(filename) - 2);
307 strcat(filename, "\\");
308 strncat(filename, fData.cFileName,
309 sizeof(filename) - 1 - strlen(filename));
310 fp = fopen(filename, "r");
312 } while (!fp && FindNextFile(hDir, &fData));
315 pr("Sorry, there is no info on %s\n", bp);
320 DWORD fAttrib = GetFileAttributes(filename);
321 if ((fAttrib == (DWORD)-1) && //INVALID_FILE_ATTRIBUTES
322 (fAttrib != FILE_ATTRIBUTE_NORMAL) &&
323 (fAttrib != FILE_ATTRIBUTE_ARCHIVE) &&
324 (fAttrib != FILE_ATTRIBUTE_READONLY)) {
325 pr("Error reading info file for %s\n", bp);
326 logerror("The info file \"%s\" is not regular file\n", filename);
332 pr("Information on: %s", bp);
333 while (fgets(buf, sizeof(buf), fp) != 0)
343 WIN32_FIND_DATA fData;
348 s_char filename[1024];
349 long nf, nhf, nl, nlhl, nhl, nll;
353 if (player->argp[1] == 0 || !*player->argp[1]) {
354 pr("Apropos what?\n");
359 if (player->argp[2]) {
360 lhitlim = atoi(player->argp[2]);
365 strncpy(filename, infodir, sizeof(filename) - 3);
366 strcat(filename, "//*");
367 hDir = FindFirstFile(filename, &fData);
368 if (hDir == INVALID_HANDLE_VALUE) {
372 fbuf = (s_char *)malloc(256);
373 lbuf = (s_char *)malloc(256);
374 lbp = (s_char *)malloc(256);
377 * lower case search string into lbp
379 bp = player->argp[1];
380 lowerit(lbp, 256, bp);
385 nf = nhf = nl = nhl = 0;
387 strncpy(filename, infodir, sizeof(filename) - 3);
388 strcat(filename, "//");
389 strncat(filename, fData.cFileName,
390 sizeof(filename) - 1 - strlen(filename));
391 fp = fopen(filename, "r");
395 while (fgets(fbuf, 256, fp)) {
396 lowerit(lbuf, 256, fbuf);
397 if (strstr(lbuf, lbp)) {
399 pr("*** %s ***\n", fData.cFileName);
408 * break if too many lines
410 if ((nhl + nlhl) > lhitlim)
422 } while (FindNextFile(hDir, &fData));
429 if ((nhl) > lhitlim) {
430 pr("Limit of %ld lines exceeded\n", lhitlim);
432 pr("Found %s in %ld of %ld files and in %ld of %ld lines\n",
433 bp, nhf, nf, nhl, nl);