]> 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 61a0b9bc1fc4dff53df7272dd2dcf3ff56fa5dac..961ad606b4c93c330d5783f15bd1effd644ca36e 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2008, 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
@@ -42,9 +41,8 @@
 #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"
@@ -79,7 +77,7 @@ info(void)
     int nmatch = 0;
     int width = 0;
     char sep;
+
     name = player->argp[1];
     if (name) {
        /*
@@ -90,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_FAIL;
        }
 
-       while ((dp = readdir(info_dp)) != 0) {
+       while ((dp = readdir(info_dp))) {
            if (strncasecmp(name, dp->d_name, strlen(name)) != 0)
                continue;
            nmatch++;
@@ -131,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);
@@ -148,15 +145,14 @@ info(void)
        fclose(fp);
        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_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;
@@ -177,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;
     }
@@ -191,7 +187,7 @@ 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_FAIL;
     }
@@ -210,11 +206,10 @@ apro(void)
      *  search
      */
     nf = nhf = nl = nhl = 0;
-    while ((dp = readdir(info_dp)) != 0) {
+    while ((dp = readdir(info_dp))) {
        if (dp->d_name[0] == '.')
            continue;
-       snprintf(filename, sizeof(filename), "%s/%s", infodir,
-                dp->d_name);
+       snprintf(filename, sizeof(filename), "%s/%s", infodir, dp->d_name);
        fp = fopen(filename, "r");
        alreadyhit = 0;
        nll = nlhl = 0;
@@ -225,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);
@@ -306,7 +301,7 @@ info(void)
     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, "*");
@@ -323,7 +318,7 @@ 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_FAIL;
        }
@@ -360,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);
@@ -369,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) &&
@@ -384,7 +377,6 @@ info(void)
        }
     }
 
-    pr("Information on:  %s", name);
     while (fgets(buf, sizeof(buf), fp) != 0)
        pr("%s", buf);
     (void)fclose(fp);
@@ -417,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) {
@@ -426,7 +418,7 @@ 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_FAIL;
     }
@@ -451,7 +443,7 @@ apro(void)
             (fData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) ||
             (fData.dwFileAttributes == FILE_ATTRIBUTE_READONLY))) {
            snprintf(filename, sizeof(filename), "%s\\%s", infodir,
-                     fData.cFileName);
+                    fData.cFileName);
            fp = fopen(filename, "rb");
            alreadyhit = 0;
            nll = nlhl = 0;