Rizin
unix-like reverse engineering framework and cli tools
cpipe.c File Reference
#include <rz_cons.h>
#include <limits.h>

Go to the source code of this file.

Macros

#define O_BINARY   0
 

Functions

static bool __dupDescriptor (int fd, int fdn)
 
RZ_API int rz_cons_pipe_open (const char *file, int fdn, int append)
 
RZ_API void rz_cons_pipe_close (int fd)
 

Variables

static int backup_fd = -1
 
static int backup_fdn = 1
 

Macro Definition Documentation

◆ O_BINARY

#define O_BINARY   0

Definition at line 13 of file cpipe.c.

Function Documentation

◆ __dupDescriptor()

static bool __dupDescriptor ( int  fd,
int  fdn 
)
static

Definition at line 16 of file cpipe.c.

16  {
17 #if __WINDOWS__
18  backup_fd = 2002 - (fd - 2); // windows xp has 2048 as limit fd
19  return _dup2(fdn, backup_fd) != -1;
20 #else
21  backup_fd = sysconf(_SC_OPEN_MAX) - (fd - 2); // portable getdtablesize()
22  if (backup_fd < 2) {
23  backup_fd = 2002 - (fd - 2); // fallback
24  }
25  return dup2(fdn, backup_fd) != -1;
26 #endif
27 }
static int backup_fd
Definition: cpipe.c:9
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause const char static mode static sync const char const char static newpath const char static pathname unsigned long static filedes void static end_data_segment static handler static getegid char static len static pgid const char static path dup2
Definition: sflib.h:94
static const z80_opcode fd[]
Definition: z80_tab.h:997

References backup_fd, dup2, and fd.

Referenced by rz_cons_pipe_open().

◆ rz_cons_pipe_close()

RZ_API void rz_cons_pipe_close ( int  fd)

Definition at line 53 of file cpipe.c.

53  {
54  if (fd != -1) {
55  close(fd);
56  if (backup_fd != -1) {
59  backup_fd = -1;
60  }
61  }
62 }
static int backup_fdn
Definition: cpipe.c:10
static static fork const void static count close
Definition: sflib.h:33

References backup_fd, backup_fdn, close, dup2, and fd.

Referenced by __rap_reg_profile(), DEFINE_HANDLE_TS_FCN_AND_SYMBOL(), rz_core_cmd_str_pipe(), and rz_core_cmd_subst_i().

◆ rz_cons_pipe_open()

RZ_API int rz_cons_pipe_open ( const char *  file,
int  fdn,
int  append 
)

Definition at line 29 of file cpipe.c.

29  {
30  if (fdn < 1) {
31  return -1;
32  }
33  const int fd_flags = O_BINARY | O_RDWR | O_CREAT | (append ? O_APPEND : O_TRUNC);
34  int fd = rz_sys_open(file, fd_flags, 0644);
35  if (fd == -1) {
36  eprintf("rz_cons_pipe_open: Cannot open file '%s'\n", file);
37  return -1;
38  }
39  if (backup_fd != -1) {
41  // already set in __dupDescriptor // backup_fd = -1;
42  }
43  backup_fdn = fdn;
44  if (!__dupDescriptor(fd, fdn)) {
45  eprintf("Cannot dup stdout to %d\n", fdn);
46  return -1;
47  }
48  close(fdn);
49  dup2(fd, fdn);
50  return fd;
51 }
#define append(x, y)
Definition: cmd_print.c:1740
#define O_BINARY
Definition: cpipe.c:13
static bool __dupDescriptor(int fd, int fdn)
Definition: cpipe.c:16
#define eprintf(x, y...)
Definition: rlcc.c:7
RZ_API int rz_sys_open(const char *path, int perm, int mode)
Definition: sys.c:1740
#define O_CREAT
Definition: sftypes.h:489
#define O_RDWR
Definition: sftypes.h:488
#define O_TRUNC
Definition: sftypes.h:492
#define O_APPEND
Definition: sftypes.h:493
Definition: gzappend.c:170

References __dupDescriptor(), append, backup_fd, backup_fdn, close, dup2, eprintf, fd, O_APPEND, O_BINARY, O_CREAT, O_RDWR, O_TRUNC, and rz_sys_open().

Referenced by __rap_reg_profile(), DEFINE_HANDLE_TS_FCN_AND_SYMBOL(), rz_core_cmd_str_pipe(), and rz_core_cmd_subst_i().

Variable Documentation

◆ backup_fd

int backup_fd = -1
static

Definition at line 9 of file cpipe.c.

Referenced by __dupDescriptor(), rz_cons_pipe_close(), and rz_cons_pipe_open().

◆ backup_fdn

int backup_fdn = 1
static

Definition at line 10 of file cpipe.c.

Referenced by rz_cons_pipe_close(), and rz_cons_pipe_open().