bug-coreutils
[Top][All Lists]
Advanced

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

cleanup patch to isolate "safer" functions within coreutils


From: Paul Eggert
Subject: cleanup patch to isolate "safer" functions within coreutils
Date: Sun, 03 Jul 2005 00:35:52 -0700
User-agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux)

I installed this:

2005-07-02  Paul Eggert  <address@hidden>

        Cleanup to isolate "safer" functions to a small part of the code.
        * lib/fcntl--.h, lib/stdio--.h, lib/stdlib--.h, lib/unistd--.h:
        * lib/fcntl-safer.h, lib/open-safer.c, lib/stdlib-safer.h:
        * lib/mkstemp-safer.c, lib/fd-reopen.h, lib/fd-reopen.c: New files.
        * lib/fopen-safer.c: Include stdio-safer.h first, to check interface.
        Don't bother including stdio.h, since stdio-safer.h does.
        * lib/fts.c (fd_safer): Remove decl.
        Include fcntl--.h rather than unistd-safer.h
        (fts_safe_changedir): Don't call fd_safer; no longer needed
        now that we include fcntl--.h.
        * lib/getloadavg.c: Include fcntl--.h rather than fcntl.h.
        Do not include unistd-safer.h.
        (getloadavg): Don't call fd_safer; no longer needed
        now that we include fcntl--.h.
        * lib/getusershell.c: Include stdio--.h rather than stdio.h
        and stdio-safer.h.
        (getusershell): Call fopen, not fopen_safer.
        * lib/save-cwd.c: Include fcntl--.h rather than fcntl.h.
        Do not include unistd-safer.h.
        (save_cwd): Don't call fd_safer; no longer needed
        now that we include fcntl--.h.
        * m4/fcntl-safer.m4, m4/fd-reopen.m4, m4/stdlib-safer.m4: New files.
        * m4/prereq.m4 (gl_PREREQ): Require gl_FCNTL_SAFER,
        gl_FD_REOPEN, gl_STDLIB_SAFER.
        * src/comm.c: Include stdio--.h, not stdio-safer.h.
        (compare_files): Use fopen, not fopen_safer.
        * src/copy.c: Include fcntl--.h, not unistd-safer.h.
        (copy_reg): Don't call fd_safer; no longer needed
        now that we include fcntl--.h.
        * src/csplit.c: Include fd-reopen.h.
        Include stdio--.h, not stdio-safer.h.
        (input_desc): Remove.  All uses changed to STDIN_FILENO.
        (set_input_file): Reopen stdin, to simplify code.
        (create_output_file): Use fopen, not fopen_safer.
        * src/dd.c: Include fd-reopen.h.
        (open_fd): Remove.  All callers changed to use fd_reopen instead.
        * src/join.c: Include stdio--.h, not stdio-safer.h.
        (main): Use fopen, not fopen_safer.
        * src/md5sum.c: Include stdio--.h.
        (digest_check): Don't try to read both checksums and data from stdin.
        * src/nohup.c: Include fd-reopen.h.
        Include unistd--.h, not unistd-safer.h.
        (main): Use fd_reopen to simplify code.  When replacing stdin,
        use "/dev/null" not "/", as that's less likely to go wrong these days.
        (main): Use dup, not dup_safer.
        * src/pr.c: Include stdio--.h, not stdio-safer.h.
        (open_file): Invoke fopen, not fopen_safer.
        * src/shred.c: Include fcntl--.h, not unistd-safer.h.
        (wipename, wipe_file): Don't use fd_safer; no longer needed
        now that we include fcntl--.h.
        * src/sort.c: Include stdio--.h rather than stdio-safer.h.
        Include stdlib--.h.  Do not include unistd-safer.h.
        (create_temp_file): Don't call fd_safer; no longer needed
        now that we include *--.h files.
        (xfopen): Don't call fopen_safer, for similar reasons.
        * src/split.c: Include fcntl--.h rather than unistd-safer.h.
        Include fd-reopen.h.
        (input_desc): Remove.  All uses replaced by STDIN_FILENO.
        (cwrite): Don't call fd_safer; no longer needed now that
        we include fcntl--.h.
        (main): Reuse stdin rather than opening a new one.  This
        saves a file descriptor.
        * src/stty.c: Include fd-reopen.h.
        (display_all, display_settings, display_window_size, set_window_size):
        Remove fd arg, since we now assume stdin.  All callers changed.
        (main): Reuse stdin rather than opening a new one.  This
        saves a file descriptor.
        * src/tac.c: Include stdlib--.h rather than unistd-safer.h.
        (copy_to_temp): Don't call fd_safer; no longer needed now
        that we include stdlib--.h.
        * src/tail.c: Include fcntl--.h, not unistd-safer.h.
        (recheck, tail_file): Don't call fd_safer; no longer needed
        now that we include fcntl--.h.
        * src/tee.c: Include stdio--.h, not stdio-safer.h.
        (tee): Don't call fopen_safer; no longer needed now that we
        include stdio--.h.
        * src/touch.c: Include fcntl--.h, not unistd-safer.h.
        (touch): Don't call fd_safer; no longer needed now that
        we include fcntl--.h.

--- /dev/null   2005-06-27 15:40:05.000000000 -0700
+++ lib/fcntl--.h       2005-07-02 20:51:01.000000000 -0700
@@ -0,0 +1,25 @@
+/* Like fcntl.h, but redefine some names to avoid glitches.
+
+   Copyright (C) 2005 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 2, 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, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+/* Written by Paul Eggert.  */
+
+#include <fcntl.h>
+#include "fcntl-safer.h"
+
+#undef open
+#define open open_safer
--- /dev/null   2005-06-27 15:40:05.000000000 -0700
+++ lib/stdio--.h       2005-07-02 23:07:40.000000000 -0700
@@ -0,0 +1,25 @@
+/* Like stdio.h, but redefine some names to avoid glitches.
+
+   Copyright (C) 2005 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 2, 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, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+/* Written by Paul Eggert.  */
+
+#include <stdio.h>
+#include "stdio-safer.h"
+
+#undef fopen
+#define fopen fopen_safer
--- /dev/null   2005-06-27 15:40:05.000000000 -0700
+++ lib/stdlib--.h      2005-07-02 22:25:43.000000000 -0700
@@ -0,0 +1,25 @@
+/* Like stdlib.h, but redefine some names to avoid glitches.
+
+   Copyright (C) 2005 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 2, 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, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+/* Written by Paul Eggert.  */
+
+#include <stdlib.h>
+#include "stdlib-safer.h"
+
+#undef mkstemp
+#define mkstemp mkstemp_safer
--- /dev/null   2005-06-27 15:40:05.000000000 -0700
+++ lib/unistd--.h      2005-07-02 20:51:38.000000000 -0700
@@ -0,0 +1,25 @@
+/* Like unistd.h, but redefine some names to avoid glitches.
+
+   Copyright (C) 2005 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 2, 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, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+/* Written by Paul Eggert.  */
+
+#include <unistd.h>
+#include "unistd-safer.h"
+
+#undef dup
+#define dup dup_safer
--- /dev/null   2005-06-27 15:40:05.000000000 -0700
+++ lib/fcntl-safer.h   2005-07-02 20:03:55.000000000 -0700
@@ -0,0 +1,21 @@
+/* Invoke fcntl-like functions, but avoid some glitches.
+
+   Copyright (C) 2005 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 2, 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, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+/* Written by Paul Eggert.  */
+
+int open_safer (char const *, int, ...);
--- /dev/null   2005-06-27 15:40:05.000000000 -0700
+++ lib/open-safer.c    2005-07-02 22:56:52.000000000 -0700
@@ -0,0 +1,47 @@
+/* Invoke open, but avoid some glitches.
+   Copyright (C) 2005 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 2, 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, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+/* Written by Paul Eggert.  */
+
+#include "fcntl-safer.h"
+
+#include <fcntl.h>
+#include <stdarg.h>
+#include "unistd-safer.h"
+
+int
+open_safer (char const *file, int flags, ...)
+{
+  mode_t mode = 0;
+
+  if (flags & O_CREAT)
+    {
+      va_list ap;
+      va_start (ap, flags);
+
+      /* Assume mode_t promotes to int if and only if it is smaller.
+        This assumption isn't guaranteed by the C standard, but we
+        don't know of any real-world counterexamples.  */
+      mode = (sizeof (mode_t) < sizeof (int)
+             ? va_arg (ap, int)
+             : va_arg (ap, mode_t));
+
+      va_end (ap);
+    }
+
+  return fd_safer (open (file, flags, mode));
+}
--- /dev/null   2005-06-27 15:40:05.000000000 -0700
+++ lib/stdlib-safer.h  2005-07-02 12:50:43.000000000 -0700
@@ -0,0 +1,21 @@
+/* Invoke stdlib.h functions, but avoid some glitches.
+
+   Copyright (C) 2005 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 2, 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, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+/* Written by Paul Eggert.  */
+
+int mkstemp_safer (char *);
--- /dev/null   2005-06-27 15:40:05.000000000 -0700
+++ lib/mkstemp-safer.c 2005-07-02 22:56:25.000000000 -0700
@@ -0,0 +1,33 @@
+/* Invoke mkstemp, but avoid some glitches.
+
+   Copyright (C) 2005 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 2, 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, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+/* Written by Paul Eggert.  */
+
+#include "stdlib-safer.h"
+
+#include <stdlib.h>
+#include "unistd-safer.h"
+
+/* Like mkstemp, but do not return STDIN_FILENO, STDOUT_FILENO, or
+   STDERR_FILENO.  */
+
+int
+mkstemp_safer (char *template)
+{
+  return fd_safer (mkstemp (template));
+}
--- /dev/null   2005-06-27 15:40:05.000000000 -0700
+++ lib/fd-reopen.h     2005-07-02 21:49:27.000000000 -0700
@@ -0,0 +1,23 @@
+/* Invoke open, but return either a desired file descriptor or -1.
+
+   Copyright (C) 2005 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 2, 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, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+/* Written by Paul Eggert.  */
+
+#include <sys/types.h>
+
+int fd_reopen (int, char const *, int, mode_t);
--- /dev/null   2005-06-27 15:40:05.000000000 -0700
+++ lib/fd-reopen.c     2005-07-02 22:54:48.000000000 -0700
@@ -0,0 +1,46 @@
+/* Invoke open, but return either a desired file descriptor or -1.
+
+   Copyright (C) 2005 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 2, 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, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+/* Written by Paul Eggert.  */
+
+#include "fd-reopen.h"
+
+#include <errno.h>
+#include <fcntl.h>
+
+/* Open a file to a particular file descriptor.  This is like standard
+   `open', except it always returns DESIRED_FD if successful.  */
+
+int
+fd_reopen (int desired_fd, char const *file, int flags, mode_t mode)
+{
+  int fd;
+
+  close (desired_fd);
+  fd = open (file, flags, mode);
+  if (fd == desired_fd || fd < 0)
+    return fd;
+  else
+    {
+      int fd2 = fcntl (fd, F_DUPFD, desired_fd);
+      int saved_errno = errno;
+      close (fd);
+      errno = saved_errno;
+      return fd2;
+    }
+}
--- /dev/null   2005-06-27 15:40:05.000000000 -0700
+++ m4/fcntl-safer.m4   2005-07-02 22:40:47.000000000 -0700
@@ -0,0 +1,11 @@
+# fcntl-safer.m4 serial 1
+dnl Copyright (C) 2005 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_FCNTL_SAFER],
+[
+  AC_LIBSOURCES([open-safer.c, fcntl-safer.h])
+  AC_LIBOBJ([open-safer])
+])
--- /dev/null   2005-06-27 15:40:05.000000000 -0700
+++ m4/stdlib-safer.m4  2005-07-02 22:41:51.000000000 -0700
@@ -0,0 +1,11 @@
+# stdlib-safer.m4 serial 1
+dnl Copyright (C) 2005 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_STDLIB_SAFER],
+[
+  AC_LIBSOURCES([mkstemp-safer.c, stdlib-safer.h])
+  AC_LIBOBJ([mkstemp-safer])
+])
--- /dev/null   2005-06-27 15:40:05.000000000 -0700
+++ m4/fd-reopen.m4     2005-07-02 22:54:09.000000000 -0700
@@ -0,0 +1,15 @@
+# Invoke open, but return either a desired file descriptor or -1.
+
+dnl Copyright (C) 2005 Free Software Foundation, Inc.
+
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+dnl Written by Paul Eggert.
+
+AC_DEFUN([gl_FD_REOPEN],
+[
+  AC_LIBSOURCES([fd-reopen.c, fd-reopen.h])
+  AC_LIBOBJ([fd-reopen])
+])
Index: lib/fopen-safer.c
===================================================================
RCS file: /fetish/cu/lib/fopen-safer.c,v
retrieving revision 1.5
diff -p -u -r1.5 fopen-safer.c
--- lib/fopen-safer.c   2 Jul 2005 08:41:46 -0000       1.5
+++ lib/fopen-safer.c   3 Jul 2005 06:36:25 -0000
@@ -1,5 +1,5 @@
 /* Invoke fopen, but avoid some glitches.
-   Copyright (C) 2001, 2004 Free Software Foundation, Inc.
+   Copyright (C) 2001, 2004, 2005 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
@@ -21,17 +21,16 @@
 # include <config.h>
 #endif
 
+#include "stdio-safer.h"
+
+#include <errno.h>
 #include <unistd.h>
-#include <unistd-safer.h>
+#include "unistd-safer.h"
 
 #ifndef STDERR_FILENO
 # define STDERR_FILENO 2
 #endif
 
-#include <errno.h>
-#include <stdio.h>
-#include <stdio-safer.h>
-
 /* Like fopen, but do not return stdin, stdout, or stderr.  */
 
 FILE *
Index: lib/fts.c
===================================================================
RCS file: /fetish/cu/lib/fts.c,v
retrieving revision 1.34
diff -p -u -r1.34 fts.c
--- lib/fts.c   2 Jun 2005 05:05:29 -0000       1.34
+++ lib/fts.c   3 Jul 2005 06:36:26 -0000
@@ -152,10 +152,9 @@ static bool enter_dir (FTS *fts, FTSENT 
 static void leave_dir (FTS *fts, FTSENT *ent) {}
 static bool setup_dir (FTS *fts) { return true; }
 static void free_dir (FTS *fts) {}
-static int fd_safer (int fd) { return fd; }
 #else
+# include "fcntl--.h"
 # include "fts-cycle.c"
-# include "unistd-safer.h"
 #endif
 
 #ifndef MAX
@@ -1319,7 +1318,7 @@ fts_safe_changedir (FTS *sp, FTSENT *p, 
        newfd = fd;
        if (ISSET(FTS_NOCHDIR))
                return (0);
-       if (fd < 0 && (newfd = fd_safer (diropen (dir))) < 0)
+       if (fd < 0 && (newfd = diropen (dir)) < 0)
                return (-1);
        if (fstat(newfd, &sb)) {
                ret = -1;
Index: lib/getloadavg.c
===================================================================
RCS file: /fetish/cu/lib/getloadavg.c,v
retrieving revision 1.32
diff -p -u -r1.32 getloadavg.c
--- lib/getloadavg.c    2 Jul 2005 08:41:46 -0000       1.32
+++ lib/getloadavg.c    3 Jul 2005 06:36:26 -0000
@@ -445,9 +445,7 @@
 #  include <sys/dg_sys_info.h>
 # endif
 
-# include <fcntl.h>
-
-# include "unistd-safer.h"
+# include "fcntl--.h"
 
 /* Avoid static vars inside a function since in HPUX they dump as pure.  */
 
@@ -907,7 +905,7 @@ getloadavg (double loadavg[], int nelem)
   if (!getloadavg_initialized)
     {
 #  ifndef SUNOS_5
-      channel = fd_safer (open ("/dev/kmem", O_RDONLY));
+      channel = open ("/dev/kmem", O_RDONLY);
       if (channel >= 0)
        {
          /* Set the channel to close on exec, so it does not
Index: lib/getusershell.c
===================================================================
RCS file: /fetish/cu/lib/getusershell.c,v
retrieving revision 1.20
diff -p -u -r1.20 getusershell.c
--- lib/getusershell.c  14 May 2005 07:58:06 -0000      1.20
+++ lib/getusershell.c  3 Jul 2005 06:36:26 -0000
@@ -33,11 +33,10 @@
 # endif
 #endif
 
-#include <stdio.h>
 #include <stdlib.h>
 #include <ctype.h>
 
-#include "stdio-safer.h"
+#include "stdio--.h"
 #include "xalloc.h"
 
 #if USE_UNLOCKED_IO
@@ -99,7 +98,7 @@ getusershell (void)
 
   if (shellstream == NULL)
     {
-      shellstream = fopen_safer (SHELLS_FILE, "r");
+      shellstream = fopen (SHELLS_FILE, "r");
       if (shellstream == NULL)
        {
          /* No shells file.  Use the default list.  */
Index: lib/save-cwd.c
===================================================================
RCS file: /fetish/cu/lib/save-cwd.c,v
retrieving revision 1.29
diff -p -u -r1.29 save-cwd.c
--- lib/save-cwd.c      2 Jul 2005 08:41:46 -0000       1.29
+++ lib/save-cwd.c      3 Jul 2005 06:36:26 -0000
@@ -25,18 +25,14 @@
 
 #include "save-cwd.h"
 
+#include <errno.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
-
 #include <unistd.h>
 
-#include <fcntl.h>
-
-#include <errno.h>
-
 #include "chdir-long.h"
-#include "unistd-safer.h"
+#include "fcntl--.h"
 #include "xgetcwd.h"
 
 /* On systems without the fchdir function (WOE), pretend that open
@@ -76,10 +72,10 @@ save_cwd (struct saved_cwd *cwd)
 {
   cwd->name = NULL;
 
-  cwd->desc = fd_safer (open (".", O_RDONLY));
+  cwd->desc = open (".", O_RDONLY);
   if (cwd->desc < 0)
     {
-      cwd->desc = fd_safer (open (".", O_WRONLY));
+      cwd->desc = open (".", O_WRONLY);
       if (cwd->desc < 0)
        {
          cwd->name = xgetcwd ();
Index: m4/prereq.m4
===================================================================
RCS file: /fetish/cu/m4/prereq.m4,v
retrieving revision 1.114
diff -p -u -r1.114 prereq.m4
--- m4/prereq.m4        2 Jun 2005 05:08:51 -0000       1.114
+++ m4/prereq.m4        3 Jul 2005 06:36:26 -0000
@@ -1,4 +1,4 @@
-#serial 57
+#serial 58
 
 dnl We use gl_ for non Autoconf macros.
 m4_pattern_forbid([^gl_[ABCDEFGHIJKLMNOPQRSTUVXYZ]])dnl
@@ -46,6 +46,8 @@ AC_DEFUN([gl_PREREQ],
   AC_REQUIRE([gl_ERROR])
   AC_REQUIRE([gl_EXCLUDE])
   AC_REQUIRE([gl_EXITFAIL])
+  AC_REQUIRE([gl_FCNTL_SAFER])
+  AC_REQUIRE([gl_FD_REOPEN])
   AC_REQUIRE([gl_FILEBLOCKS])
   AC_REQUIRE([gl_FILEMODE])
   AC_REQUIRE([gl_FILE_NAME_CONCAT])
@@ -130,6 +132,7 @@ AC_DEFUN([gl_PREREQ],
   AC_REQUIRE([gl_SHA1])
   AC_REQUIRE([gl_STAT_MACROS])
   AC_REQUIRE([gl_STDIO_SAFER])
+  AC_REQUIRE([gl_STDLIB_SAFER])
   AC_REQUIRE([gl_STRCASE])
   AC_REQUIRE([gl_STRINTCMP])
   AC_REQUIRE([gl_STRNUMCMP])
Index: src/comm.c
===================================================================
RCS file: /fetish/cu/src/comm.c,v
retrieving revision 1.83
diff -p -u -r1.83 comm.c
--- src/comm.c  14 May 2005 07:58:36 -0000      1.83
+++ src/comm.c  3 Jul 2005 06:36:26 -0000
@@ -27,7 +27,7 @@
 #include "error.h"
 #include "hard-locale.h"
 #include "quote.h"
-#include "stdio-safer.h"
+#include "stdio--.h"
 #include "xmemcoll.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
@@ -160,9 +160,7 @@ compare_files (char **infiles)
     {
       initbuffer (&lb1[i]);
       thisline[i] = &lb1[i];
-      streams[i] = (STREQ (infiles[i], "-")
-                   ? stdin
-                   : fopen_safer (infiles[i], "r"));
+      streams[i] = (STREQ (infiles[i], "-") ? stdin : fopen (infiles[i], "r"));
       if (!streams[i])
        error (EXIT_FAILURE, errno, "%s", infiles[i]);
 
Index: src/copy.c
===================================================================
RCS file: /fetish/cu/src/copy.c,v
retrieving revision 1.185
diff -p -u -r1.185 copy.c
--- src/copy.c  2 Jun 2005 05:17:24 -0000       1.185
+++ src/copy.c  3 Jul 2005 06:36:26 -0000
@@ -36,6 +36,7 @@
 #include "dirname.h"
 #include "euidaccess.h"
 #include "error.h"
+#include "fcntl--.h"
 #include "filenamecat.h"
 #include "full-write.h"
 #include "getpagesize.h"
@@ -44,7 +45,6 @@
 #include "quote.h"
 #include "same.h"
 #include "savedir.h"
-#include "unistd-safer.h"
 #include "utimecmp.h"
 #include "utimens.h"
 #include "xreadlink.h"
@@ -213,7 +213,6 @@ copy_reg (char const *src_name, char con
   bool make_holes = false;
 
   source_desc = open (src_name, O_RDONLY);
-  source_desc = fd_safer (source_desc);
   if (source_desc < 0)
     {
       error (0, errno, _("cannot open %s for reading"), quote (src_name));
@@ -265,7 +264,6 @@ copy_reg (char const *src_name, char con
        }
     }
 
-  dest_desc = fd_safer (dest_desc);
   if (dest_desc < 0)
     {
       error (0, errno, _("cannot create regular file %s"), quote (dst_name));
Index: src/csplit.c
===================================================================
RCS file: /fetish/cu/src/csplit.c,v
retrieving revision 1.140
diff -p -u -r1.140 csplit.c
--- src/csplit.c        16 Jun 2005 21:33:43 -0000      1.140
+++ src/csplit.c        3 Jul 2005 06:36:26 -0000
@@ -30,10 +30,11 @@
 #include <regex.h>
 
 #include "error.h"
+#include "fd-reopen.h"
 #include "inttostr.h"
 #include "quote.h"
 #include "safe-read.h"
-#include "stdio-safer.h"
+#include "stdio--.h"
 #include "xstrtol.h"
 
 /* Use SA_NOCLDSTOP as a proxy for whether the sigaction machinery is
@@ -130,9 +131,6 @@ void usage (int status);
 /* The name this program was run with. */
 char *program_name;
 
-/* Input file descriptor. */
-static int input_desc;
-
 /* Start of buffer list. */
 static struct buffer_record *head = NULL;
 
@@ -279,7 +277,7 @@ read_input (char *dest, size_t max_n_byt
   if (max_n_bytes == 0)
     return 0;
 
-  bytes_read = safe_read (input_desc, dest, max_n_bytes);
+  bytes_read = safe_read (STDIN_FILENO, dest, max_n_bytes);
 
   if (bytes_read == 0)
     have_read_eof = true;
@@ -638,19 +636,13 @@ no_more_lines (void)
   return find_line (current_line + 1) == NULL;
 }
 
-/* Set the name of the input file to NAME and open it. */
+/* Open NAME as standard input.  */
 
 static void
 set_input_file (const char *name)
 {
-  if (STREQ (name, "-"))
-    input_desc = STDIN_FILENO;
-  else
-    {
-      input_desc = open (name, O_RDONLY);
-      if (input_desc < 0)
-       error (EXIT_FAILURE, errno, "%s", name);
-    }
+  if (! STREQ (name, "-") && fd_reopen (STDIN_FILENO, name, O_RDONLY, 0) < 0)
+    error (EXIT_FAILURE, errno, _("cannot open %s for reading"), quote (name));
 }
 
 /* Write all lines from the beginning of the buffer up to, but
@@ -940,7 +932,7 @@ create_output_file (void)
 
   /* Create the output file in a critical section, to avoid races.  */
   sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
-  output_stream = fopen_safer (output_filename, "w");
+  output_stream = fopen (output_filename, "w");
   fopen_ok = (output_stream != NULL);
   fopen_errno = errno;
   files_created += fopen_ok;
@@ -1438,7 +1430,7 @@ main (int argc, char **argv)
 
   split_file ();
 
-  if (close (input_desc) < 0)
+  if (close (STDIN_FILENO) != 0)
     {
       error (0, errno, _("read error"));
       cleanup_fatal ();
Index: src/dd.c
===================================================================
RCS file: /fetish/cu/src/dd.c,v
retrieving revision 1.183
diff -p -u -r1.183 dd.c
--- src/dd.c    28 May 2005 04:22:26 -0000      1.183
+++ src/dd.c    3 Jul 2005 06:36:26 -0000
@@ -28,6 +28,7 @@
 
 #include "system.h"
 #include "error.h"
+#include "fd-reopen.h"
 #include "gethrxtime.h"
 #include "getpagesize.h"
 #include "human.h"
@@ -752,28 +753,6 @@ iwrite (int fd, char const *buf, size_t 
   return total_written;
 }
 
-/* Open a file to a particular file descriptor.  This is like standard
-   `open', except it always returns DESIRED_FD if successful.  */
-static int
-open_fd (int desired_fd, char const *filename, int options, mode_t mode)
-{
-  int fd;
-  close (desired_fd);
-  fd = open (filename, options, mode);
-  if (fd < 0)
-    return -1;
-
-  if (fd != desired_fd)
-    {
-      if (dup2 (fd, desired_fd) != desired_fd)
-       desired_fd = -1;
-      if (close (fd) != 0)
-       return -1;
-    }
-
-  return desired_fd;
-}
-
 /* Write, then empty, the output buffer `obuf'. */
 
 static void
@@ -1630,7 +1609,7 @@ main (int argc, char **argv)
     }
   else
     {
-      if (open_fd (STDIN_FILENO, input_file, O_RDONLY | input_flags, 0) < 0)
+      if (fd_reopen (STDIN_FILENO, input_file, O_RDONLY | input_flags, 0) < 0)
        error (EXIT_FAILURE, errno, _("opening %s"), quote (input_file));
     }
 
@@ -1657,8 +1636,9 @@ main (int argc, char **argv)
         need to read to satisfy a `seek=' request.  If we can't read
         the file, go ahead with write-only access; it might work.  */
       if ((! seek_records
-          || open_fd (STDOUT_FILENO, output_file, O_RDWR | opts, perms) < 0)
-         && open_fd (STDOUT_FILENO, output_file, O_WRONLY | opts, perms) < 0)
+          || fd_reopen (STDOUT_FILENO, output_file, O_RDWR | opts, perms) < 0)
+         && (fd_reopen (STDOUT_FILENO, output_file, O_WRONLY | opts, perms)
+             < 0))
        error (EXIT_FAILURE, errno, _("opening %s"), quote (output_file));
 
 #if HAVE_FTRUNCATE
Index: src/join.c
===================================================================
RCS file: /fetish/cu/src/join.c,v
retrieving revision 1.140
diff -p -u -r1.140 join.c
--- src/join.c  16 Jun 2005 21:33:43 -0000      1.140
+++ src/join.c  3 Jul 2005 06:36:27 -0000
@@ -30,7 +30,7 @@
 #include "linebuffer.h"
 #include "memcasecmp.h"
 #include "quote.h"
-#include "stdio-safer.h"
+#include "stdio--.h"
 #include "xmemcoll.h"
 #include "xstrtol.h"
 
@@ -933,10 +933,10 @@ main (int argc, char **argv)
   if (join_field_2 == SIZE_MAX)
     join_field_2 = 0;
 
-  fp1 = STREQ (names[0], "-") ? stdin : fopen_safer (names[0], "r");
+  fp1 = STREQ (names[0], "-") ? stdin : fopen (names[0], "r");
   if (!fp1)
     error (EXIT_FAILURE, errno, "%s", names[0]);
-  fp2 = STREQ (names[1], "-") ? stdin : fopen_safer (names[1], "r");
+  fp2 = STREQ (names[1], "-") ? stdin : fopen (names[1], "r");
   if (!fp2)
     error (EXIT_FAILURE, errno, "%s", names[1]);
   if (fp1 == fp2)
Index: src/md5sum.c
===================================================================
RCS file: /fetish/cu/src/md5sum.c,v
retrieving revision 1.135
diff -p -u -r1.135 md5sum.c
--- src/md5sum.c        30 May 2005 07:33:38 -0000      1.135
+++ src/md5sum.c        3 Jul 2005 06:36:27 -0000
@@ -31,6 +31,7 @@
 #include "getline.h"
 #include "error.h"
 #include "quote.h"
+#include "stdio--.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
 #define PROGRAM_NAME (algorithm == ALG_MD5 ? "md5sum" : "sha1sum")
@@ -415,6 +416,7 @@ digest_check (const char *checkfile_name
        line[--line_length] = '\0';
 
       if (! (split_3 (line, line_length, &hex_digest, &binary, &filename)
+            && ! (is_stdin && STREQ (filename, "-"))
             && hex_digits (hex_digest)))
        {
          if (warn)
Index: src/nohup.c
===================================================================
RCS file: /fetish/cu/src/nohup.c,v
retrieving revision 1.30
diff -p -u -r1.30 nohup.c
--- src/nohup.c 3 Jun 2005 04:49:30 -0000       1.30
+++ src/nohup.c 3 Jul 2005 06:36:27 -0000
@@ -28,9 +28,10 @@
 #include "cloexec.h"
 #include "error.h"
 #include "filenamecat.h"
+#include "fd-reopen.h"
 #include "long-options.h"
 #include "quote.h"
-#include "unistd-safer.h"
+#include "unistd--.h"
 
 #define PROGRAM_NAME "nohup"
 
@@ -75,7 +76,6 @@ int
 main (int argc, char **argv)
 {
   int saved_stderr_fd = STDERR_FILENO;
-  bool nohup_out = false;
 
   initialize_main (&argc, &argv);
   program_name = argv[0];
@@ -97,6 +97,14 @@ main (int argc, char **argv)
       usage (NOHUP_FAILURE);
     }
 
+  /* If standard input is a tty, replace it with a file descriptor
+     that exists but gives you an error if you try to read it.  POSIX
+     requires nohup to leave standard input alone, but that's less
+     useful in practice as it causes a "nohup foo & exit" session to
+     hang with OpenSSH.  */
+  if (!getenv ("POSIXLY_CORRECT") && isatty (STDIN_FILENO))
+    fd_reopen (STDIN_FILENO, "/dev/null", O_WRONLY, 0);
+
   /* If standard output is a tty, redirect it (appending) to a file.
      First try nohup.out, then $HOME/nohup.out.  */
   if (isatty (STDOUT_FILENO))
@@ -106,7 +114,7 @@ main (int argc, char **argv)
       int flags = O_CREAT | O_WRONLY | O_APPEND;
       mode_t mode = S_IRUSR | S_IWUSR;
       mode_t umask_value = umask (~mode);
-      int fd = open (file, flags, mode);
+      int fd = fd_reopen (STDOUT_FILENO, file, flags, mode);
 
       if (fd < 0)
        {
@@ -115,7 +123,7 @@ main (int argc, char **argv)
          if (home)
            {
              in_home = file_name_concat (home, file, NULL);
-             fd = open (in_home, flags, mode);
+             fd = fd_reopen (STDOUT_FILENO, in_home, flags, mode);
            }
          if (fd < 0)
            {
@@ -130,15 +138,8 @@ main (int argc, char **argv)
        }
 
       umask (umask_value);
-
-      /* Redirect standard output to the file.  */
-      if (fd != STDOUT_FILENO
-         && (dup2 (fd, STDOUT_FILENO) < 0 || close (fd) != 0))
-       error (NOHUP_FAILURE, errno, _("failed to redirect standard output"));
-
       error (0, 0, _("appending output to %s"), quote (file));
       free (in_home);
-      nohup_out = true;
     }
 
   /* If standard error is a tty, redirect it to stdout.  */
@@ -148,7 +149,7 @@ main (int argc, char **argv)
         if execve fails.  It's no big deal if this dup fails.  It might
         not change anything, and at worst, it'll lead to suppression of
         the post-failed-execve diagnostic.  */
-      saved_stderr_fd = dup_safer (STDERR_FILENO);
+      saved_stderr_fd = dup (STDERR_FILENO);
 
       if (0 <= saved_stderr_fd
          && set_cloexec_flag (saved_stderr_fd, true) != 0)
@@ -164,25 +165,6 @@ main (int argc, char **argv)
        }
     }
 
-  /* If standard input is a tty, replace it with a file descriptor
-     that exists but gives you an error if you try to read it.  POSIX
-     requires nohup to leave standard input alone, but that's less
-     useful in practice as it causes a "nohup foo & exit" session to
-     hang with OpenSSH.  */
-  if (!getenv ("POSIXLY_CORRECT") && isatty (STDIN_FILENO))
-    {
-      close (STDIN_FILENO);
-      if (nohup_out)
-       dup (STDOUT_FILENO);
-      else
-       {
-         /* This won't give you a read error on older systems if you're
-            root, but there's no portable way to fix this and it's
-            not worth worrying about these days.  */
-         open ("/", O_RDONLY);
-       }
-    }
-
   signal (SIGHUP, SIG_IGN);
 
   {
Index: src/pr.c
===================================================================
RCS file: /fetish/cu/src/pr.c,v
retrieving revision 1.151
diff -p -u -r1.151 pr.c
--- src/pr.c    29 Jun 2005 16:25:50 -0000      1.151
+++ src/pr.c    3 Jul 2005 06:36:27 -0000
@@ -320,7 +320,7 @@
 #include "inttostr.h"
 #include "mbswidth.h"
 #include "quote.h"
-#include "stdio-safer.h"
+#include "stdio--.h"
 #include "strftime.h"
 #include "xstrtol.h"
 
@@ -1506,7 +1506,7 @@ open_file (char *name, COLUMN *p)
   else
     {
       p->name = name;
-      p->fp = fopen_safer (name, "r");
+      p->fp = fopen (name, "r");
     }
   if (p->fp == NULL)
     {
Index: src/shred.c
===================================================================
RCS file: /fetish/cu/src/shred.c,v
retrieving revision 1.114
diff -p -u -r1.114 shred.c
--- src/shred.c 19 Jun 2005 08:34:49 -0000      1.114
+++ src/shred.c 3 Jul 2005 06:36:27 -0000
@@ -102,13 +102,13 @@
 #include "xstrtol.h"
 #include "dirname.h"
 #include "error.h"
+#include "fcntl--.h"
 #include "gethrxtime.h"
 #include "getpagesize.h"
 #include "human.h"
 #include "inttostr.h"
 #include "quotearg.h"          /* For quotearg_colon */
 #include "quote.h"             /* For quotearg_colon */
-#include "unistd-safer.h"
 
 #define DEFAULT_PASSES 25      /* Default */
 
@@ -1354,7 +1354,6 @@ wipename (char *oldname, char const *qol
   int dir_fd = open (dir, O_WRONLY | O_NOCTTY);
   if (dir_fd < 0)
     dir_fd = open (dir, O_RDONLY | O_NOCTTY);
-  dir_fd = fd_safer (dir_fd);
 
   if (flags->verbose)
     error (0, 0, _("%s: removing"), qoldname);
@@ -1448,7 +1447,6 @@ wipefile (char *name, char const *qname,
       && (errno == EACCES && flags->force)
       && chmod (name, S_IWUSR) == 0)
     fd = open (name, O_WRONLY | O_NOCTTY);
-  fd = fd_safer (fd);
   if (fd < 0)
     {
       error (0, errno, _("%s: failed to open for writing"), qname);
Index: src/sort.c
===================================================================
RCS file: /fetish/cu/src/sort.c,v
retrieving revision 1.318
diff -p -u -r1.318 sort.c
--- src/sort.c  29 Jun 2005 16:26:20 -0000      1.318
+++ src/sort.c  3 Jul 2005 06:36:27 -0000
@@ -34,9 +34,9 @@
 #include "physmem.h"
 #include "posixver.h"
 #include "quote.h"
-#include "stdio-safer.h"
+#include "stdlib--.h"
+#include "stdio--.h"
 #include "strnumcmp.h"
-#include "unistd-safer.h"
 #include "xmemcoll.h"
 #include "xstrtol.h"
 
@@ -439,7 +439,6 @@ create_temp_file (FILE **pfp)
   sigprocmask (SIG_SETMASK, &oldset, NULL);
   errno = saved_errno;
 
-  fd = fd_safer (fd);
   if (fd < 0 || (*pfp = fdopen (fd, "w")) == NULL)
     die (_("cannot create temporary file"), file);
 
@@ -466,7 +465,8 @@ xfopen (const char *file, const char *ho
     }
   else
     {
-      if ((fp = fopen_safer (file, how)) == NULL)
+      fp = fopen (file, how);
+      if (! fp)
        die (_("open failed"), file);
     }
 
Index: src/split.c
===================================================================
RCS file: /fetish/cu/src/split.c,v
retrieving revision 1.109
diff -p -u -r1.109 split.c
--- src/split.c 16 Jun 2005 21:33:43 -0000      1.109
+++ src/split.c 3 Jul 2005 06:36:27 -0000
@@ -30,13 +30,14 @@
 #include "system.h"
 #include "dirname.h"
 #include "error.h"
+#include "fd-reopen.h"
+#include "fcntl--.h"
 #include "getpagesize.h"
 #include "full-read.h"
 #include "full-write.h"
 #include "inttostr.h"
 #include "quote.h"
 #include "safe-read.h"
-#include "unistd-safer.h"
 #include "xstrtol.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
@@ -68,9 +69,6 @@ static char const *suffix_alphabet = "ab
 /* Name of input file.  May be "-".  */
 static char *infile;
 
-/* Descriptor on which input file is open.  */
-static int input_desc;
-
 /* Descriptor on which output file is open.  */
 static int output_desc;
 
@@ -212,10 +210,10 @@ cwrite (bool new_file_flag, const char *
       next_file_name ();
       if (verbose)
        fprintf (stderr, _("creating file %s\n"), quote (outfile));
-      output_desc = fd_safer (open (outfile,
-                                   O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
-                                   (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
-                                    | S_IROTH | S_IWOTH)));
+      output_desc = open (outfile,
+                         O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
+                         (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
+                          | S_IROTH | S_IWOTH));
       if (output_desc < 0)
        error (EXIT_FAILURE, errno, "%s", outfile);
     }
@@ -237,7 +235,7 @@ bytes_split (uintmax_t n_bytes, char *bu
 
   do
     {
-      n_read = full_read (input_desc, buf, bufsize);
+      n_read = full_read (STDIN_FILENO, buf, bufsize);
       if (n_read == SAFE_READ_ERROR)
         error (EXIT_FAILURE, errno, "%s", infile);
       bp_out = buf;
@@ -281,7 +279,7 @@ lines_split (uintmax_t n_lines, char *bu
 
   do
     {
-      n_read = full_read (input_desc, buf, bufsize);
+      n_read = full_read (STDIN_FILENO, buf, bufsize);
       if (n_read == SAFE_READ_ERROR)
        error (EXIT_FAILURE, errno, "%s", infile);
       bp = bp_out = buf;
@@ -333,7 +331,7 @@ line_bytes_split (size_t n_bytes)
     {
       /* Fill up the full buffer size from the input file.  */
 
-      n_read = full_read (input_desc, buf + n_buffered, n_bytes - n_buffered);
+      n_read = full_read (STDIN_FILENO, buf + n_buffered, n_bytes - 
n_buffered);
       if (n_read == SAFE_READ_ERROR)
        error (EXIT_FAILURE, errno, "%s", infile);
 
@@ -537,23 +535,20 @@ main (int argc, char **argv)
     }
 
   /* Open the input file.  */
-  if (STREQ (infile, "-"))
-    input_desc = STDIN_FILENO;
-  else
-    {
-      input_desc = open (infile, O_RDONLY);
-      if (input_desc < 0)
-       error (EXIT_FAILURE, errno, "%s", infile);
-    }
+  if (! STREQ (infile, "-")
+      && fd_reopen (STDIN_FILENO, infile, O_RDONLY, 0) < 0)
+    error (EXIT_FAILURE, errno, _("cannot open %s for reading"),
+          quote (infile));
+
   /* Binary I/O is safer when bytecounts are used.  */
-  SET_BINARY (input_desc);
+  SET_BINARY (STDIN_FILENO);
 
   /* No output file is open now.  */
   output_desc = -1;
 
   /* Get the optimal block size of input device and make a buffer.  */
 
-  if (fstat (input_desc, &stat_buf) < 0)
+  if (fstat (STDIN_FILENO, &stat_buf) != 0)
     error (EXIT_FAILURE, errno, "%s", infile);
   in_blk_size = ST_BLKSIZE (stat_buf);
 
@@ -578,7 +573,7 @@ main (int argc, char **argv)
       abort ();
     }
 
-  if (close (input_desc) < 0)
+  if (close (STDIN_FILENO) != 0)
     error (EXIT_FAILURE, errno, "%s", infile);
   if (output_desc >= 0 && close (output_desc) < 0)
     error (EXIT_FAILURE, errno, "%s", outfile);
Index: src/stty.c
===================================================================
RCS file: /fetish/cu/src/stty.c,v
retrieving revision 1.135
diff -p -u -r1.135 stty.c
--- src/stty.c  16 Jun 2005 21:44:25 -0000      1.135
+++ src/stty.c  3 Jul 2005 06:36:27 -0000
@@ -56,6 +56,7 @@
 
 #include "system.h"
 #include "error.h"
+#include "fd-reopen.h"
 #include "quote.h"
 #include "vasprintf.h"
 #include "xstrtol.h"
@@ -420,22 +421,21 @@ static bool set_mode (struct mode_info *
 static unsigned long int integer_arg (const char *s, unsigned long int max);
 static speed_t string_to_baud (const char *arg);
 static tcflag_t *mode_type_flag (enum mode_type type, struct termios *mode);
-static void display_all (struct termios *mode, int fd, const char 
*device_name);
+static void display_all (struct termios *mode, char const *device_name);
 static void display_changed (struct termios *mode);
 static void display_recoverable (struct termios *mode);
 static void display_settings (enum output_type output_type,
-                             struct termios *mode, int fd,
+                             struct termios *mode,
                              const char *device_name);
 static void display_speed (struct termios *mode, bool fancy);
-static void display_window_size (bool fancy, int fd, const char *device_name);
+static void display_window_size (bool fancy, char const *device_name);
 static void sane_mode (struct termios *mode);
 static void set_control_char (struct control_info *info,
                              const char *arg,
                              struct termios *mode);
 static void set_speed (enum speed_setting type, const char *arg,
                       struct termios *mode);
-static void set_window_size (int rows, int cols, int fd,
-                            const char *device_name);
+static void set_window_size (int rows, int cols, char const *device_name);
 
 /* The width of the screen, for output wrapping. */
 static int max_col;
@@ -737,7 +737,6 @@ main (int argc, char **argv)
   int k;
   bool noargs = true;
   char *file_name = NULL;
-  int fd;
   const char *device_name;
 
   initialize_main (&argc, &argv);
@@ -827,31 +826,27 @@ main (int argc, char **argv)
     {
       int fdflags;
       device_name = file_name;
-      fd = open (device_name, O_RDONLY | O_NONBLOCK);
-      if (fd < 0)
+      if (fd_reopen (STDIN_FILENO, device_name, O_RDONLY | O_NONBLOCK, 0) < 0)
        error (EXIT_FAILURE, errno, "%s", device_name);
-      if ((fdflags = fcntl (fd, F_GETFL)) == -1
-         || fcntl (fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
+      if ((fdflags = fcntl (STDIN_FILENO, F_GETFL)) == -1
+         || fcntl (STDIN_FILENO, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
        error (EXIT_FAILURE, errno, _("%s: couldn't reset non-blocking mode"),
               device_name);
     }
   else
-    {
-      fd = STDIN_FILENO;
-      device_name = _("standard input");
-    }
+    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 (fd, &mode))
+  if (tcgetattr (STDIN_FILENO, &mode))
     error (EXIT_FAILURE, errno, "%s", device_name);
 
   if (verbose_output | recoverable_output | noargs)
     {
       max_col = screen_columns ();
       current_col = 0;
-      display_settings (output_type, &mode, fd, device_name);
+      display_settings (output_type, &mode, device_name);
       exit (EXIT_SUCCESS);
     }
 
@@ -941,7 +936,7 @@ main (int argc, char **argv)
                }
              ++k;
              set_window_size (integer_arg (argv[k], INT_MAX), -1,
-                              fd, device_name);
+                              device_name);
            }
          else if (STREQ (arg, "cols")
                   || STREQ (arg, "columns"))
@@ -953,13 +948,13 @@ main (int argc, char **argv)
                }
              ++k;
              set_window_size (-1, integer_arg (argv[k], INT_MAX),
-                              fd, device_name);
+                              device_name);
            }
          else if (STREQ (arg, "size"))
            {
              max_col = screen_columns ();
              current_col = 0;
-             display_window_size (false, fd, device_name);
+             display_window_size (false, device_name);
            }
 #endif
 #ifdef HAVE_C_LINE
@@ -1005,7 +1000,7 @@ main (int argc, char **argv)
     {
       struct termios new_mode;
 
-      if (tcsetattr (fd, TCSADRAIN, &mode))
+      if (tcsetattr (STDIN_FILENO, TCSADRAIN, &mode))
        error (EXIT_FAILURE, errno, "%s", device_name);
 
       /* POSIX (according to Zlotnick's book) tcsetattr returns zero if
@@ -1018,7 +1013,7 @@ main (int argc, char **argv)
       /* 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 (fd, &new_mode))
+      if (tcgetattr (STDIN_FILENO, &new_mode))
        error (EXIT_FAILURE, errno, "%s", device_name);
 
       /* Normally, one shouldn't use memcmp to compare structures that
@@ -1319,11 +1314,11 @@ get_win_size (int fd, struct winsize *wi
 }
 
 static void
-set_window_size (int rows, int cols, int fd, const char *device_name)
+set_window_size (int rows, int cols, char const *device_name)
 {
   struct winsize win;
 
-  if (get_win_size (fd, &win))
+  if (get_win_size (STDIN_FILENO, &win))
     {
       if (errno != EINVAL)
        error (EXIT_FAILURE, errno, "%s", device_name);
@@ -1367,25 +1362,25 @@ set_window_size (int rows, int cols, int
       win.ws_row = 1;
       win.ws_col = 1;
 
-      if (ioctl (fd, TIOCSWINSZ, (char *) &win))
+      if (ioctl (STDIN_FILENO, TIOCSWINSZ, (char *) &win))
        error (EXIT_FAILURE, errno, "%s", device_name);
 
-      if (ioctl (fd, TIOCSSIZE, (char *) &ttysz))
+      if (ioctl (STDIN_FILENO, TIOCSSIZE, (char *) &ttysz))
        error (EXIT_FAILURE, errno, "%s", device_name);
       return;
     }
 # endif
 
-  if (ioctl (fd, TIOCSWINSZ, (char *) &win))
+  if (ioctl (STDIN_FILENO, TIOCSWINSZ, (char *) &win))
     error (EXIT_FAILURE, errno, "%s", device_name);
 }
 
 static void
-display_window_size (bool fancy, int fd, const char *device_name)
+display_window_size (bool fancy, char const *device_name)
 {
   struct winsize win;
 
-  if (get_win_size (fd, &win))
+  if (get_win_size (STDIN_FILENO, &win))
     {
       if (errno != EINVAL)
        error (EXIT_FAILURE, errno, "%s", device_name);
@@ -1419,7 +1414,7 @@ screen_columns (void)
     return win.ws_col;
 #endif
   {
-    /* Use $COLUMNS if it's in [1..INT_MAX-1].  */
+    /* Use $COLUMNS if it's in [1..INT_MAX].  */
     char *col_string = getenv ("COLUMNS");
     long int n_columns;
     if (!(col_string != NULL
@@ -1458,7 +1453,7 @@ mode_type_flag (enum mode_type type, str
 
 static void
 display_settings (enum output_type output_type, struct termios *mode,
-                 int fd, const char *device_name)
+                 char const *device_name)
 {
   switch (output_type)
     {
@@ -1467,7 +1462,7 @@ display_settings (enum output_type outpu
       break;
 
     case all:
-      display_all (mode, fd, device_name);
+      display_all (mode, device_name);
       break;
 
     case recoverable:
@@ -1562,7 +1557,7 @@ display_changed (struct termios *mode)
 }
 
 static void
-display_all (struct termios *mode, int fd, const char *device_name)
+display_all (struct termios *mode, char const *device_name)
 {
   int i;
   tcflag_t *bitsp;
@@ -1571,7 +1566,7 @@ display_all (struct termios *mode, int f
 
   display_speed (mode, true);
 #ifdef TIOCGWINSZ
-  display_window_size (true, fd, device_name);
+  display_window_size (true, device_name);
 #endif
 #ifdef HAVE_C_LINE
   wrapf ("line = %d;", mode->c_line);
Index: src/tac.c
===================================================================
RCS file: /fetish/cu/src/tac.c,v
retrieving revision 1.122
diff -p -u -r1.122 tac.c
--- src/tac.c   19 Jun 2005 09:10:28 -0000      1.122
+++ src/tac.c   3 Jul 2005 06:36:28 -0000
@@ -48,7 +48,7 @@ tac -r -s '.\|
 #include "quote.h"
 #include "quotearg.h"
 #include "safe-read.h"
-#include "unistd-safer.h"
+#include "stdlib--.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
 #define PROGRAM_NAME "tac"
@@ -455,7 +455,8 @@ copy_to_temp (FILE **g_tmp, char **g_tem
       return false;
     }
 
-  if ((fd = fd_safer (fd)) < 0 || ! (tmp = fdopen (fd, "w+")))
+  tmp = fdopen (fd, "w+");
+  if (! tmp)
     {
       error (0, errno, _("cannot open %s for writing"), quote (tempfile));
       close (fd);
Index: src/tail.c
===================================================================
RCS file: /fetish/cu/src/tail.c,v
retrieving revision 1.238
diff -p -u -r1.238 tail.c
--- src/tail.c  16 Jun 2005 21:33:43 -0000      1.238
+++ src/tail.c  3 Jul 2005 06:36:28 -0000
@@ -35,11 +35,11 @@
 #include "argmatch.h"
 #include "c-strtod.h"
 #include "error.h"
+#include "fcntl--.h"
 #include "inttostr.h"
 #include "posixver.h"
 #include "quote.h"
 #include "safe-read.h"
-#include "unistd-safer.h"
 #include "xnanosleep.h"
 #include "xstrtol.h"
 #include "xstrtod.h"
@@ -856,8 +856,7 @@ recheck (struct File_spec *f, bool block
   bool new_file;
   int fd = (is_stdin
            ? STDIN_FILENO
-           : fd_safer (open (f->name,
-                             O_RDONLY | (blocking ? 0 : O_NONBLOCK))));
+           : open (f->name, O_RDONLY | (blocking ? 0 : O_NONBLOCK)));
 
   assert (valid_file_spec (f));
 
@@ -1286,9 +1285,7 @@ tail_file (struct File_spec *f, uintmax_
       fd = STDIN_FILENO;
     }
   else
-    {
-      fd = fd_safer (open (f->name, O_RDONLY));
-    }
+    fd = open (f->name, O_RDONLY);
 
   f->tailable = !(reopen_inaccessible_files && fd == -1);
 
Index: src/tee.c
===================================================================
RCS file: /fetish/cu/src/tee.c,v
retrieving revision 1.80
diff -p -u -r1.80 tee.c
--- src/tee.c   14 May 2005 07:58:37 -0000      1.80
+++ src/tee.c   3 Jul 2005 06:36:28 -0000
@@ -25,7 +25,7 @@
 
 #include "system.h"
 #include "error.h"
-#include "stdio-safer.h"
+#include "stdio--.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
 #define PROGRAM_NAME "tee"
@@ -162,7 +162,7 @@ tee (int nfiles, const char **files)
     {
       descriptors[i] = (STREQ (files[i], "-")
                        ? stdout
-                       : fopen_safer (files[i], mode_string));
+                       : fopen (files[i], mode_string));
       if (descriptors[i] == NULL)
        {
          error (0, errno, "%s", files[i]);
Index: src/touch.c
===================================================================
RCS file: /fetish/cu/src/touch.c,v
retrieving revision 1.131
diff -p -u -r1.131 touch.c
--- src/touch.c 14 May 2005 07:58:37 -0000      1.131
+++ src/touch.c 3 Jul 2005 06:36:28 -0000
@@ -26,12 +26,12 @@
 #include "system.h"
 #include "argmatch.h"
 #include "error.h"
+#include "fcntl--.h"
 #include "getdate.h"
 #include "posixtm.h"
 #include "posixver.h"
 #include "quote.h"
 #include "safe-read.h"
-#include "unistd-safer.h"
 #include "utimens.h"
 
 /* The official name of this program (e.g., no `g' prefix).  */
@@ -129,7 +129,6 @@ touch (const char *file)
       /* Try to open FILE, creating it if necessary.  */
       fd = open (file, O_WRONLY | O_CREAT | O_NONBLOCK | O_NOCTTY,
                 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
-      fd = fd_safer (fd);
 
       /* Don't save a copy of errno if it's EISDIR, since that would lead
         touch to give a bogus diagnostic for e.g., `touch /' (assuming




reply via email to

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