bug-coreutils
[Top][All Lists]
Advanced

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

Re: adding a few 'const's


From: Jim Meyering
Subject: Re: adding a few 'const's
Date: Wed, 25 Oct 2006 01:06:02 +0200

Bruno Haible <address@hidden> wrote:
> Compiling coreutils-6.4 with gcc-4.1 and
>
> ./configure --prefix=/packages/gnu \
>   CPPFLAGS="-Wall -Wformat=2 -Wmissing-field-initializers 
> -Wmissing-format-attribute -Wpointer-arith -Wstrict-aliasing=2 
> -Wwrite-strings" \
>   CFLAGS="-O2 -g -Wbad-function-cast -Wdeclaration-after-statement"
>
> yields a few warnings. They point to places where the code readability and
> robustness can be increased by a few consts.

Thanks.  I've applied those, but with the e.g., "char const *" syntax
rather than "const char *".

2006-10-25  Bruno Haible  <address@hidden>

        * src/cat.c (infile): Add "const" to declaration.
        * src/csplit.c (prefix): Likewise.
        * src/printf.c (cfcc_msg): Likewise.
        * src/tail.c (valid_file_spec): Likewise.
        * src/cut.c (cut_file): Likewise, for a parameter.
        * src/expr.c (str_value): Likewise.
        * src/fold.c (fold_file): Likewise.
        * src/pr.c (init_header): Likewise.
        * src/dircolors.c (dc_parse_stream): Likewise, for a local.
        * src/tr.c (make_printable_str): Likewise.
        * src/nl.c (body_type, header_type, footer_type, current_type):
        (separator_str, build_type_arg, nl_file): Likewise, for many.
        * src/paste.c (main): Don't assign a read-only string to 'optarg'.
        * src/tac.c (separator, tac_seekable, copy_to_temp): Likewise.

diff --git a/src/cat.c b/src/cat.c
index ec2c528..90a73c2 100644
--- a/src/cat.c
+++ b/src/cat.c
@@ -55,7 +55,7 @@ #define max(h,i) ((h) > (i) ? (h) : (i))
 char *program_name;

 /* Name of input file.  May be "-".  */
-static char *infile;
+static char const *infile;

 /* Descriptor on which input file is open.  */
 static int input_desc;
diff --git a/src/csplit.c b/src/csplit.c
index 07bcb20..e174ee5 100644
--- a/src/csplit.c
+++ b/src/csplit.c
@@ -149,13 +149,13 @@ static uintmax_t current_line = 0;
 static bool have_read_eof = false;

 /* Name of output files. */
-static char * volatile filename_space = NULL;
+static char *volatile filename_space = NULL;

 /* Prefix part of output file names. */
-static char * volatile prefix = NULL;
+static char const *volatile prefix = NULL;

 /* Suffix part of output file names. */
-static char * volatile suffix = NULL;
+static char *volatile suffix = NULL;

 /* Number of digits to use in output file names. */
 static int volatile digits = 2;
diff --git a/src/cut.c b/src/cut.c
index 43eecb9..4faae4c 100644
--- a/src/cut.c
+++ b/src/cut.c
@@ -1,5 +1,5 @@
 /* cut - remove parts of lines of files
-   Copyright (C) 1997-2005 Free Software Foundation, Inc.
+   Copyright (C) 1997-2006 Free Software Foundation, Inc.
    Copyright (C) 1984 David M. Ihnat

    This program is free software; you can redistribute it and/or modify
@@ -702,7 +702,7 @@ cut_stream (FILE *stream)
    Return true if successful.  */

 static bool
-cut_file (char *file)
+cut_file (char const *file)
 {
   FILE *stream;

diff --git a/src/dircolors.c b/src/dircolors.c
index fc9319b..cf6e2a9 100644
--- a/src/dircolors.c
+++ b/src/dircolors.c
@@ -246,7 +246,7 @@ dc_parse_stream (FILE *fp, const char *f
   char *input_line = NULL;
   size_t input_line_size = 0;
   char const *line;
-  char *term;
+  char const *term;
   bool ok = true;

   /* State for the parser.  */
diff --git a/src/expr.c b/src/expr.c
index 99bbdd8..7f9f532 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -236,7 +236,7 @@ int_value (intmax_t i)
 /* Return a VALUE for S.  */

 static VALUE *
-str_value (char *s)
+str_value (char const *s)
 {
   VALUE *v = xmalloc (sizeof *v);
   v->type = string;
diff --git a/src/fold.c b/src/fold.c
index cef98c1..0d4ea58 100644
--- a/src/fold.c
+++ b/src/fold.c
@@ -1,5 +1,5 @@
 /* fold -- wrap each input line to fit in specified width.
-   Copyright (C) 91, 1995-2005 Free Software Foundation, Inc.
+   Copyright (C) 91, 1995-2006 Free Software Foundation, Inc.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -122,7 +122,7 @@ adjust_column (size_t column, char c)
    Return true if successful.  */

 static bool
-fold_file (char *filename, size_t width)
+fold_file (char const *filename, size_t width)
 {
   FILE *istream;
   int c;
diff --git a/src/nl.c b/src/nl.c
index 7ee57b7..e8502a8 100644
--- a/src/nl.c
+++ b/src/nl.c
@@ -64,16 +64,16 @@ enum section
 char *program_name;

 /* Format of body lines (-b).  */
-static char *body_type = "t";
+static char const *body_type = "t";

 /* Format of header lines (-h).  */
-static char *header_type = "n";
+static char const *header_type = "n";

 /* Format of footer lines (-f).  */
-static char *footer_type = "n";
+static char const *footer_type = "n";

 /* Format currently being used (body, header, or footer).  */
-static char *current_type;
+static char const *current_type;

 /* Regex for body lines to number (-bp).  */
 static struct re_pattern_buffer body_regex;
@@ -93,7 +93,7 @@ static char footer_fastmap[UCHAR_MAX + 1
 static struct re_pattern_buffer *current_regex = NULL;

 /* Separator string to print after line number (-s).  */
-static char *separator_str = "\t";
+static char const *separator_str = "\t";

 /* Input section delimiter string (-d).  */
 static char const *section_del = DEFAULT_SECTION_DELIMITERS;
@@ -235,9 +235,10 @@ FORMAT is one of:\n\
    according to `optarg'.  */

 static bool
-build_type_arg (char **typep, struct re_pattern_buffer *regexp, char *fastmap)
+build_type_arg (char const **typep,
+               struct re_pattern_buffer *regexp, char *fastmap)
 {
-  const char *errmsg;
+  char const *errmsg;
   bool rval = true;

   switch (*optarg)
@@ -414,7 +415,7 @@ process_file (FILE *fp)
    Return true if successful.  */

 static bool
-nl_file (const char *file)
+nl_file (char const *file)
 {
   FILE *stream;

diff --git a/src/paste.c b/src/paste.c
index 1903bcd..414fb88 100644
--- a/src/paste.c
+++ b/src/paste.c
@@ -463,9 +463,7 @@ main (int argc, char **argv)
        {
        case 'd':
          /* Delimiter character(s). */
-         if (optarg[0] == '\0')
-           optarg = "\\0";
-         delim_arg = optarg;
+         delim_arg = (optarg[0] == '\0' ? "\\0" : optarg);
          break;

        case 's':
diff --git a/src/pr.c b/src/pr.c
index 0967588..bd694f4 100644
--- a/src/pr.c
+++ b/src/pr.c
@@ -430,7 +430,7 @@ static void getoptarg (char *arg, char s
 void usage (int status);
 static void print_files (int number_of_files, char **av);
 static void init_parameters (int number_of_files);
-static void init_header (char *filename, int desc);
+static void init_header (char const *filename, int desc);
 static bool init_fps (int number_of_files, char **av);
 static void init_funcs (void);
 static void init_store_cols (void);
@@ -1653,7 +1653,7 @@ print_files (int number_of_files, char *
    FILENAME for reading.  */

 static void
-init_header (char *filename, int desc)
+init_header (char const *filename, int desc)
 {
   char *buf = NULL;
   struct stat st;
diff --git a/src/printf.c b/src/printf.c
index bfb4072..43fe5ba 100644
--- a/src/printf.c
+++ b/src/printf.c
@@ -76,7 +76,7 @@ static bool posixly_correct;

 /* This message appears in N_() here rather than just in _() below because
    the sole use would have been in a #define.  */
-static char *const cfcc_msg =
+static char const *const cfcc_msg =
  N_("warning: %s: character(s) following character constant have been 
ignored");

 /* The name this program was run with. */
diff --git a/src/tac.c b/src/tac.c
index b3777b2..dc166aa 100644
--- a/src/tac.c
+++ b/src/tac.c
@@ -78,7 +78,7 @@ #define WRITESIZE 8192
 char *program_name;

 /* The string that separates the records of the file. */
-static char *separator;
+static char const *separator;

 /* True if we have ever read standard input.  */
 static bool have_read_stdin = false;
@@ -211,7 +211,7 @@ tac_seekable (int input_fd, const char *
      Only used when the separator is attached to the preceding record. */
   bool first_time = true;
   char first_char = *separator;        /* Speed optimization, non-regexp. */
-  char *separator1 = separator + 1; /* Speed optimization, non-regexp. */
+  char const *separator1 = separator + 1; /* Speed optimization, non-regexp. */
   size_t match_length1 = match_length - 1; /* Speed optimization, non-regexp. 
*/
   struct re_registers regs;

@@ -426,7 +426,7 @@ static bool
 copy_to_temp (FILE **g_tmp, char **g_tempfile, int input_fd, char const *file)
 {
   static char *template = NULL;
-  static char *tempdir;
+  static char const *tempdir;
   char *tempfile;
   FILE *tmp;
   int fd;
diff --git a/src/tail.c b/src/tail.c
index 7d8f421..d17f40a 100644
--- a/src/tail.c
+++ b/src/tail.c
@@ -292,7 +292,7 @@ valid_file_spec (struct File_spec const
   return ((f->fd == -1) ^ (f->errnum == 0));
 }

-static char *
+static char const *
 pretty_name (struct File_spec const *f)
 {
   return (STREQ (f->name, "-") ? "standard input" : f->name);
diff --git a/src/tr.c b/src/tr.c
index e18e558..214eb2b 100644
--- a/src/tr.c
+++ b/src/tr.c
@@ -601,7 +601,7 @@ make_printable_str (char const *s, size_
   for (i = 0; i < len; i++)
     {
       char buf[5];
-      char *tmp = NULL;
+      char const *tmp = NULL;
       unsigned char c = s[i];

       switch (c)




reply via email to

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