]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
Remove unnecessary uses of `defined' in #if* tests.
authorJim Meyering <jim@meyering.net>
Fri, 16 Jun 1995 03:14:19 +0000 (03:14 +0000)
committerJim Meyering <jim@meyering.net>
Fri, 16 Jun 1995 03:14:19 +0000 (03:14 +0000)
Don't use #elif.  Some older compilers don't grok it.
(split_3): New function to parse out sum, flag, and filename
when reading check file.
(hex_digits): Remove length parameter since string parameter is
now nul-terminated.
(main): Don't allocate separate arrays for filename and sum
when checking.  Get pointers into line buffer with split_3 instead
of using sscanf.

src/md5sum.c

index bd88976ae8aedbd0c65420c948e751fc189a05f8..2154209f6e49a0b53ef5367b9b5af17d2eceb312 100644 (file)
@@ -32,7 +32,7 @@
 #include <stdio.h>
 #include <sys/types.h>
 
-#if defined (HAVE_LIMITS_H) || defined (_LIBC)
+#if HAVE_LIMITS_H || _LIBC
 # include <limits.h>
 #endif
 
 # ifdef MSDOS
 #  define TEXT1TO1 "rb"
 #  define TEXTCNVT "r"
-# elif defined VMS
-#  define TEXT1TO1 "rb", "ctx=stm"
-#  define TEXTCNVT "r", "ctx=stm"
+# else
+#  if defined VMS
+#   define TEXT1TO1 "rb", "ctx=stm"
+#   define TEXTCNVT "r", "ctx=stm"
+#  endif
 # endif
 # define FILETYPE FILE *
 # define STDINFILE stdin
@@ -166,15 +168,72 @@ static void process_buffer __P ((const void *buffer, size_t len,
 
 #ifndef USE_AS_LIBRARY
 
+/* FIXME: but this won't work with filenames containing blanks.  */
+/* FIXME: This is provisory.  Use strtok.  */
+
 static int
-hex_digits (const char *md5num, size_t len)
+split_3 (char *s, char **u, char **v, char **w)
 {
   size_t i;
+  char *p[3];
+
+#define ISWHITE(c) ((c) == ' ' || (c) == '\t')
+
+  i = 0;
+  while (s[i] && ISWHITE (s[i]))
+    ++i;
+  if (s[i])
+    {
+      p[0] = &s[i];
+      while (s[i] && !ISWHITE (s[i]))
+       ++i;
+      if (s[i])
+       s[i++] = '\0';
+      while (s[i] && ISWHITE (s[i]))
+       ++i;
+      if (s[i])
+       {
+         p[1] = &s[i];
+         while (s[i] && !ISWHITE (s[i]))
+           ++i;
+         if (s[i])
+           s[i++] = '\0';
+         while (s[i] && ISWHITE (s[i]))
+           ++i;
+         if (s[i])
+           {
+             p[2] = &s[i];
+             /* Skip past the third token.  */
+             while (s[i] && !ISWHITE (s[i]))
+               ++i;
+             if (s[i])
+               s[i++] = '\0';
+             /* Allow trailing white space.  */
+             while (s[i] && ISWHITE (s[i]))
+               ++i;
+             if (!s[i])
+               {
+                 *u = p[0];
+                 *v = p[1];
+                 *w = p[2];
+                 return 0;
+               }
+           }
+       }
+    }
+  return 1;
+}
 
-  for (i = 0; i < len; ++i)
+/* FIXME: use strcspn.  */
+
+static int
+hex_digits (const char *s)
+{
+  while (*s)
     {
-      if (!ISXDIGIT (md5num[i]))
+      if (!ISXDIGIT (*s))
         return 0;
+      ++s;
     }
   return 1;
 }
@@ -315,21 +374,27 @@ main (argc, argv)
       do
        {
          char line[1024];
-         char filename[FILENAME_MAX];
-         char type_flag;
-         char md5num[32];
-         int items;
+         char *filename;
+         char *type_flag;
+         char *md5num;
+         int err;
 
-         /* FIXME: It may be better to use getline here.  */
+         /* FIXME: Use getline, not fgets.  */
          if (fgets (line, 1024, cfp) == NULL)
            break;
 
+         /* Remove any trailing newline.  */
+         if (line[strlen (line) - 1] == '\n')
+           line[strlen (line) - 1] = '\0';
+
          /* FIXME: maybe accept the output of --string=STRING.  */
-         items = sscanf (line, "%32c %c %s", md5num, &type_flag, filename);
+         err = split_3 (line, &md5num, &type_flag, &filename);
 
-         if (items != 3
-             || !hex_digits (md5num, 32)
-             || (type_flag != 'b' && type_flag != 't'))
+         if (err
+             || strlen (md5num) != 32
+             || !hex_digits (md5num)
+             || strlen (type_flag) != 1
+             || (*type_flag != 'b' && *type_flag != 't'))
            {
              if (verbose)
                error (0, 0, _("invalid line in check file: %s"), line);
@@ -347,7 +412,7 @@ main (argc, argv)
                fflush (stdout);
 
              ++n_tests;
-             md5_file (filename, md5buffer, type_flag == 'b');
+             md5_file (filename, md5buffer, *type_flag == 'b');
 
              /* Compare generated binary number with text representation
                 in check file.  Ignore case of hex digits.  */