Paul Eggert wrote:
> I resurrected and refreshed that old Coreutils code and put it into a new
> Gnulib
> module stdopen
I would like to use this modules in GNU gettext and GNU libiconv. But I'm
encountering two issues:
1) A compilation failure with IRIX cc:
cc -n32 -DHAVE_CONFIG_H -I. -I../../gllib -I.. -DGNULIB_STRICT_CHECKING=1
-I/u/guest/bruno/prefix-n32-cc/include -g -c -o stdopen.o ../../gllib/stdopen.c
cc-1029 cc: ERROR File = ../../gllib/stdopen.c, Line = 39
An expression is expected at this point.
for (int fd = STDIN_FILENO; fd <= STDERR_FILENO; fd++)
^
cc-1020 cc: ERROR File = ../../gllib/stdopen.c, Line = 39
The identifier "fd" is undefined.
for (int fd = STDIN_FILENO; fd <= STDERR_FILENO; fd++)
^
2 errors detected in the compilation of "../../gllib/stdopen.c".
2) I don't want to repeat the error message code in each and every
program. So, I put that into a new module 'xstdopen'.
Done through the attached commits.
>From 3e35b2e771b051ae6d205a3bf95bd67543b2458c Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Sun, 6 Jan 2019 09:24:04 +0100
Subject: [PATCH 1/3] stdopen: Fix compilation error with IRIX cc.
* lib/stdopen.c (stdopen): Do not use C99-style decl in loop.
---
ChangeLog | 5 +++++
lib/stdopen.c | 3 ++-
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/ChangeLog b/ChangeLog
index 73a86f2..db51bd8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2019-01-06 Bruno Haible <[email protected]>
+
+ stdopen: Fix compilation error with IRIX cc.
+ * lib/stdopen.c (stdopen): Do not use C99-style decl in loop.
+
2019-01-05 Paul Eggert <[email protected]>
xfreopen need not include stdio--.h
diff --git a/lib/stdopen.c b/lib/stdopen.c
index 3903224..29350d1 100644
--- a/lib/stdopen.c
+++ b/lib/stdopen.c
@@ -36,7 +36,8 @@
int
stdopen (void)
{
- for (int fd = STDIN_FILENO; fd <= STDERR_FILENO; fd++)
+ int fd;
+ for (fd = STDIN_FILENO; fd <= STDERR_FILENO; fd++)
{
if (fcntl (fd, F_GETFD) < 0)
{
--
2.7.4
>From aca08ef1e45f2bbd1356ef190591e350dabfff0c Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Sun, 6 Jan 2019 09:25:59 +0100
Subject: [PATCH 2/3] xstdopen: New module.
* lib/xstdopen.h: New file.
* lib/xstdopen.c: New file.
* modules/xstdopen: New file.
---
ChangeLog | 7 +++++++
lib/xstdopen.c | 35 +++++++++++++++++++++++++++++++++++
lib/xstdopen.h | 28 ++++++++++++++++++++++++++++
modules/xstdopen | 26 ++++++++++++++++++++++++++
4 files changed, 96 insertions(+)
create mode 100644 lib/xstdopen.c
create mode 100644 lib/xstdopen.h
create mode 100644 modules/xstdopen
diff --git a/ChangeLog b/ChangeLog
index db51bd8..89e57e6 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,12 @@
2019-01-06 Bruno Haible <[email protected]>
+ xstdopen: New module.
+ * lib/xstdopen.h: New file.
+ * lib/xstdopen.c: New file.
+ * modules/xstdopen: New file.
+
+2019-01-06 Bruno Haible <[email protected]>
+
stdopen: Fix compilation error with IRIX cc.
* lib/stdopen.c (stdopen): Do not use C99-style decl in loop.
diff --git a/lib/xstdopen.c b/lib/xstdopen.c
new file mode 100644
index 0000000..677a280
--- /dev/null
+++ b/lib/xstdopen.c
@@ -0,0 +1,35 @@
+/* Ensure that stdin, stdout, stderr are open.
+ Copyright (C) 2019 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 <https://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+/* Specification. */
+#include "xstdopen.h"
+
+#include "stdopen.h"
+#include "error.h"
+#include "exitfail.h"
+
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+
+void
+xstdopen (void)
+{
+ int stdopen_errno = stdopen ();
+ if (stdopen_errno != 0)
+ error (exit_failure, stdopen_errno, _("standard file descriptors"));
+}
diff --git a/lib/xstdopen.h b/lib/xstdopen.h
new file mode 100644
index 0000000..08a8a04
--- /dev/null
+++ b/lib/xstdopen.h
@@ -0,0 +1,28 @@
+/* Ensure that stdin, stdout, stderr are open.
+ Copyright (C) 2019 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 <https://www.gnu.org/licenses/>. */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Ensures that the file descriptors of stdin, stdout, stderr (0, 1, 2) are
+ open. Exits the program with an error message upon failure; the error
+ message may not appear if stderr is closed. */
+extern void xstdopen (void);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/modules/xstdopen b/modules/xstdopen
new file mode 100644
index 0000000..cd9e570
--- /dev/null
+++ b/modules/xstdopen
@@ -0,0 +1,26 @@
+Description:
+Ensure that stdin, stdout, stderr are open.
+
+Files:
+lib/xstdopen.h
+lib/xstdopen.c
+
+Depends-on:
+stdopen
+error
+gettext-h
+exitfail
+
+configure.ac:
+
+Makefile.am:
+lib_SOURCES += xstdopen.c
+
+Include:
+"xstdopen.h"
+
+License:
+GPL
+
+Maintainer:
+all
--
2.7.4
>From 7687815c173602e485c86ab73da09e1b583c9232 Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Sun, 6 Jan 2019 09:27:42 +0100
Subject: [PATCH 3/3] xstdopen: Add tests.
* tests/test-xstdopen.c: New file.
* tests/test-xstdopen.sh: New file.
* modules/xstdopen-tests: New file.
---
ChangeLog | 5 +++++
modules/xstdopen-tests | 12 ++++++++++++
tests/test-xstdopen.c | 32 ++++++++++++++++++++++++++++++++
tests/test-xstdopen.sh | 20 ++++++++++++++++++++
4 files changed, 69 insertions(+)
create mode 100644 modules/xstdopen-tests
create mode 100644 tests/test-xstdopen.c
create mode 100755 tests/test-xstdopen.sh
diff --git a/ChangeLog b/ChangeLog
index 89e57e6..f9ecd2e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,10 @@
2019-01-06 Bruno Haible <[email protected]>
+ xstdopen: Add tests.
+ * tests/test-xstdopen.c: New file.
+ * tests/test-xstdopen.sh: New file.
+ * modules/xstdopen-tests: New file.
+
xstdopen: New module.
* lib/xstdopen.h: New file.
* lib/xstdopen.c: New file.
diff --git a/modules/xstdopen-tests b/modules/xstdopen-tests
new file mode 100644
index 0000000..0810f4a
--- /dev/null
+++ b/modules/xstdopen-tests
@@ -0,0 +1,12 @@
+Files:
+tests/test-xstdopen.c
+tests/test-xstdopen.sh
+tests/macros.h
+
+Depends-on:
+test-framework-sh
+
+Makefile.am:
+TESTS += test-xstdopen.sh
+check_PROGRAMS += test-xstdopen
+test_xstdopen_LDADD = $(LDADD) @LIBINTL@
diff --git a/tests/test-xstdopen.c b/tests/test-xstdopen.c
new file mode 100644
index 0000000..0bd4a43
--- /dev/null
+++ b/tests/test-xstdopen.c
@@ -0,0 +1,32 @@
+/* Test of xstdopen() function.
+ Copyright (C) 2019 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 <https://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#include "xstdopen.h"
+
+#include <fcntl.h>
+
+#include "macros.h"
+
+int
+main (void)
+{
+ xstdopen ();
+ ASSERT (open ("Makefile", O_RDONLY) >= 3);
+
+ return 0;
+}
diff --git a/tests/test-xstdopen.sh b/tests/test-xstdopen.sh
new file mode 100755
index 0000000..5ad2ad2
--- /dev/null
+++ b/tests/test-xstdopen.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+# Test with all of stdin, stdout, stderr open.
+./test-xstdopen || exit 1
+
+# The syntax for closed file descriptors in sh scripts is specified by POSIX in
+# section 2.7.5 of
+# http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
+
+# Test with stdin closed.
+./test-xstdopen <&- || exit 1
+
+# Test with stdout closed.
+./test-xstdopen >&- || exit 1
+
+# Test with stderr closed.
+./test-xstdopen 2>&- || exit 1
+
+# Test with all of stdin, stdout, stderr closed.
+./test-xstdopen <&- >&- 2>&- || exit 1
--
2.7.4