]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/info.c
info: Use S_ISREG() instead of S_IFREG for readability
[empserver] / src / lib / commands / info.c
index 122e1f3e1646d0cbe2fdde304b3cb7659ab806f2..961ad606b4c93c330d5783f15bd1effd644ca36e 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2004, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2020, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
  *  info.c: display an info page
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Mike Wise, 1997 - added apropos and case insensitivity
  *     Doug Hay, 1998
  *     Steve McClure, 1998-2000
+ *     Ron Koenderink, 2004
  */
 
-#include "misc.h"
-#include "player.h"
+#include <config.h>
+
+#include <ctype.h>
 #include <errno.h>
-#include <string.h>
-#include <stdio.h>
 #include <sys/stat.h>
+#include <stdio.h>
 #if !defined(_WIN32)
+#include <strings.h>
 #include <dirent.h>
-#else
-#include <windows.h>
 #endif
 #include "commands.h"
 #include "optlist.h"
 
-static s_char *
-lowerit(s_char *buf, int n, s_char *orig)
+static char *
+lowerit(char *buf, int n, char *orig)
 {                              /* converts a string to lower case */
     /* lower case output buffer */
     /* size of output buffer */
     /* input strig */
     int i;
-    s_char *tmp;
-    tmp = buf;
-    memset(buf, 0, n);
-    for (i = 0; i < n && *orig; i++) {
-       *tmp++ = tolower(*orig++);
-    }
+    for (i = 0; i < n - 1 && orig[i]; i++)
+       buf[i] = tolower(orig[i]);
+    buf[i] = 0;
     return buf;
 }
 
-static int
-strnccmp(s_char *s1, s_char *s2, int n)
-{
-    int i;
-    char c1, c2;
-    for (i = 0; i < n && *s1 && s2; i++) {
-       c1 = tolower(*s1++);
-       c2 = tolower(*s2++);
-       if (c1 > c2)
-           return 1;
-       else if (c1 < c2)
-           return -1;
-    }
-    return 0;
-}
-
 #if !defined(_WIN32)
 
 int
 info(void)
 {
-    s_char buf[255];
+    char buf[255];
     FILE *fp;
-    s_char *bp;
+    char *name;
+    char *tmp_name;
     struct stat statb;
     struct dirent *dp;
-    s_char filename[1024];
+    char filename[1024];
+    char last[256];
     DIR *info_dp;
+    int nmatch = 0;
+    int width = 0;
+    char sep;
 
-    if (player->argp[1] == 0 || !*player->argp[1])
-       bp = "TOP";
-    /*
-     * don't let sneaky people go outside the info directory
-     */
-    else if (NULL != (bp = strrchr(player->argp[1], '/')))
-       bp++;
-    else
-       bp = player->argp[1];
-    sprintf(filename, "%s/%s", infodir, bp);
+    name = player->argp[1];
+    if (name) {
+       /*
+        * don't let sneaky people go outside the info directory
+        */
+       if (NULL != (tmp_name = strrchr(name, '/')))
+           name = tmp_name + 1;
+    }
+    if (!name || !*name)
+       name = "TOP";
+
+    snprintf(filename, sizeof(filename), "%s/%s", infodir, name);
     fp = fopen(filename, "r");
     if (fp == NULL) {
-       int len = strlen(bp);
-       /* may be a "partial" request.  */
+       /* may be a "partial" request. */
        info_dp = opendir(infodir);
-       if (info_dp == 0) {
+       if (!info_dp) {
            pr("Can't open info dir\n");
            logerror("Can't open info dir \"%s\"\n", infodir);
-           return RET_SYS;
+           return RET_FAIL;
        }
-       rewinddir(info_dp);
-       while ((dp = readdir(info_dp)) != 0 && fp == 0) {
-           if (strnccmp(bp, dp->d_name, len) != 0)
+
+       while ((dp = readdir(info_dp))) {
+           if (strncasecmp(name, dp->d_name, strlen(name)) != 0)
                continue;
-           sprintf(filename, "%s/%s", infodir, dp->d_name);
-           fp = fopen(filename, "r");
+           nmatch++;
+           if (nmatch == 1) {
+               snprintf(last, sizeof(last), "%s", dp->d_name);
+           } else {
+               if (nmatch == 2) {
+                   pr("`%s' is ambiguous.  The following topics match:\n%s",
+                       name, last);
+                   width = strlen(last);
+               }
+               width += 2 + strlen(dp->d_name);
+               sep = ' ';
+               if (width > 75) {
+                   sep = '\n';
+                   width = strlen(dp->d_name);
+               }
+               pr(",%c%s", sep, dp->d_name);
+           }
        }
        closedir(info_dp);
+       if (nmatch == 0) {
+           pr("Sorry, there is no info on %s\n", name);
+           return RET_FAIL;
+       } else if (nmatch > 1) {
+           pr(".\n");
+           return RET_FAIL;
+       }
+       snprintf(filename, sizeof(filename), "%s/%s", infodir, last);
+       fp = fopen(filename, "r");
        if (fp == NULL) {
-           pr("Sorry, there is no info on %s\n", bp);
+           pr("Error reading info file for %s\n", name);
+           logerror("Cannot open for \"%s\" info file (%s)",
+                    filename, strerror(errno));
            return RET_FAIL;
        }
     }
     if (fstat(fileno(fp), &statb) < 0) {
-       pr("Error reading info file for %s\n", bp);
+       pr("Error reading info file for %s\n", name);
        logerror("Cannot fstat for \"%s\" info file (%s)",
-           filename, strerror(errno));
+                filename, strerror(errno));
        fclose(fp);
-       return RET_SYS;
+       return RET_FAIL;
     }
-    if ((statb.st_mode & S_IFREG) == 0) {
-       pr("Error reading info file for %s\n", bp);
+    if (!S_ISREG(statb.st_mode)) {
+       pr("Error reading info file for %s\n", name);
        logerror("The info file \"%s\" is not regular file\n", filename);
        fclose(fp);
-       return RET_SYS;
+       return RET_FAIL;
     }
-    pr("Information on:  %s    Last modification date: %s",
-       bp, ctime(&statb.st_mtime));
-    while (fgets(buf, sizeof(buf), fp) != 0)
+
+    while (fgets(buf, sizeof(buf), fp))
        pr("%s", buf);
     (void)fclose(fp);
     return RET_OK;
@@ -151,17 +162,18 @@ int
 apro(void)
 {
     FILE *fp;
-    s_char *bp, *lbp;
-    s_char *fbuf;
-    s_char *lbuf;
+    char *name, *lbp;
+    char *fbuf;
+    char *lbuf;
     struct dirent *dp;
-    s_char filename[1024];
+    char filename[1024];
     DIR *info_dp;
     long nf, nhf, nl, nlhl, nhl, nll;
     int alreadyhit;
     int lhitlim;
+    struct stat statb;
 
-    if (player->argp[1] == 0 || !*player->argp[1]) {
+    if (!player->argp[1] || !*player->argp[1]) {
        pr("Apropos what?\n");
        return RET_FAIL;
     }
@@ -174,32 +186,46 @@ apro(void)
     }
 
     info_dp = opendir(infodir);
-    if (info_dp == 0) {
-       pr("Can't open info dir \"%s\"\n", infodir);
-       return RET_SYS;
+    if (info_dp == NULL) {
+       pr("Can't open info dir\n");
+       logerror("Can't open info dir \"%s\"", infodir);
+       return RET_FAIL;
     }
 
-    fbuf = (s_char *)malloc(256);
-    lbuf = (s_char *)malloc(256);
-    lbp = (s_char *)malloc(256);
+    fbuf = malloc(256);
+    lbuf = malloc(256);
+    lbp = malloc(256);
 
     /*
      *  lower case search string into lbp
      */
-    bp = player->argp[1];
-    lowerit(lbp, 256, bp);
+    name = player->argp[1];
+    lowerit(lbp, 256, name);
 
     /*
      *  search
      */
     nf = nhf = nl = nhl = 0;
-    rewinddir(info_dp);
-    while ((dp = readdir(info_dp)) != 0) {
-       sprintf(filename, "%s/%s", infodir, dp->d_name);
+    while ((dp = readdir(info_dp))) {
+       if (dp->d_name[0] == '.')
+           continue;
+       snprintf(filename, sizeof(filename), "%s/%s", infodir, dp->d_name);
        fp = fopen(filename, "r");
        alreadyhit = 0;
        nll = nlhl = 0;
        if (fp != NULL) {
+           if (fstat(fileno(fp), &statb) < 0) {
+               logerror("Cannot stat for \"%s\" info file (%s)",
+                        filename, strerror(errno));
+               fclose(fp);
+               continue;
+           }
+           if (!S_ISREG(statb.st_mode)) {
+               logerror("The info file \"%s\" is not regular file\n",
+                        filename);
+               fclose(fp);
+               continue;
+           }
            while (fgets(fbuf, 256, fp)) {
                lowerit(lbuf, 256, fbuf);
                if (strstr(lbuf, lbp)) {
@@ -238,54 +264,52 @@ apro(void)
        pr("Limit of %d lines exceeded\n", lhitlim);
     }
     pr("Found %s in %ld of %ld files and in %ld of %ld lines\n",
-       bp, nhf, nf, nhl, nl);
+       name, nhf, nf, nhl, nl);
     return RET_OK;
 }
 
-#else
+#else  /* _WIN32 */
 
 int
 info(void)
 {
-    s_char buf[255];
+    char buf[255];
     FILE *fp;
-    s_char *bp;
-    s_char *bp2;
-    s_char filename[1024];
+    char *name;
+    char *tmp_name;
+    char filename[1024];
+    char last[256];
+    int nmatch = 0;
+    int width = 0;
+    char sep;
 
-    if (player->argp[1] == 0 || !*player->argp[1])
-       bp = "TOP";
-    else {
+    name = player->argp[1];
+    if (name) {
        /*
         * don't let sneaky people go outside the info directory
         */
-       bp = player->argp[1];
-       if (NULL != (bp2 = strrchr(bp, '/')))
-           bp = ++bp2;
-       if (NULL != (bp2 = strrchr(bp, '\\')))
-           bp = ++bp2;
-       if (NULL != (bp2 = strrchr(bp, ':')))
-           bp = ++bp2;
-       if (!*bp)
-           bp = "TOP";
+       if (NULL != (tmp_name = strrchr(name, '/')))
+           name = tmp_name + 1;
+       if (NULL != (tmp_name = strrchr(name, '\\')))
+           name = tmp_name + 1;
+       if (NULL != (tmp_name = strrchr(name, ':')))
+           name = tmp_name + 1;
     }
+    if (!name || !*name)
+       name = "TOP";
 
-    strncpy(filename, infodir, sizeof(filename) - 2);
-    strcat(filename, "\\");
-    strncat(filename, bp, sizeof(filename) - 1 - strlen(filename));
-    fp = fopen(filename, "r");
+    snprintf(filename, sizeof(filename) - 1, "%s\\%s", infodir, name);
+    fp = fopen(filename, "rb");
     if (fp == NULL) {
-       /* may be a "partial" request.  */
+       /* may be a "partial" request. */
        HANDLE hDir;
        WIN32_FIND_DATA fData;
-       int len = strlen(bp);
-       strncat(filename, "*", sizeof(filename) - 1 - strlen(filename));
+       strcat(filename, "*");
        hDir = FindFirstFile(filename, &fData);
        if (hDir == INVALID_HANDLE_VALUE) {
            switch (GetLastError()) {
            case ERROR_FILE_NOT_FOUND:
-               pr("Sorry, there is no info on %s\n", bp);
-               return RET_FAIL;
+               pr("Sorry, there is no info on %s\n", name);
                break;
            case ERROR_PATH_NOT_FOUND:
                pr("Can't open info dir\n");
@@ -293,43 +317,66 @@ info(void)
                break;
            default:
                pr("Error reading info dir\n");
-               logerror("Error (%d) reading info dir(%s)/file(%s)",
-                   infodir, filename, GetLastError());
+               logerror("Error (%lu) reading info dir(%s)\\file(%s)",
+                        GetLastError(), infodir, filename);
            }
-           return RET_SYS;
+           return RET_FAIL;
        }
        do {
-           if (((fData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) ||
+           if ((fData.dwFileAttributes != (DWORD)-1) &&
+               ((fData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) ||
                 (fData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) ||
                 (fData.dwFileAttributes == FILE_ATTRIBUTE_READONLY)) &&
-               (strnccmp(bp, fData.cFileName, len) == 0)) {
-               strncpy(filename, infodir, sizeof(filename) - 2);
-               strcat(filename, "\\");
-               strncat(filename, fData.cFileName,
-                       sizeof(filename) - 1 - strlen(filename));
-               fp = fopen(filename, "r");
+               (strncasecmp(name, fData.cFileName, strlen(name)) == 0)) {
+               nmatch++;
+               if (nmatch == 1) {
+                   snprintf(last, sizeof(last), "%s", fData.cFileName);
+               } else {
+                   if (nmatch == 2) {
+                       pr("`%s' is ambiguous.  The following topics match:\n%s",
+                           name, last);
+                       width = strlen(last);
+                   }
+                   width += 2 + strlen(fData.cFileName);
+                   sep = ' ';
+                   if (width > 75) {
+                       sep = '\n';
+                       width = strlen(fData.cFileName);
+                   }
+                   pr(",%c%s", sep, fData.cFileName);
+               }
            }
-       } while (!fp && FindNextFile(hDir, &fData));
+       } while (FindNextFile(hDir, &fData));
        FindClose(hDir);
+       if (nmatch == 0) {
+           pr("Sorry, there is no info on %s\n", name);
+           return RET_FAIL;
+       } else if (nmatch > 1) {
+           pr(".\n");
+           return RET_FAIL;
+       }
+       snprintf(filename, sizeof(filename), "%s/%s", infodir, last);
+       fp = fopen(filename, "rb");
        if (fp == NULL) {
-           pr("Sorry, there is no info on %s\n", bp);
+           pr("Error reading info file for %s\n", name);
+           logerror("Cannot open for \"%s\" info file (%s)",
+                    filename, strerror(errno));
            return RET_FAIL;
        }
-    }
-    else {
+    } else {
        DWORD fAttrib = GetFileAttributes(filename);
-       if ((fAttrib == (DWORD)-1) && //INVALID_FILE_ATTRIBUTES
-           (fAttrib != FILE_ATTRIBUTE_NORMAL) &&
-           (fAttrib != FILE_ATTRIBUTE_ARCHIVE) &&
-           (fAttrib != FILE_ATTRIBUTE_READONLY)) {
-           pr("Error reading info file for %s\n", bp);
-           logerror("The info file \"%s\" is not regular file\n", filename);
+       if ((fAttrib == (DWORD)-1) || /* INVALID_FILE_ATTRIBUTES */
+           ((fAttrib != FILE_ATTRIBUTE_NORMAL) &&
+            (fAttrib != FILE_ATTRIBUTE_ARCHIVE) &&
+            (fAttrib != FILE_ATTRIBUTE_READONLY))) {
+           pr("Error reading info file for %s\n", name);
+           logerror("The info file \"%s\" is not regular file\n",
+                    filename);
            fclose(fp);
-           return RET_SYS;
+           return RET_FAIL;
        }
     }
 
-    pr("Information on:  %s", bp);
     while (fgets(buf, sizeof(buf), fp) != 0)
        pr("%s", buf);
     (void)fclose(fp);
@@ -342,10 +389,10 @@ apro(void)
     HANDLE hDir;
     WIN32_FIND_DATA fData;
     FILE *fp;
-    s_char *bp, *lbp;
-    s_char *fbuf;
-    s_char *lbuf;
-    s_char filename[1024];
+    char *name, *lbp;
+    char *fbuf;
+    char *lbuf;
+    char filename[1024];
     long nf, nhf, nl, nlhl, nhl, nll;
     int alreadyhit;
     int lhitlim;
@@ -362,63 +409,73 @@ apro(void)
            lhitlim = 100;
     }
 
-    strncpy(filename, infodir, sizeof(filename) - 3);
-    strcat(filename, "//*");
+    snprintf(filename, sizeof(filename), "%s\\*", infodir);
     hDir = FindFirstFile(filename, &fData);
     if (hDir == INVALID_HANDLE_VALUE) {
+       if (GetLastError() == ERROR_PATH_NOT_FOUND) {
+           pr("Can't open info dir\n");
+           logerror("Can't open info dir \"%s\"", infodir);
+       } else {
+           pr("Error reading info dir\n");
+           logerror("Error (%lu) reading info dir(%s)\\file(%s)",
+                    GetLastError(), infodir, filename);
+       }
        return RET_FAIL;
     }
 
-    fbuf = (s_char *)malloc(256);
-    lbuf = (s_char *)malloc(256);
-    lbp = (s_char *)malloc(256);
+    fbuf = malloc(256);
+    lbuf = malloc(256);
+    lbp = malloc(256);
 
     /*
      *  lower case search string into lbp
      */
-    bp = player->argp[1];
-    lowerit(lbp, 256, bp);
+    name = player->argp[1];
+    lowerit(lbp, 256, name);
 
     /*
      *  search
      */
     nf = nhf = nl = nhl = 0;
     do {
-       strncpy(filename, infodir, sizeof(filename) - 3);
-       strcat(filename, "//");
-       strncat(filename, fData.cFileName,
-               sizeof(filename) - 1 - strlen(filename));
-       fp = fopen(filename, "r");
-       alreadyhit = 0;
-       nll = nlhl = 0;
-       if (fp != NULL) {
-           while (fgets(fbuf, 256, fp)) {
-               lowerit(lbuf, 256, fbuf);
-               if (strstr(lbuf, lbp)) {
-                   if (!alreadyhit) {
-                       pr("*** %s ***\n", fData.cFileName);
-                       alreadyhit = 1;
-                       nhf++;
+       if ((fData.dwFileAttributes != (DWORD)-1) &&
+           ((fData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) ||
+            (fData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) ||
+            (fData.dwFileAttributes == FILE_ATTRIBUTE_READONLY))) {
+           snprintf(filename, sizeof(filename), "%s\\%s", infodir,
+                    fData.cFileName);
+           fp = fopen(filename, "rb");
+           alreadyhit = 0;
+           nll = nlhl = 0;
+           if (fp != NULL) {
+               while (fgets(fbuf, 256, fp)) {
+                   lowerit(lbuf, 256, fbuf);
+                   if (strstr(lbuf, lbp)) {
+                       if (!alreadyhit) {
+                           pr("*** %s ***\n", fData.cFileName);
+                           alreadyhit = 1;
+                           nhf++;
+                       }
+                       fbuf[74] = '\n';
+                       fbuf[75] = 0;
+                       pr("   %s", fbuf);
+                       nlhl++;
+                       /*
+                        * break if too many lines
+                        */
+                       if ((nhl + nlhl) > lhitlim)
+                           break;
                    }
-                   fbuf[74] = '\n';
-                   fbuf[75] = 0;
-                   pr("   %s", fbuf);
-                   nlhl++;
-                   /*
-                    * break if too many lines
-                    */
-                   if ((nhl + nlhl) > lhitlim)
-                       break;
+                   nll++;
                }
-               nll++;
+               fclose(fp);
            }
-           fclose(fp);
+           nhl += nlhl;
+           nl += nll;
+           nf++;
+           if (nhl > lhitlim)
+               break;
        }
-       nhl += nlhl;
-       nl += nll;
-       nf++;
-       if (nhl > lhitlim)
-           break;
     } while (FindNextFile(hDir, &fData));
     FindClose(hDir);
 
@@ -427,11 +484,11 @@ apro(void)
     free(lbp);
 
     if ((nhl) > lhitlim) {
-       pr("Limit of %ld lines exceeded\n", lhitlim);
+       pr("Limit of %d lines exceeded\n", lhitlim);
     }
     pr("Found %s in %ld of %ld files and in %ld of %ld lines\n",
-       bp, nhf, nf, nhl, nl);
+       name, nhf, nf, nhl, nl);
     return RET_OK;
 }
 
-#endif
+#endif /* _WIN32 */