bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

dd minor cleanup to improve conv= diagnostics


From: Paul Eggert
Subject: dd minor cleanup to improve conv= diagnostics
Date: Fri, 03 Sep 2004 01:14:23 -0700

I installed this minor code cleanup.  The only externally-visible
effect is to improve the quality of diagonstics for invalid usages of
incompatible conversions, and to fix a typo in the usage message.

2004-09-03  Paul Eggert  <address@hidden>

        * src/dd.c (usage): "alternated EBCDIC" -> "alternate EBCDIC".
        (bit_count): Remove.  All uses changed to....
        (multiple_bits_set): New function.
        (scanargs): Use it, and check separately for each set of
        incompatible options, to improve diagnostics.
        (MX): Remove.
        (apply_translations): Move checks for incompatible options
        to scanargs, so that they're done consistently.

Index: dd.c
===================================================================
RCS file: /home/eggert/coreutils/cu/src/dd.c,v
retrieving revision 1.164
retrieving revision 1.165
diff -p -u -r1.164 -r1.165
--- dd.c        2 Aug 2004 23:29:39 -0000       1.164
+++ dd.c        3 Sep 2004 08:11:34 -0000       1.165
@@ -365,7 +365,7 @@ Each CONV symbol may be:\n\
       fputs (_("\
   ascii     from EBCDIC to ASCII\n\
   ebcdic    from ASCII to EBCDIC\n\
-  ibm       from ASCII to alternated EBCDIC\n\
+  ibm       from ASCII to alternate EBCDIC\n\
   block     pad newline-terminated records with spaces to cbs-size\n\
   unblock   replace trailing spaces in cbs-size records with newline\n\
   lcase     change upper case to lower case\n\
@@ -424,16 +424,12 @@ translate_charset (char const *new_trans
   translation_needed = true;
 }
 
-/* Return the number of 1 bits in `i'. */
+/* Return true if I has more than one bit set.  I must be nonnegative.  */
 
-static int
-bit_count (register int i)
+static inline bool
+multiple_bits_set (int i)
 {
-  register int set_bits;
-
-  for (set_bits = 0; i != 0; set_bits++)
-    i &= i - 1;
-  return set_bits;
+  return (i & (i - 1)) != 0;
 }
 
 static void
@@ -725,7 +721,13 @@ scanargs (int argc, char **argv)
   if (input_flags & (O_DSYNC | O_SYNC))
     input_flags |= O_RSYNC;
 
-  if ((conversions_mask & (C_EXCL | C_NOCREAT)) == (C_EXCL | C_NOCREAT))
+  if (multiple_bits_set (conversions_mask & (C_ASCII | C_EBCDIC | C_IBM)))
+    error (EXIT_FAILURE, 0, _("cannot combine any two of {ascii,ebcdic,ibm}"));
+  if (multiple_bits_set (conversions_mask & (C_BLOCK | C_UNBLOCK)))
+    error (EXIT_FAILURE, 0, _("cannot combine block and unblock"));
+  if (multiple_bits_set (conversions_mask & (C_LCASE | C_UCASE)))
+    error (EXIT_FAILURE, 0, _("cannot combine lcase and ucase"));
+  if (multiple_bits_set (conversions_mask & (C_EXCL | C_NOCREAT)))
     error (EXIT_FAILURE, 0, _("cannot combine excl and nocreat"));
 }
 
@@ -735,16 +737,6 @@ static void
 apply_translations (void)
 {
   int i;
-
-#define MX(a) (bit_count (conversions_mask & (a)))
-  if ((MX (C_ASCII | C_EBCDIC | C_IBM) > 1)
-      || (MX (C_BLOCK | C_UNBLOCK) > 1)
-      || (MX (C_LCASE | C_UCASE) > 1))
-    {
-      error (EXIT_FAILURE, 0, _("\
-       only one conv in {ascii,ebcdic,ibm}, {lcase,ucase}, {block,unblock}"));
-    }
-#undef MX
 
   if (conversions_mask & C_ASCII)
     translate_charset (ebcdic_to_ascii);




reply via email to

[Prev in Thread] Current Thread [Next in Thread]