]> 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 f62ee0aee664af0b93faff779a94a648e2acf8fb..961ad606b4c93c330d5783f15bd1effd644ca36e 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2006, 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,
@@ -14,8 +14,7 @@
  *  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/>.
  *
  *  ---
  *
@@ -26,7 +25,7 @@
  *  ---
  *
  *  info.c: display an info page
- * 
+ *
  *  Known contributors to this file:
  *     Dave Pare, 1986
  *     Mike Wise, 1997 - added apropos and case insensitivity
 
 #include <config.h>
 
+#include <ctype.h>
 #include <errno.h>
-#include <string.h>
-#include <stdio.h>
 #include <sys/stat.h>
-#include <time.h>
+#include <stdio.h>
 #if !defined(_WIN32)
+#include <strings.h>
 #include <dirent.h>
-#else
-#include <windows.h>
 #endif
-#include "misc.h"
-#include "player.h"
 #include "commands.h"
 #include "optlist.h"
 
@@ -59,12 +54,9 @@ lowerit(char *buf, int n, char *orig)
     /* size of output buffer */
     /* input strig */
     int i;
-    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;
 }
 
@@ -85,7 +77,7 @@ info(void)
     int nmatch = 0;
     int width = 0;
     char sep;
+
     name = player->argp[1];
     if (name) {
        /*
@@ -96,19 +88,19 @@ info(void)
     }
     if (!name || !*name)
        name = "TOP";
+
     snprintf(filename, sizeof(filename), "%s/%s", infodir, name);
     fp = fopen(filename, "r");
     if (fp == NULL) {
-       /* 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;
        }
 
-       while ((dp = readdir(info_dp)) != 0) {
+       while ((dp = readdir(info_dp))) {
            if (strncasecmp(name, dp->d_name, strlen(name)) != 0)
                continue;
            nmatch++;
@@ -137,8 +129,7 @@ info(void)
            pr(".\n");
            return RET_FAIL;
        }
-       snprintf(filename, sizeof(filename), "%s/%s", infodir,
-                last);
+       snprintf(filename, sizeof(filename), "%s/%s", infodir, last);
        fp = fopen(filename, "r");
        if (fp == NULL) {
            pr("Error reading info file for %s\n", name);
@@ -152,17 +143,16 @@ info(void)
        logerror("Cannot fstat for \"%s\" info file (%s)",
                 filename, strerror(errno));
        fclose(fp);
-       return RET_SYS;
+       return RET_FAIL;
     }
-    if ((statb.st_mode & S_IFREG) == 0) {
+    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",
-       name, 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;
@@ -183,7 +173,7 @@ apro(void)
     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;
     }
@@ -197,9 +187,9 @@ apro(void)
 
     info_dp = opendir(infodir);
     if (info_dp == NULL) {
-       pr("Can't open info dir \n");
+       pr("Can't open info dir\n");
        logerror("Can't open info dir \"%s\"", infodir);
-       return RET_SYS;
+       return RET_FAIL;
     }
 
     fbuf = malloc(256);
@@ -216,9 +206,10 @@ apro(void)
      *  search
      */
     nf = nhf = nl = nhl = 0;
-    while ((dp = readdir(info_dp)) != 0) {
-       snprintf(filename, sizeof(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;
@@ -229,7 +220,7 @@ apro(void)
                fclose(fp);
                continue;
            }
-           if ((statb.st_mode & S_IFREG) == 0) {
+           if (!S_ISREG(statb.st_mode)) {
                logerror("The info file \"%s\" is not regular file\n",
                         filename);
                fclose(fp);
@@ -307,10 +298,10 @@ info(void)
     if (!name || !*name)
        name = "TOP";
 
-    _snprintf(filename, sizeof(filename) - 1, "%s\\%s", infodir, name);
+    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;
        strcat(filename, "*");
@@ -319,7 +310,6 @@ info(void)
            switch (GetLastError()) {
            case ERROR_FILE_NOT_FOUND:
                pr("Sorry, there is no info on %s\n", name);
-               return RET_FAIL;
                break;
            case ERROR_PATH_NOT_FOUND:
                pr("Can't open info dir\n");
@@ -328,9 +318,9 @@ info(void)
            default:
                pr("Error reading info dir\n");
                logerror("Error (%lu) reading info dir(%s)\\file(%s)",
-                   GetLastError(), infodir, filename);
+                        GetLastError(), infodir, filename);
            }
-           return RET_SYS;
+           return RET_FAIL;
        }
        do {
            if ((fData.dwFileAttributes != (DWORD)-1) &&
@@ -340,7 +330,7 @@ info(void)
                (strncasecmp(name, fData.cFileName, strlen(name)) == 0)) {
                nmatch++;
                if (nmatch == 1) {
-                   _snprintf(last, sizeof(last), "%s", fData.cFileName);
+                   snprintf(last, sizeof(last), "%s", fData.cFileName);
                } else {
                    if (nmatch == 2) {
                        pr("`%s' is ambiguous.  The following topics match:\n%s",
@@ -365,8 +355,7 @@ info(void)
            pr(".\n");
            return RET_FAIL;
        }
-       _snprintf(filename, sizeof(filename), "%s/%s",
-                 infodir, last);
+       snprintf(filename, sizeof(filename), "%s/%s", infodir, last);
        fp = fopen(filename, "rb");
        if (fp == NULL) {
            pr("Error reading info file for %s\n", name);
@@ -374,8 +363,7 @@ info(void)
                     filename, strerror(errno));
            return RET_FAIL;
        }
-    }
-    else {
+    } else {
        DWORD fAttrib = GetFileAttributes(filename);
        if ((fAttrib == (DWORD)-1) || /* INVALID_FILE_ATTRIBUTES */
            ((fAttrib != FILE_ATTRIBUTE_NORMAL) &&
@@ -385,11 +373,10 @@ info(void)
            logerror("The info file \"%s\" is not regular file\n",
                     filename);
            fclose(fp);
-           return RET_SYS;
+           return RET_FAIL;
        }
     }
 
-    pr("Information on:  %s", name);
     while (fgets(buf, sizeof(buf), fp) != 0)
        pr("%s", buf);
     (void)fclose(fp);
@@ -422,7 +409,7 @@ apro(void)
            lhitlim = 100;
     }
 
-    _snprintf(filename, sizeof(filename), "%s\\*",infodir);
+    snprintf(filename, sizeof(filename), "%s\\*", infodir);
     hDir = FindFirstFile(filename, &fData);
     if (hDir == INVALID_HANDLE_VALUE) {
        if (GetLastError() == ERROR_PATH_NOT_FOUND) {
@@ -431,9 +418,9 @@ apro(void)
        } else {
            pr("Error reading info dir\n");
            logerror("Error (%lu) reading info dir(%s)\\file(%s)",
-               GetLastError(), infodir, filename);
+                    GetLastError(), infodir, filename);
        }
-       return RET_SYS;
+       return RET_FAIL;
     }
 
     fbuf = malloc(256);
@@ -455,8 +442,8 @@ apro(void)
            ((fData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) ||
             (fData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) ||
             (fData.dwFileAttributes == FILE_ATTRIBUTE_READONLY))) {
-           _snprintf(filename, sizeof(filename), "%s\\%s", infodir,
-                     fData.cFileName);
+           snprintf(filename, sizeof(filename), "%s\\%s", infodir,
+                    fData.cFileName);
            fp = fopen(filename, "rb");
            alreadyhit = 0;
            nll = nlhl = 0;