* src/ls.c (quote_name): Use initializer rather than memset to
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 10 Oct 2006 22:57:07 +0000 (22:57 +0000)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 10 Oct 2006 22:57:07 +0000 (22:57 +0000)
initialize an object to zero.  This is easier to read and is less
likely to introduce an runtime error due to a mixup.  It causes
gcc -W to issue a warning, but you can work around this by
appending -Wno-missing-field-initializers.
* src/pathchk.c (portable_chars_only): Likewise.
* src/shred.c (main): Likewise.
* src/stty.c (main): Likewise.
* src/tr.c (card_of_complement): Likewise.
* src/wc.c (wc): Likewise.

ChangeLog
src/ls.c
src/pathchk.c
src/shred.c
src/stty.c
src/tr.c
src/wc.c

index 1f68ad2..625bc8d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2006-10-10  Paul Eggert  <eggert@cs.ucla.edu>
+
+       * src/ls.c (quote_name): Use initializer rather than memset to
+       initialize an object to zero.  This is easier to read and is less
+       likely to introduce an runtime error due to a mixup.  It causes
+       gcc -W to issue a warning, but you can work around this by
+       appending -Wno-missing-field-initializers.
+       * src/pathchk.c (portable_chars_only): Likewise.
+       * src/shred.c (main): Likewise.
+       * src/stty.c (main): Likewise.
+       * src/tr.c (card_of_complement): Likewise.
+       * src/wc.c (wc): Likewise.
+
 2006-10-09  Paul Eggert  <eggert@cs.ucla.edu>
 
        * src/sort.c (usage): Mention again that sort fields are origin 1.
index 573f5f5..c63330e 100644 (file)
--- a/src/ls.c
+++ b/src/ls.c
@@ -3604,8 +3604,7 @@ quote_name (FILE *out, const char *name, struct quoting_options const *options,
                     reach its end, replacing each non-printable multibyte
                     character with a single question mark.  */
                  {
-                   mbstate_t mbstate;
-                   memset (&mbstate, 0, sizeof mbstate);
+                   mbstate_t mbstate = { 0, };
                    do
                      {
                        wchar_t wc;
index 04b46d8..2fc55d3 100644 (file)
@@ -200,11 +200,8 @@ portable_chars_only (char const *file, size_t filelen)
 
   if (*invalid)
     {
-      mbstate_t mbstate;
-      size_t charlen;
-
-      memset (&mbstate, 0, sizeof mbstate);
-      charlen = mbrlen (invalid, filelen - validlen, &mbstate);
+      mbstate_t mbstate = { 0, };
+      size_t charlen = mbrlen (invalid, filelen - validlen, &mbstate);
       error (0, 0,
             _("nonportable character %s in file name %s"),
             quotearg_n_style_mem (1, locale_quoting_style, invalid,
index d3b1c4a..4c1819c 100644 (file)
@@ -1091,7 +1091,7 @@ int
 main (int argc, char **argv)
 {
   bool ok = true;
-  struct Options flags;
+  struct Options flags = { 0, };
   char **file;
   int n_files;
   int c;
@@ -1106,8 +1106,6 @@ main (int argc, char **argv)
 
   atexit (close_stdout);
 
-  memset (&flags, 0, sizeof flags);
-
   flags.n_iterations = DEFAULT_PASSES;
   flags.size = -1;
 
index 9e1ba0a..33a821d 100644 (file)
@@ -729,7 +729,10 @@ settings, CHAR is taken literally, or coded as in ^c, 0x37, 0177 or\n\
 int
 main (int argc, char **argv)
 {
-  struct termios mode;
+  /* Initialize to all zeroes so there is no risk memcmp will report a
+     spurious difference in an uninitialized portion of the structure.  */
+  struct termios mode = { 0, };
+
   enum output_type output_type;
   int optc;
   int argi = 0;
@@ -840,9 +843,6 @@ main (int argc, char **argv)
   else
     device_name = _("standard input");
 
-  /* Initialize to all zeroes so there is no risk memcmp will report a
-     spurious difference in an uninitialized portion of the structure.  */
-  memset (&mode, 0, sizeof (mode));
   if (tcgetattr (STDIN_FILENO, &mode))
     error (EXIT_FAILURE, errno, "%s", device_name);
 
@@ -1002,7 +1002,9 @@ main (int argc, char **argv)
 
   if (require_set_attr)
     {
-      struct termios new_mode;
+      /* Initialize to all zeroes so there is no risk memcmp will report a
+        spurious difference in an uninitialized portion of the structure.  */
+      struct termios new_mode = { 0, };
 
       if (tcsetattr (STDIN_FILENO, TCSADRAIN, &mode))
        error (EXIT_FAILURE, errno, "%s", device_name);
@@ -1014,9 +1016,6 @@ main (int argc, char **argv)
         this partial failure, get the current terminal attributes and
         compare them to the requested ones.  */
 
-      /* Initialize to all zeroes so there is no risk memcmp will report a
-        spurious difference in an uninitialized portion of the structure.  */
-      memset (&new_mode, 0, sizeof (new_mode));
       if (tcgetattr (STDIN_FILENO, &new_mode))
        error (EXIT_FAILURE, errno, "%s", device_name);
 
index faa38be..e18e558 100644 (file)
--- a/src/tr.c
+++ b/src/tr.c
@@ -1177,9 +1177,8 @@ card_of_complement (struct Spec_list *s)
 {
   int c;
   int cardinality = N_CHARS;
-  bool in_set[N_CHARS];
+  bool in_set[N_CHARS] = { 0, };
 
-  memset (in_set, 0, sizeof in_set);
   s->state = BEGIN_STATE;
   while ((c = get_next (s, NULL)) != -1)
     {
index e21fb7f..332f32d 100644 (file)
--- a/src/wc.c
+++ b/src/wc.c
@@ -273,7 +273,7 @@ wc (int fd, char const *file_x, struct fstatus *fstatus)
     {
       bool in_word = false;
       uintmax_t linepos = 0;
-      mbstate_t state;
+      mbstate_t state = { 0, };
       uintmax_t last_error_line = 0;
       int last_error_errno = 0;
 # if SUPPORT_OLD_MBRTOWC
@@ -289,7 +289,6 @@ wc (int fd, char const *file_x, struct fstatus *fstatus)
       const size_t prev = 0;
 # endif
 
-      memset (&state, 0, sizeof (mbstate_t));
       while ((bytes_read = safe_read (fd, buf + prev, BUFFER_SIZE - prev)) > 0)
        {
          const char *p;