Use symbolic names for standard file descriptors.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2013-12-20 11:26:56 +04:00
parent c81c6c9f54
commit 094fd0cd89
10 changed files with 38 additions and 36 deletions

View File

@ -23,6 +23,8 @@
#include <time.h> /* AIX for tm */
#endif
#include <unistd.h>
/*** typedefs(not structures) and defined constants **********************************************/
#ifndef major
@ -40,6 +42,18 @@
#define makedev(major,minor) ((((major) & 0xff) << 8) | ((minor) & 0xff))
#endif
#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif
#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif
#ifndef STDERR_FILENO
#define STDERR_FILENO 2
#endif
/*** enums ***************************************************************************************/
/*** structures declarations (and typedefs of structures)*****************************************/

View File

@ -58,11 +58,12 @@
#ifdef HAVE_GET_PROCESS_STATS
#include <sys/procstats.h>
#endif
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include "lib/global.h"
#include "lib/unixcompat.h"
#include "lib/vfs/vfs.h" /* VFS_ENCODING_PREFIX */
#include "lib/strutil.h" /* str_move() */
#include "lib/util.h"
@ -516,8 +517,8 @@ open_error_pipe (void)
{
message (D_NORMAL, _("Warning"), _("Pipe failed"));
}
old_error = dup (2);
if (old_error < 0 || close (2) || dup (error_pipe[1]) != 2)
old_error = dup (STDERR_FILENO);
if (old_error < 0 || close (STDERR_FILENO) != 0 || dup (error_pipe[1]) != STDERR_FILENO)
{
message (D_NORMAL, _("Warning"), _("Dup failed"));
@ -577,7 +578,7 @@ close_error_pipe (int error, const char *text)
title = _("Warning");
if (old_error >= 0)
{
if (dup2 (old_error, 2) == -1)
if (dup2 (old_error, STDERR_FILENO) == -1)
{
if (error < 0)
error = D_ERROR;

View File

@ -38,7 +38,6 @@
#include <grp.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "lib/global.h"
#include "lib/unixcompat.h"

View File

@ -43,10 +43,11 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h> /* waitpid() */
#include <unistd.h>
#include <fcntl.h>
#include "lib/global.h"
#include "lib/unixcompat.h"
#include "lib/tty/key.h" /* add_select_channel(), delete_select_channel() */
#include "lib/widget.h" /* message() */
#include "lib/event-types.h"
@ -547,18 +548,18 @@ do_background (file_op_context_t * ctx, char *info)
top_dlg = NULL;
/* Make stdin/stdout/stderr point somewhere */
close (0);
close (1);
close (2);
close (STDIN_FILENO);
close (STDOUT_FILENO);
close (STDERR_FILENO);
nullfd = open ("/dev/null", O_RDWR);
if (nullfd != -1)
{
while (dup2 (nullfd, 0) == -1 && errno == EINTR)
while (dup2 (nullfd, STDIN_FILENO) == -1 && errno == EINTR)
;
while (dup2 (nullfd, 1) == -1 && errno == EINTR)
while (dup2 (nullfd, STDOUT_FILENO) == -1 && errno == EINTR)
;
while (dup2 (nullfd, 2) == -1 && errno == EINTR)
while (dup2 (nullfd, STDERR_FILENO) == -1 && errno == EINTR)
;
}

View File

@ -39,10 +39,10 @@
#include <sys/ioctl.h>
#endif
#endif
#include <unistd.h>
#include "lib/global.h"
#include "lib/unixcompat.h"
#include "lib/tty/tty.h"
#include "lib/skin.h" /* tty_set_normal_attrs */
#include "lib/tty/win.h"
@ -193,17 +193,17 @@ handle_console_linux (console_action_t action)
/* Bind the pipe 0 to the standard input */
do
{
if (dup2 (pipefd1[0], 0) == -1)
if (dup2 (pipefd1[0], STDIN_FILENO) == -1)
break;
status = close (pipefd1[0]);
/* Bind the pipe 1 to the standard output */
if (dup2 (pipefd2[1], 1) == -1)
if (dup2 (pipefd2[1], STDOUT_FILENO) == -1)
break;
status = close (pipefd2[1]);
/* Bind standard error to /dev/null */
status = open ("/dev/null", O_WRONLY);
if (dup2 (status, 2) == -1)
if (dup2 (status, STDERR_FILENO) == -1)
break;
status = close (status);
if (tty_name != NULL)

View File

@ -67,7 +67,8 @@
#endif
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include "lib/unixcompat.h" /* STDERR_FILENO */
#define LINUX_CONS_SAVER_C
#include "cons.saver.h"
@ -164,7 +165,7 @@ main (int argc, char **argv)
const char *p, *q;
struct winsize winsz;
close (2);
close (STDERR_FILENO);
if (argc != 2)
die ();

View File

@ -37,7 +37,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "lib/global.h"

View File

@ -47,7 +47,6 @@
#include <sys/ioctl.h>
#endif
#include <termios.h>
#include <unistd.h>
#ifdef HAVE_STROPTS_H
#include <stropts.h> /* For I_PUSH */
@ -55,6 +54,7 @@
#include "lib/global.h"
#include "lib/unixcompat.h"
#include "lib/tty/tty.h" /* LINES */
#include "lib/tty/key.h" /* XCTRL */
#include "lib/vfs/vfs.h"
@ -94,18 +94,6 @@ gboolean update_subshell_prompt = FALSE;
#define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
#endif
#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif
#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif
#ifndef STDERR_FILENO
#define STDERR_FILENO 2
#endif
/* Initial length of the buffer for the subshell's prompt */
#define INITIAL_PROMPT_SIZE 10

View File

@ -36,7 +36,6 @@
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "lib/global.h"
#include "lib/unixcompat.h"

View File

@ -332,11 +332,11 @@ fish_pipeopen (struct vfs_s_super *super, const char *path, const char *argv[])
}
else
{
res = dup2 (fileset1[0], 0);
res = dup2 (fileset1[0], STDIN_FILENO);
close (fileset1[0]);
close (fileset1[1]);
res = dup2 (fileset2[1], 1);
close (2);
res = dup2 (fileset2[1], STDOUT_FILENO);
close (STDERR_FILENO);
/* stderr to /dev/null */
res = open ("/dev/null", O_WRONLY);
close (fileset2[0]);