[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH x 5] warnings, some legit
From: |
Jim Meyering |
Subject: |
[PATCH x 5] warnings, some legit |
Date: |
Wed, 15 Oct 2008 18:54:24 +0200 |
Here's a 5-patch series from a week or so ago.
It started out with the aim to avoid some warnings.
Some of those warnings were legitimate.
At first I was tempted to simply ignore any freopen failure,
thinking that it was so unlikely as to be truly ignorable.
And in addition, most are guarded by "if (O_BINARY &&...",
so wouldn't even be compiled on systems I care about.
However, there are actually many ways in which freopen can
fail, so I wrote xfreopen and used that everywhere.
Feedback welcome.
I'll wait a day or two, just in case...
>From 1d50f90dc3ad9be649b6a13b35ecb32a863bbca6 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Wed, 8 Oct 2008 10:44:12 +0200
Subject: [PATCH] maint: avoid warnings due to attribute warn_unused_result
Now that a (void) cast no longer suffices to ignore warnings from gcc
about uses of functions marked with the warn_unused_result attribute,
we need an alternative. For the record, here's one of the ignorable
warnings: "copy.c:233: warning: ignoring return value of 'fchown',
declared with attribute warn_unused_result"
* src/copy.h (ignore_value): New static inline function.
* src/copy.c (set_owner): Use it in place of "(void)" casts,
to ignore lchown and fchown failures.
* src/cp.c (re_protect): Use it to ignore lchown failure.
* src/remove.c (preprocess_dir): Remove unnecessary "(void)" cast.
---
src/copy.c | 4 ++--
src/copy.h | 6 ++++++
src/cp.c | 2 +-
src/remove.c | 2 +-
4 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/src/copy.c b/src/copy.c
index 82c6978..1aeec15 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -230,7 +230,7 @@ set_owner (const struct cp_options *x, char const
*dst_name, int dest_desc,
/* We've failed to set *both*. Now, try to set just the group
ID, but ignore any failure here, and don't change errno. */
int saved_errno = errno;
- (void) fchown (dest_desc, -1, gid);
+ ignore_value (fchown (dest_desc, -1, gid));
errno = saved_errno;
}
}
@@ -243,7 +243,7 @@ set_owner (const struct cp_options *x, char const
*dst_name, int dest_desc,
/* We've failed to set *both*. Now, try to set just the group
ID, but ignore any failure here, and don't change errno. */
int saved_errno = errno;
- (void) lchown (dst_name, -1, gid);
+ ignore_value (lchown (dst_name, -1, gid));
errno = saved_errno;
}
}
diff --git a/src/copy.h b/src/copy.h
index 86a8161..f48f38c 100644
--- a/src/copy.h
+++ b/src/copy.h
@@ -221,6 +221,12 @@ struct cp_options
? lstat (Src_name, Src_sb) \
: stat (Src_name, Src_sb))
+static inline void
+ignore_value (int i ATTRIBUTE_UNUSED)
+{
+ /* empty */
+}
+
/* Arrange to make rename calls go through the wrapper function
on systems with a rename function that fails for a source file name
specified with a trailing slash. */
diff --git a/src/cp.c b/src/cp.c
index 77d4702..d93ee02 100644
--- a/src/cp.c
+++ b/src/cp.c
@@ -326,7 +326,7 @@ re_protect (char const *const_dst_name, size_t src_offset,
}
/* Failing to preserve ownership is OK. Still, try to preserve
the group, but ignore the possible error. */
- (void) lchown (dst_name, -1, p->st.st_gid);
+ ignore_value (lchown (dst_name, -1, p->st.st_gid));
}
}
diff --git a/src/remove.c b/src/remove.c
index 2e342c0..e92f78c 100644
--- a/src/remove.c
+++ b/src/remove.c
@@ -1423,7 +1423,7 @@ preprocess_dir (DIR **dirp, struct rm_options const *x)
for (size_t i = 0; i < n; i++)
{
/* ignore failure */
- (void) unlinkat (dir_fd, vv[i]->name, 0);
+ unlinkat (dir_fd, vv[i]->name, 0);
}
cleanup:
--
1.6.0.2.98.gc82e
>From 74fa1e8bd522e275a03d9b17d0a8d676ad898f1a Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Wed, 8 Oct 2008 10:59:32 +0200
Subject: [PATCH] mktemp: avoid compiler warning about discarding "const"
* src/mktemp.c (TMP): Define: (char *) "/tmp".
(main): Use it in place of "/tmp".
---
src/mktemp.c | 6 ++++--
1 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/src/mktemp.c b/src/mktemp.c
index 0555d41..295d134 100644
--- a/src/mktemp.c
+++ b/src/mktemp.c
@@ -33,6 +33,8 @@
#define AUTHORS proper_name ("Jim Meyering")
+#define TMP (char *) "/tmp"
+
static const char *default_template = "tmp.XXXXXXXXXX";
/* For long options that have no equivalent short option, use a
@@ -219,7 +221,7 @@ main (int argc, char **argv)
char *env = getenv ("TMPDIR");
dest_dir = (env && *env
? env
- : (dest_dir_arg ? dest_dir_arg : "/tmp"));
+ : (dest_dir_arg ? dest_dir_arg : TMP));
if (last_component (template) != template)
error (EXIT_FAILURE, 0,
@@ -233,7 +235,7 @@ main (int argc, char **argv)
else
{
char *env = getenv ("TMPDIR");
- dest_dir = (env && *env ? env : "/tmp");
+ dest_dir = (env && *env ? env : TMP);
}
if (IS_ABSOLUTE_FILE_NAME (template))
error (EXIT_FAILURE, 0,
--
1.6.0.2.98.gc82e
>From 6fc767036a513dad05c43cca2253e7f0a1c3cde0 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Sun, 12 Oct 2008 16:09:00 +0200
Subject: [PATCH] mktemp: diagnose freopen failure
* src/mktemp [--quiet]: Don't ignore freopen failure.
---
src/mktemp.c | 4 +++-
1 files changed, 3 insertions(+), 1 deletions(-)
diff --git a/src/mktemp.c b/src/mktemp.c
index 295d134..8998f5c 100644
--- a/src/mktemp.c
+++ b/src/mktemp.c
@@ -190,7 +190,9 @@ main (int argc, char **argv)
{
/* From here on, redirect stderr to /dev/null.
A diagnostic from getopt_long, above, would still go to stderr. */
- freopen ("/dev/null", "wb", stderr);
+ if (!freopen ("/dev/null", "wb", stderr))
+ error (EXIT_FAILURE, errno,
+ _("failed to redirect stderr to /dev/null"));
}
n_args = argc - optind;
--
1.6.0.2.98.gc82e
>From 54eb9a3753710b8701cfeb7e1d19ea0fbc3d6281 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Sun, 12 Oct 2008 15:36:28 +0200
Subject: [PATCH] xfreopen: new module
* gl/lib/xfreopen.c: New file.
* gl/lib/xfreopen.h: New file.
* gl/modules/xfreopen: New file.
---
gl/lib/xfreopen.c | 33 +++++++++++++++++++++++++++++++++
gl/lib/xfreopen.h | 2 ++
gl/modules/xfreopen | 25 +++++++++++++++++++++++++
3 files changed, 60 insertions(+), 0 deletions(-)
create mode 100644 gl/lib/xfreopen.c
create mode 100644 gl/lib/xfreopen.h
create mode 100644 gl/modules/xfreopen
diff --git a/gl/lib/xfreopen.c b/gl/lib/xfreopen.c
new file mode 100644
index 0000000..ee37149
--- /dev/null
+++ b/gl/lib/xfreopen.c
@@ -0,0 +1,33 @@
+/* a wrapper for frepoen
+ Copyright (C) 2008 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
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+#include "xfreopen.h"
+
+#include <errno.h>
+#include "error.h"
+#include "exitfail.h"
+#include "quote.h"
+
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+
+void
+xfreopen (char const *filename, char const *mode, FILE *fp)
+{
+ if (!freopen (filename, mode, fp))
+ error (exit_failure, errno, _("failed to reopen %s"), quote (filename));
+}
diff --git a/gl/lib/xfreopen.h b/gl/lib/xfreopen.h
new file mode 100644
index 0000000..2ce49b5
--- /dev/null
+++ b/gl/lib/xfreopen.h
@@ -0,0 +1,2 @@
+#include <stdio.h>
+void xfreopen (char const *filename, char const *mode, FILE *fp);
diff --git a/gl/modules/xfreopen b/gl/modules/xfreopen
new file mode 100644
index 0000000..411f80b
--- /dev/null
+++ b/gl/modules/xfreopen
@@ -0,0 +1,25 @@
+Description:
+a wrapper for frepoen
+
+Files:
+lib/xfreopen.c
+lib/xfreopen.h
+
+Depends-on:
+error
+exitfail
+quote
+
+configure.ac:
+
+Makefile.am:
+lib_SOURCES += xfreopen.c xfreopen.h
+
+Include:
+"xfreopen.h"
+
+License:
+LGPL
+
+Maintainer:
+Jim Meyering
--
1.6.0.2.98.gc82e
>From 8f7884154771b0ba8524a612d0308eec0daecfd3 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Sun, 12 Oct 2008 14:50:02 +0200
Subject: [PATCH] use xfreopen in place of unchecked freopen
* bootstrap.conf (modules): Add xfreopen.
* src/cat.c (main): Include "xfreopen.h". Use xfreopen.
* src/cksum.c (cksum): Likewise.
* src/head.c (head_file, main): Likewise.
* src/md5sum.c (digest_file): Likewise.
* src/od.c (open_next_file): Likewise.
* src/split.c (type_undef): Likewise.
* src/sum.c (bsd_sum_file, sysv_sum_file): Likewise.
* src/tac.c (tac_file, main): Likewise.
* src/tail.c (tail_file, main): Likewise.
* src/tee.c (tee_files): Likewise.
* src/tr.c (main): Likewise.
* src/wc.c (wc_file): Likewise.
* src/ls.c (has_capability) [!HAVE_CAP]: Mark parameter as unused.
* po/POTFILES.in: Add lib/xfreopen.c
---
bootstrap.conf | 4 +++-
po/POTFILES.in | 1 +
src/cat.c | 5 +++--
src/cksum.c | 3 ++-
src/head.c | 5 +++--
src/ls.c | 2 +-
src/md5sum.c | 3 ++-
src/od.c | 3 ++-
src/split.c | 3 ++-
src/sum.c | 5 +++--
src/tac.c | 5 +++--
src/tail.c | 5 +++--
src/tee.c | 5 +++--
src/tr.c | 5 +++--
src/wc.c | 3 ++-
15 files changed, 36 insertions(+), 21 deletions(-)
diff --git a/bootstrap.conf b/bootstrap.conf
index b3eec48..8337b08 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -97,7 +97,9 @@ gnulib_modules="
vc-list-files
verify version-etc-fsf
wcwidth winsz-ioctl winsz-termios write-any-file
- xalloc xgetcwd xgethostname
+ xalloc
+ xfreopen
+ xgetcwd xgethostname
xmemcoll xnanosleep
xprintf
xprintf-posix
diff --git a/po/POTFILES.in b/po/POTFILES.in
index c8e97d6..8a6e541 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -24,6 +24,7 @@ lib/unicodeio.c
lib/userspec.c
lib/version-etc.c
lib/xalloc-die.c
+lib/xfreopen.c
lib/xfts.c
lib/xmemcoll.c
lib/xmemxfrm.c
diff --git a/src/cat.c b/src/cat.c
index 9c1996a..543e5cf 100644
--- a/src/cat.c
+++ b/src/cat.c
@@ -39,6 +39,7 @@
#include "full-write.h"
#include "quote.h"
#include "safe-read.h"
+#include "xfreopen.h"
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "cat"
@@ -664,7 +665,7 @@ main (int argc, char **argv)
{
file_open_mode |= O_BINARY;
if (O_BINARY && ! isatty (STDOUT_FILENO))
- freopen (NULL, "wb", stdout);
+ xfreopen (NULL, "wb", stdout);
}
/* Check if any of the input files are the same as the output file. */
@@ -684,7 +685,7 @@ main (int argc, char **argv)
have_read_stdin = true;
input_desc = STDIN_FILENO;
if ((file_open_mode & O_BINARY) && ! isatty (STDIN_FILENO))
- freopen (NULL, "rb", stdin);
+ xfreopen (NULL, "rb", stdin);
}
else
{
diff --git a/src/cksum.c b/src/cksum.c
index 8bbed37..2893d30 100644
--- a/src/cksum.c
+++ b/src/cksum.c
@@ -43,6 +43,7 @@
#include <sys/types.h>
#include <stdint.h>
#include "system.h"
+#include "xfreopen.h"
#ifdef CRCTAB
@@ -192,7 +193,7 @@ cksum (const char *file, bool print_name)
fp = stdin;
have_read_stdin = true;
if (O_BINARY && ! isatty (STDIN_FILENO))
- freopen (NULL, "rb", stdin);
+ xfreopen (NULL, "rb", stdin);
}
else
{
diff --git a/src/head.c b/src/head.c
index 0ef912a..7f90d4b 100644
--- a/src/head.c
+++ b/src/head.c
@@ -36,6 +36,7 @@
#include "full-read.h"
#include "quote.h"
#include "safe-read.h"
+#include "xfreopen.h"
#include "xstrtol.h"
/* The official name of this program (e.g., no `g' prefix). */
@@ -838,7 +839,7 @@ head_file (const char *filename, uintmax_t n_units, bool
count_lines,
fd = STDIN_FILENO;
filename = _("standard input");
if (O_BINARY && ! isatty (STDIN_FILENO))
- freopen (NULL, "rb", stdin);
+ xfreopen (NULL, "rb", stdin);
}
else
{
@@ -1051,7 +1052,7 @@ main (int argc, char **argv)
: default_file_list);
if (O_BINARY && ! isatty (STDOUT_FILENO))
- freopen (NULL, "wb", stdout);
+ xfreopen (NULL, "wb", stdout);
for (i = 0; file_list[i]; ++i)
ok &= head_file (file_list[i], n_units, count_lines, elide_from_end);
diff --git a/src/ls.c b/src/ls.c
index ea35b17..efe42e3 100644
--- a/src/ls.c
+++ b/src/ls.c
@@ -3944,7 +3944,7 @@ has_capability (char const *name)
}
#else
static bool
-has_capability (char const *name)
+has_capability (char const *name ATTRIBUTE_UNUSED)
{
return false;
}
diff --git a/src/md5sum.c b/src/md5sum.c
index 238c02e..d31a780 100644
--- a/src/md5sum.c
+++ b/src/md5sum.c
@@ -37,6 +37,7 @@
#endif
#include "error.h"
#include "stdio--.h"
+#include "xfreopen.h"
/* The official name of this program (e.g., no `g' prefix). */
#if HASH_ALGO_MD5
@@ -391,7 +392,7 @@ digest_file (const char *filename, int *binary, unsigned
char *bin_result)
if (*binary < 0)
*binary = ! isatty (STDIN_FILENO);
if (*binary)
- freopen (NULL, "rb", stdin);
+ xfreopen (NULL, "rb", stdin);
}
}
else
diff --git a/src/od.c b/src/od.c
index ed3b50a..2dcb398 100644
--- a/src/od.c
+++ b/src/od.c
@@ -25,6 +25,7 @@
#include "system.h"
#include "error.h"
#include "quote.h"
+#include "xfreopen.h"
#include "xprintf.h"
#include "xstrtol.h"
@@ -864,7 +865,7 @@ open_next_file (void)
in_stream = stdin;
have_read_stdin = true;
if (O_BINARY && ! isatty (STDIN_FILENO))
- freopen (NULL, "rb", stdin);
+ xfreopen (NULL, "rb", stdin);
}
else
{
diff --git a/src/split.c b/src/split.c
index 85687c4..be182ef 100644
--- a/src/split.c
+++ b/src/split.c
@@ -34,6 +34,7 @@
#include "full-write.h"
#include "quote.h"
#include "safe-read.h"
+#include "xfreopen.h"
#include "xstrtol.h"
/* The official name of this program (e.g., no `g' prefix). */
@@ -544,7 +545,7 @@ main (int argc, char **argv)
/* Binary I/O is safer when bytecounts are used. */
if (O_BINARY && ! isatty (STDIN_FILENO))
- freopen (NULL, "rb", stdin);
+ xfreopen (NULL, "rb", stdin);
/* No output file is open now. */
output_desc = -1;
diff --git a/src/sum.c b/src/sum.c
index 314656f..4cb8f31 100644
--- a/src/sum.c
+++ b/src/sum.c
@@ -28,6 +28,7 @@
#include "error.h"
#include "human.h"
#include "safe-read.h"
+#include "xfreopen.h"
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "sum"
@@ -97,7 +98,7 @@ bsd_sum_file (const char *file, int print_name)
fp = stdin;
have_read_stdin = true;
if (O_BINARY && ! isatty (STDIN_FILENO))
- freopen (NULL, "rb", stdin);
+ xfreopen (NULL, "rb", stdin);
}
else
{
@@ -165,7 +166,7 @@ sysv_sum_file (const char *file, int print_name)
fd = STDIN_FILENO;
have_read_stdin = true;
if (O_BINARY && ! isatty (STDIN_FILENO))
- freopen (NULL, "rb", stdin);
+ xfreopen (NULL, "rb", stdin);
}
else
{
diff --git a/src/tac.c b/src/tac.c
index c83986f..be8f3ab 100644
--- a/src/tac.c
+++ b/src/tac.c
@@ -48,6 +48,7 @@ tac -r -s '.\|
#include "quotearg.h"
#include "safe-read.h"
#include "stdlib--.h"
+#include "xfreopen.h"
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "tac"
@@ -533,7 +534,7 @@ tac_file (const char *filename)
fd = STDIN_FILENO;
filename = _("standard input");
if (O_BINARY && ! isatty (STDIN_FILENO))
- freopen (NULL, "rb", stdin);
+ xfreopen (NULL, "rb", stdin);
}
else
{
@@ -647,7 +648,7 @@ main (int argc, char **argv)
: default_file_list);
if (O_BINARY && ! isatty (STDOUT_FILENO))
- freopen (NULL, "wb", stdout);
+ xfreopen (NULL, "wb", stdout);
{
size_t i;
diff --git a/src/tail.c b/src/tail.c
index 43fd6d4..6ad6d43 100644
--- a/src/tail.c
+++ b/src/tail.c
@@ -40,6 +40,7 @@
#include "quote.h"
#include "safe-read.h"
#include "stat-time.h"
+#include "xfreopen.h"
#include "xnanosleep.h"
#include "xstrtol.h"
#include "xstrtod.h"
@@ -1273,7 +1274,7 @@ tail_file (struct File_spec *f, uintmax_t n_units)
have_read_stdin = true;
fd = STDIN_FILENO;
if (O_BINARY && ! isatty (STDIN_FILENO))
- freopen (NULL, "rb", stdin);
+ xfreopen (NULL, "rb", stdin);
}
else
fd = open (f->name, O_RDONLY | O_BINARY);
@@ -1683,7 +1684,7 @@ main (int argc, char **argv)
print_headers = true;
if (O_BINARY && ! isatty (STDOUT_FILENO))
- freopen (NULL, "wb", stdout);
+ xfreopen (NULL, "wb", stdout);
for (i = 0; i < n_files; i++)
ok &= tail_file (&F[i], n_units);
diff --git a/src/tee.c b/src/tee.c
index 4e46aab..2d13f6d 100644
--- a/src/tee.c
+++ b/src/tee.c
@@ -24,6 +24,7 @@
#include "system.h"
#include "error.h"
#include "stdio--.h"
+#include "xfreopen.h"
/* The official name of this program (e.g., no `g' prefix). */
#define PROGRAM_NAME "tee"
@@ -152,9 +153,9 @@ tee_files (int nfiles, const char **files)
files[i] = files[i - 1];
if (O_BINARY && ! isatty (STDIN_FILENO))
- freopen (NULL, "rb", stdin);
+ xfreopen (NULL, "rb", stdin);
if (O_BINARY && ! isatty (STDOUT_FILENO))
- freopen (NULL, "wb", stdout);
+ xfreopen (NULL, "wb", stdout);
/* In the array of NFILES + 1 descriptors, make
the first one correspond to standard output. */
diff --git a/src/tr.c b/src/tr.c
index 0f94eef..f4b5317 100644
--- a/src/tr.c
+++ b/src/tr.c
@@ -27,6 +27,7 @@
#include "error.h"
#include "quote.h"
#include "safe-read.h"
+#include "xfreopen.h"
#include "xstrtol.h"
/* The official name of this program (e.g., no `g' prefix). */
@@ -1750,9 +1751,9 @@ main (int argc, char **argv)
non-printable characters, or characters which are stripped away
by text-mode reads (like CR and ^Z). */
if (O_BINARY && ! isatty (STDIN_FILENO))
- freopen (NULL, "rb", stdin);
+ xfreopen (NULL, "rb", stdin);
if (O_BINARY && ! isatty (STDOUT_FILENO))
- freopen (NULL, "wb", stdout);
+ xfreopen (NULL, "wb", stdout);
if (squeeze_repeats && non_option_args == 1)
{
diff --git a/src/wc.c b/src/wc.c
index 0bb1929..280d7ac 100644
--- a/src/wc.c
+++ b/src/wc.c
@@ -32,6 +32,7 @@
#include "quotearg.h"
#include "readtokens0.h"
#include "safe-read.h"
+#include "xfreopen.h"
#if !defined iswspace && !HAVE_ISWSPACE
# define iswspace(wc) \
@@ -486,7 +487,7 @@ wc_file (char const *file, struct fstatus *fstatus)
{
have_read_stdin = true;
if (O_BINARY && ! isatty (STDIN_FILENO))
- freopen (NULL, "rb", stdin);
+ xfreopen (NULL, "rb", stdin);
return wc (STDIN_FILENO, file, fstatus);
}
else
--
1.6.0.2.98.gc82e
- [PATCH x 5] warnings, some legit,
Jim Meyering <=