]> git.pond.sub.org Git - empserver/blob - src/lib/commands/info.c
Remove the unused/not needed code.
[empserver] / src / lib / commands / info.c
1 /*
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
5  *
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.
10  *
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.
15  *
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
19  *
20  *  ---
21  *
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.
25  *
26  *  ---
27  *
28  *  info.c: display an info page
29  * 
30  *  Known contributors to this file:
31  *     Dave Pare, 1986
32  *     Mike Wise, 1997 - added apropos and case insensitivity
33  *     Doug Hay, 1998
34  *     Steve McClure, 1998-2000
35  */
36
37 #include "misc.h"
38 #include "player.h"
39 #include <errno.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <sys/stat.h>
43 #if !defined(_WIN32)
44 #include <dirent.h>
45 #else
46 #include <windows.h>
47 #endif
48 #include "commands.h"
49 #include "optlist.h"
50
51 static s_char *
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 */
56     /* input strig */
57     int i;
58     s_char *tmp;
59     tmp = buf;
60     memset(buf, 0, n);
61     for (i = 0; i < n && *orig; i++) {
62         *tmp++ = tolower(*orig++);
63     }
64     return buf;
65 }
66
67 static int
68 strnccmp(s_char *s1, s_char *s2, int n)
69 {
70     int i;
71     char c1, c2;
72     for (i = 0; i < n && *s1 && s2; i++) {
73         c1 = tolower(*s1++);
74         c2 = tolower(*s2++);
75         if (c1 > c2)
76             return 1;
77         else if (c1 < c2)
78             return -1;
79     }
80     return 0;
81 }
82
83 #if !defined(_WIN32)
84
85 int
86 info(void)
87 {
88     s_char buf[255];
89     FILE *fp;
90     s_char *bp;
91     struct stat statb;
92     struct dirent *dp;
93     s_char filename[1024];
94     DIR *info_dp;
95
96     if (player->argp[1] == 0 || !*player->argp[1])
97         bp = "TOP";
98     /*
99      * don't let sneaky people go outside the info directory
100      */
101     else if (NULL != (bp = strrchr(player->argp[1], '/')))
102         bp++;
103     else
104         bp = player->argp[1];
105     sprintf(filename, "%s/%s", infodir, bp);
106     fp = fopen(filename, "r");
107     if (fp == NULL) {
108         int len = strlen(bp);
109         /* may be a "partial" request.  */
110         info_dp = opendir(infodir);
111         if (info_dp == 0) {
112             pr("Can't open info dir \"%s\"\n", infodir);
113             return RET_SYS;
114         }
115         rewinddir(info_dp);
116         while ((dp = readdir(info_dp)) != 0 && fp == 0) {
117             if (strnccmp(bp, dp->d_name, len) != 0)
118                 continue;
119             sprintf(filename, "%s/%s", infodir, dp->d_name);
120             fp = fopen(filename, "r");
121         }
122         closedir(info_dp);
123         if (fp == NULL) {
124             pr("Sorry, there is no info on %s\n", bp);
125             return RET_FAIL;
126         }
127     }
128     if (fstat(fileno(fp), &statb) < 0) {
129         pr("Cannot read info page for \"%s\" (%s)\n",
130            dp->d_name, strerror(errno));
131         fclose(fp);
132         return RET_SYS;
133     }
134     if ((statb.st_mode & S_IFREG) == 0) {
135         pr("There is no available information on \"%s\"\n", dp->d_name);
136         fclose(fp);
137         return RET_FAIL;
138     }
139     pr("Information on:  %s    Last modification date: %s",
140        bp, ctime(&statb.st_mtime));
141     while (fgets(buf, sizeof(buf), fp) != 0)
142         pr("%s", buf);
143     (void)fclose(fp);
144     return RET_OK;
145 }
146
147 int
148 apro(void)
149 {
150     FILE *fp;
151     s_char *bp, *lbp;
152     s_char *fbuf;
153     s_char *lbuf;
154     struct dirent *dp;
155     s_char filename[1024];
156     DIR *info_dp;
157     long nf, nhf, nl, nlhl, nhl, nll;
158     int alreadyhit;
159     int lhitlim;
160
161     if (player->argp[1] == 0 || !*player->argp[1]) {
162         pr("Apropos what?\n");
163         return RET_FAIL;
164     }
165
166     lhitlim = 100;
167     if (player->argp[2]) {
168         lhitlim = atoi(player->argp[2]);
169         if (lhitlim <= 0)
170             lhitlim = 100;
171     }
172
173     info_dp = opendir(infodir);
174     if (info_dp == 0) {
175         pr("Can't open info dir \"%s\"\n", infodir);
176         return RET_SYS;
177     }
178
179     fbuf = (s_char *)malloc(256);
180     lbuf = (s_char *)malloc(256);
181     lbp = (s_char *)malloc(256);
182
183     /*
184      *  lower case search string into lbp
185      */
186     bp = player->argp[1];
187     lowerit(lbp, 256, bp);
188
189     /*
190      *  search
191      */
192     nf = nhf = nl = nhl = 0;
193     rewinddir(info_dp);
194     while ((dp = readdir(info_dp)) != 0) {
195         sprintf(filename, "%s/%s", infodir, dp->d_name);
196         fp = fopen(filename, "r");
197         alreadyhit = 0;
198         nll = nlhl = 0;
199         if (fp != NULL) {
200             while (fgets(fbuf, 256, fp)) {
201                 lowerit(lbuf, 256, fbuf);
202                 if (strstr(lbuf, lbp)) {
203                     if (!alreadyhit) {
204                         pr("*** %s ***\n", dp->d_name);
205                         alreadyhit = 1;
206                         nhf++;
207                     }
208                     fbuf[74] = '\n';
209                     fbuf[75] = 0;
210                     pr("   %s", fbuf);
211                     nlhl++;
212                     /*
213                      * break if too many lines
214                      */
215                     if ((nhl + nlhl) > lhitlim)
216                         break;
217                 }
218                 nll++;
219             }
220             fclose(fp);
221         }
222         nhl += nlhl;
223         nl += nll;
224         nf++;
225         if (nhl > lhitlim)
226             break;
227     }
228     closedir(info_dp);
229
230     free(fbuf);
231     free(lbuf);
232     free(lbp);
233
234     if ((nhl) > lhitlim) {
235         pr("Limit of %d lines exceeded\n", lhitlim);
236     }
237     pr("Found %s in %ld of %ld files and in %ld of %ld lines\n",
238        bp, nhf, nf, nhl, nl);
239     return RET_OK;
240 }
241
242 #else
243
244 int
245 info(void)
246 {
247     s_char buf[255];
248     FILE *fp;
249     s_char *bp;
250     s_char *bp2;
251     s_char filename[1024];
252
253     if (player->argp[1] == 0 || !*player->argp[1])
254         bp = "TOP";
255     else {
256         /*
257          * don't let sneaky people go outside the info directory
258          */
259         bp = player->argp[1];
260         if (NULL != (bp2 = strrchr(bp, '/')))
261             bp = ++bp2;
262         if (NULL != (bp2 = strrchr(bp, '\\')))
263             bp = ++bp2;
264         if (NULL != (bp2 = strrchr(bp, ':')))
265             bp = ++bp2;
266         if (!*bp)
267             bp = "TOP";
268     }
269
270     strncpy(filename, infodir, sizeof(filename) - 2);
271     strcat(filename, "//");
272     strncat(filename, bp, sizeof(filename) - 1 - strlen(filename));
273     fp = fopen(filename, "r");
274     if (fp == NULL) {
275         /* may be a "partial" request.  */
276         HANDLE hDir;
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);
285                 break;
286             case ERROR_PATH_NOT_FOUND:
287                 pr("Can't open info dir \"%s\"\n", infodir);
288                 break;
289             default:
290                 pr("Error getting info file \"%s\"\n", filename);
291             }
292             return RET_SYS;
293         }
294         do {
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");
304             }
305         } while (!fp && FindNextFile(hDir, &fData));
306         FindClose(hDir);
307         if (fp == NULL) {
308             pr("Sorry, there is no info on %s\n", bp);
309             return RET_FAIL;
310         }
311     }
312     pr("Information on:  %s", bp);
313     while (fgets(buf, sizeof(buf), fp) != 0)
314         pr("%s", buf);
315     (void)fclose(fp);
316     return RET_OK;
317 }
318
319 int
320 apro(void)
321 {
322     HANDLE hDir;
323     WIN32_FIND_DATA fData;
324     FILE *fp;
325     s_char *bp, *lbp;
326     s_char *fbuf;
327     s_char *lbuf;
328     s_char filename[1024];
329     long nf, nhf, nl, nlhl, nhl, nll;
330     int alreadyhit;
331     int lhitlim;
332
333     if (player->argp[1] == 0 || !*player->argp[1]) {
334         pr("Apropos what?\n");
335         return RET_FAIL;
336     }
337
338     lhitlim = 100;
339     if (player->argp[2]) {
340         lhitlim = atoi(player->argp[2]);
341         if (lhitlim <= 0)
342             lhitlim = 100;
343     }
344
345     strncpy(filename, infodir, sizeof(filename) - 3);
346     strcat(filename, "//*");
347     hDir = FindFirstFile(filename, &fData);
348     if (hDir == INVALID_HANDLE_VALUE) {
349         return RET_FAIL;
350     }
351
352     fbuf = (s_char *)malloc(256);
353     lbuf = (s_char *)malloc(256);
354     lbp = (s_char *)malloc(256);
355
356     /*
357      *  lower case search string into lbp
358      */
359     bp = player->argp[1];
360     lowerit(lbp, 256, bp);
361
362     /*
363      *  search
364      */
365     nf = nhf = nl = nhl = 0;
366     do {
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");
372         alreadyhit = 0;
373         nll = nlhl = 0;
374         if (fp != NULL) {
375             while (fgets(fbuf, 256, fp)) {
376                 lowerit(lbuf, 256, fbuf);
377                 if (strstr(lbuf, lbp)) {
378                     if (!alreadyhit) {
379                         pr("*** %s ***\n", fData.cFileName);
380                         alreadyhit = 1;
381                         nhf++;
382                     }
383                     fbuf[74] = '\n';
384                     fbuf[75] = 0;
385                     pr("   %s", fbuf);
386                     nlhl++;
387                     /*
388                      * break if too many lines
389                      */
390                     if ((nhl + nlhl) > lhitlim)
391                         break;
392                 }
393                 nll++;
394             }
395             fclose(fp);
396         }
397         nhl += nlhl;
398         nl += nll;
399         nf++;
400         if (nhl > lhitlim)
401             break;
402     } while (FindNextFile(hDir, &fData));
403     FindClose(hDir);
404
405     free(fbuf);
406     free(lbuf);
407     free(lbp);
408
409     if ((nhl) > lhitlim) {
410         pr("Limit of %ld lines exceeded\n", lhitlim);
411     }
412     pr("Found %s in %ld of %ld files and in %ld of %ld lines\n",
413        bp, nhf, nf, nhl, nl);
414     return RET_OK;
415 }
416
417 #endif