]> git.pond.sub.org Git - empserver/blobdiff - src/lib/commands/info.c
Update copyright notice
[empserver] / src / lib / commands / info.c
index 9a1f3a9476b942b84741a65e6b5230dc6a6d89a8..d12ba196023aa5143417ccb0e83f681940879966 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2004, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -19,9 +19,9 @@
  *
  *  ---
  *
- *  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.
  *
  *  ---
  *
  *     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 <dirent.h>
 #else
 #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;
 }
 
@@ -69,14 +67,14 @@ lowerit(s_char *buf, int n, s_char *orig)
 int
 info(void)
 {
-    s_char buf[255];
+    char buf[255];
     FILE *fp;
-    s_char *name;
-    s_char *tmp_name;
+    char *name;
+    char *tmp_name;
     struct stat statb;
     struct dirent *dp;
-    s_char filename[1024];
-    s_char last[256];
+    char filename[1024];
+    char last[256];
     DIR *info_dp;
     int nmatch = 0;
     int width = 0;
@@ -96,7 +94,6 @@ info(void)
     snprintf(filename, sizeof(filename), "%s/%s", infodir, name);
     fp = fopen(filename, "r");
     if (fp == NULL) {
-       int len = strlen(name);
        /* may be a "partial" request.  */
        info_dp = opendir(infodir);
        if (info_dp == 0) {
@@ -140,14 +137,14 @@ info(void)
        if (fp == NULL) {
            pr("Error reading info file for %s\n", name);
            logerror("Cannot open for \"%s\" info file (%s)",
-               filename, strerror(errno));
+                    filename, strerror(errno));
            return RET_FAIL;
        }
     }
     if (fstat(fileno(fp), &statb) < 0) {
        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;
     }
@@ -169,11 +166,11 @@ int
 apro(void)
 {
     FILE *fp;
-    s_char *name, *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;
@@ -199,9 +196,9 @@ apro(void)
        return RET_SYS;
     }
 
-    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
@@ -214,8 +211,10 @@ apro(void)
      */
     nf = nhf = nl = nhl = 0;
     while ((dp = readdir(info_dp)) != 0) {
+       if (dp->d_name[0] == '.')
+           continue;
        snprintf(filename, sizeof(filename), "%s/%s", infodir,
-           dp->d_name);
+                dp->d_name);
        fp = fopen(filename, "r");
        alreadyhit = 0;
        nll = nlhl = 0;
@@ -274,17 +273,17 @@ apro(void)
     return RET_OK;
 }
 
-#else
+#else  /* _WIN32 */
 
 int
 info(void)
 {
-    s_char buf[255];
+    char buf[255];
     FILE *fp;
-    s_char *name;
-    s_char *tmp_name;
-    s_char filename[1024];
-    s_char last[256];
+    char *name;
+    char *tmp_name;
+    char filename[1024];
+    char last[256];
     int nmatch = 0;
     int width = 0;
     char sep;
@@ -304,13 +303,12 @@ info(void)
     if (!name || !*name)
        name = "TOP";
 
-    _snprintf(filename, sizeof(filename) - 1, "%s\\%s", infodir, name);
-    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.  */
        HANDLE hDir;
        WIN32_FIND_DATA fData;
-       int len = strlen(name);
        strcat(filename, "*");
        hDir = FindFirstFile(filename, &fData);
        if (hDir == INVALID_HANDLE_VALUE) {
@@ -325,8 +323,8 @@ 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;
        }
@@ -338,7 +336,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",
@@ -363,22 +361,22 @@ info(void)
            pr(".\n");
            return RET_FAIL;
        }
-       _snprintf(filename, sizeof(filename), "%s/%s",
+       snprintf(filename, sizeof(filename), "%s/%s",
                  infodir, last);
-       fp = fopen(filename, "r");
+       fp = fopen(filename, "rb");
        if (fp == NULL) {
            pr("Error reading info file for %s\n", name);
            logerror("Cannot open for \"%s\" info file (%s)",
-               filename, strerror(errno));
+                    filename, strerror(errno));
            return RET_FAIL;
        }
     }
     else {
        DWORD fAttrib = GetFileAttributes(filename);
-       if ((fAttrib == (DWORD)-1) || //INVALID_FILE_ATTRIBUTES
-           (fAttrib != FILE_ATTRIBUTE_NORMAL) &&
-           (fAttrib != FILE_ATTRIBUTE_ARCHIVE) &&
-           (fAttrib != FILE_ATTRIBUTE_READONLY)) {
+       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);
@@ -400,10 +398,10 @@ apro(void)
     HANDLE hDir;
     WIN32_FIND_DATA fData;
     FILE *fp;
-    s_char *name, *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;
@@ -420,7 +418,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) {
@@ -428,15 +426,15 @@ apro(void)
            logerror("Can't open info dir \"%s\"", infodir);
        } else {
            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;
     }
 
-    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
@@ -453,9 +451,9 @@ apro(void)
            ((fData.dwFileAttributes == FILE_ATTRIBUTE_NORMAL) ||
             (fData.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) ||
             (fData.dwFileAttributes == FILE_ATTRIBUTE_READONLY))) {
-           _snprintf(filename, sizeof(filename), "%s\\%s", infodir,
+           snprintf(filename, sizeof(filename), "%s\\%s", infodir,
                      fData.cFileName);
-           fp = fopen(filename, "r");
+           fp = fopen(filename, "rb");
            alreadyhit = 0;
            nll = nlhl = 0;
            if (fp != NULL) {
@@ -495,11 +493,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",
        name, nhf, nf, nhl, nl);
     return RET_OK;
 }
 
-#endif
+#endif /* _WIN32 */