Skip to content

Commit

Permalink
Use symbolic names for standard file descriptors.
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Borodin <[email protected]>
  • Loading branch information
aborodin committed Feb 10, 2014
1 parent c81c6c9 commit 094fd0c
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 36 deletions.
14 changes: 14 additions & 0 deletions lib/unixcompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <time.h> /* AIX for tm */
#endif

#include <unistd.h>

/*** typedefs(not structures) and defined constants **********************************************/

#ifndef major
Expand All @@ -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)*****************************************/
Expand Down
9 changes: 5 additions & 4 deletions lib/utilunix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"));

Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion lib/vfs/utilvfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
#include <grp.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "lib/global.h"
#include "lib/unixcompat.h"
Expand Down
15 changes: 8 additions & 7 deletions src/background.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
;
}

Expand Down
8 changes: 4 additions & 4 deletions src/cons.handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/consaver/cons.saver.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 ();
Expand Down
1 change: 0 additions & 1 deletion src/filemanager/panel.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "lib/global.h"

Expand Down
14 changes: 1 addition & 13 deletions src/subshell.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@
#include <sys/ioctl.h>
#endif
#include <termios.h>
#include <unistd.h>

#ifdef HAVE_STROPTS_H
#include <stropts.h> /* For I_PUSH */
#endif /* HAVE_STROPTS_H */

#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"
Expand Down Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion src/vfs/cpio/cpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions src/vfs/fish/fish.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down

0 comments on commit 094fd0c

Please sign in to comment.