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++);
78 s_char filename[1024];
83 name = player->argp[1];
86 * don't let sneaky people go outside the info directory
88 if (NULL != (tmp_name = strrchr(name, '/')))
94 snprintf(filename, sizeof(filename), "%s/%s", infodir, name);
95 fp = fopen(filename, "r");
97 int len = strlen(name);
98 /* may be a "partial" request. */
99 info_dp = opendir(infodir);
101 pr("Can't open info dir\n");
102 logerror("Can't open info dir \"%s\"\n", infodir);
106 while ((dp = readdir(info_dp)) != 0) {
107 if (strncasecmp(name, dp->d_name, strlen(name)) != 0)
111 snprintf(last, sizeof(last), "%s", dp->d_name);
114 pr("The following info topics were found %s", last);
115 pr(", %s", dp->d_name);
120 pr("Sorry, there is no info on %s\n", name);
122 } else if (nmatch > 1) {
126 snprintf(filename, sizeof(filename), "%s/%s", infodir,
128 fp = fopen(filename, "r");
130 pr("Error reading info file for %s\n", name);
131 logerror("Cannot open for \"%s\" info file (%s)",
132 filename, strerror(errno));
136 if (fstat(fileno(fp), &statb) < 0) {
137 pr("Error reading info file for %s\n", name);
138 logerror("Cannot fstat for \"%s\" info file (%s)",
139 filename, strerror(errno));
143 if ((statb.st_mode & S_IFREG) == 0) {
144 pr("Error reading info file for %s\n", name);
145 logerror("The info file \"%s\" is not regular file\n", filename);
149 pr("Information on: %s Last modification date: %s",
150 name, ctime(&statb.st_mtime));
151 while (fgets(buf, sizeof(buf), fp) != 0)
165 s_char filename[1024];
167 long nf, nhf, nl, nlhl, nhl, nll;
172 if (player->argp[1] == 0 || !*player->argp[1]) {
173 pr("Apropos what?\n");
178 if (player->argp[2]) {
179 lhitlim = atoi(player->argp[2]);
184 info_dp = opendir(infodir);
185 if (info_dp == NULL) {
186 pr("Can't open info dir \n");
187 logerror("Can't open info dir \"%s\"", infodir);
191 fbuf = (s_char *)malloc(256);
192 lbuf = (s_char *)malloc(256);
193 lbp = (s_char *)malloc(256);
196 * lower case search string into lbp
198 name = player->argp[1];
199 lowerit(lbp, 256, name);
204 nf = nhf = nl = nhl = 0;
205 while ((dp = readdir(info_dp)) != 0) {
206 snprintf(filename, sizeof(filename), "%s/%s", infodir,
208 fp = fopen(filename, "r");
212 if (fstat(fileno(fp), &statb) < 0) {
213 logerror("Cannot stat for \"%s\" info file (%s)",
214 filename, strerror(errno));
218 if ((statb.st_mode & S_IFREG) == 0) {
219 logerror("The info file \"%s\" is not regular file\n",
224 while (fgets(fbuf, 256, fp)) {
225 lowerit(lbuf, 256, fbuf);
226 if (strstr(lbuf, lbp)) {
228 pr("*** %s ***\n", dp->d_name);
237 * break if too many lines
239 if ((nhl + nlhl) > lhitlim)
258 if ((nhl) > lhitlim) {
259 pr("Limit of %d lines exceeded\n", lhitlim);
261 pr("Found %s in %ld of %ld files and in %ld of %ld lines\n",
262 name, nhf, nf, nhl, nl);
275 s_char filename[1024];
279 name = player->argp[1];
282 * don't let sneaky people go outside the info directory
284 if (NULL != (tmp_name = strrchr(name, '/')))
286 if (NULL != (tmp_name = strrchr(name, '\\')))
288 if (NULL != (tmp_name = strrchr(name, ':')))
294 _snprintf(filename, sizeof(filename) - 1, "%s\\%s", infodir, name);
295 fp = fopen(filename, "r");
297 /* may be a "partial" request. */
299 WIN32_FIND_DATA fData;
300 int len = strlen(name);
301 strcat(filename, "*");
302 hDir = FindFirstFile(filename, &fData);
303 if (hDir == INVALID_HANDLE_VALUE) {
304 switch (GetLastError()) {
305 case ERROR_FILE_NOT_FOUND:
306 pr("Sorry, there is no info on %s\n", name);
309 case ERROR_PATH_NOT_FOUND:
310 pr("Can't open info dir\n");
311 logerror("Can't open info dir \"%s\"", infodir);
314 pr("Error reading info dir\n");
315 logerror("Error (%d) reading info dir(%s)\\file(%s)",
316 infodir, filename, GetLastError());
321 if ((fData.dwFileAttributes != (DWORD)-1) &&
322 ((fData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) ||
323 (fData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) ||
324 (fData.dwFileAttributes == FILE_ATTRIBUTE_READONLY)) &&
325 (strncasecmp(name, fData.cFileName, strlen(name)) == 0)) {
328 _snprintf(last, sizeof(last), "%s", fData.cFileName);
331 pr("The following info topics were found %s",
333 pr(", %s", fData.cFileName);
336 } while (FindNextFile(hDir, &fData));
339 pr("Sorry, there is no info on %s\n", name);
341 } else if (nmatch > 1) {
345 _snprintf(filename, sizeof(filename), "%s/%s",
347 fp = fopen(filename, "r");
349 pr("Error reading info file for %s\n", name);
350 logerror("Cannot open for \"%s\" info file (%s)",
351 filename, strerror(errno));
356 DWORD fAttrib = GetFileAttributes(filename);
357 if ((fAttrib == (DWORD)-1) || //INVALID_FILE_ATTRIBUTES
358 (fAttrib != FILE_ATTRIBUTE_NORMAL) &&
359 (fAttrib != FILE_ATTRIBUTE_ARCHIVE) &&
360 (fAttrib != FILE_ATTRIBUTE_READONLY)) {
361 pr("Error reading info file for %s\n", name);
362 logerror("The info file \"%s\" is not regular file\n",
369 pr("Information on: %s", name);
370 while (fgets(buf, sizeof(buf), fp) != 0)
380 WIN32_FIND_DATA fData;
385 s_char filename[1024];
386 long nf, nhf, nl, nlhl, nhl, nll;
390 if (player->argp[1] == 0 || !*player->argp[1]) {
391 pr("Apropos what?\n");
396 if (player->argp[2]) {
397 lhitlim = atoi(player->argp[2]);
402 _snprintf(filename, sizeof(filename), "%s\\*",infodir);
403 hDir = FindFirstFile(filename, &fData);
404 if (hDir == INVALID_HANDLE_VALUE) {
405 if (GetLastError() == ERROR_PATH_NOT_FOUND) {
406 pr("Can't open info dir\n");
407 logerror("Can't open info dir \"%s\"", infodir);
409 pr("Error reading info dir\n");
410 logerror("Error (%d) reading info dir(%s)\\file(%s)",
411 infodir, filename, GetLastError());
416 fbuf = (s_char *)malloc(256);
417 lbuf = (s_char *)malloc(256);
418 lbp = (s_char *)malloc(256);
421 * lower case search string into lbp
423 name = player->argp[1];
424 lowerit(lbp, 256, name);
429 nf = nhf = nl = nhl = 0;
431 if ((fData.dwFileAttributes != (DWORD)-1) &&
432 ((fData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) ||
433 (fData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) ||
434 (fData.dwFileAttributes == FILE_ATTRIBUTE_READONLY))) {
435 _snprintf(filename, sizeof(filename), "%s\\%s", infodir,
437 fp = fopen(filename, "r");
441 while (fgets(fbuf, 256, fp)) {
442 lowerit(lbuf, 256, fbuf);
443 if (strstr(lbuf, lbp)) {
445 pr("*** %s ***\n", fData.cFileName);
454 * break if too many lines
456 if ((nhl + nlhl) > lhitlim)
469 } while (FindNextFile(hDir, &fData));
476 if ((nhl) > lhitlim) {
477 pr("Limit of %ld lines exceeded\n", lhitlim);
479 pr("Found %s in %ld of %ld files and in %ld of %ld lines\n",
480 name, nhf, nf, nhl, nl);