2011-04-04 16:33:27 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2011 pancake<nopcode.org> */
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
#include <r_cons.h>
|
|
|
|
#include <unistd.h>
|
2011-11-16 01:06:46 +00:00
|
|
|
#include <limits.h>
|
2009-02-05 21:08:46 +00:00
|
|
|
|
|
|
|
//TODO: cons_pipe should be using a stack pipe_push, pipe_pop
|
|
|
|
/* this is the base fd.. more than one is supported :) */
|
2011-11-15 23:44:18 +00:00
|
|
|
static int backup_fd = -1;
|
2009-02-05 21:08:46 +00:00
|
|
|
|
2010-07-12 19:37:40 +00:00
|
|
|
R_API int r_cons_pipe_open(const char *file, int append) {
|
2011-04-04 16:33:27 +00:00
|
|
|
int fd = open (file, O_RDWR | O_CREAT | (append?O_APPEND:O_TRUNC), 0644);
|
2009-02-05 21:08:46 +00:00
|
|
|
if (fd==-1) {
|
2010-08-23 04:39:23 +00:00
|
|
|
eprintf ("Cannot open file '%s'\n", file);
|
2009-02-05 21:08:46 +00:00
|
|
|
return -1;
|
2010-08-23 04:39:23 +00:00
|
|
|
} else eprintf ("%s created\n", file);
|
2011-11-15 23:44:18 +00:00
|
|
|
if (backup_fd != -1) {
|
|
|
|
close (backup_fd);
|
|
|
|
}
|
2011-11-16 01:06:46 +00:00
|
|
|
#if __WINDOWS__
|
|
|
|
backup_fd = 2002-(fd-2); // windows xp has 2048 as limit fd
|
|
|
|
if (_dup2 (1, backup_fd) == -1) {
|
|
|
|
#else
|
2011-11-15 23:44:18 +00:00
|
|
|
backup_fd = sysconf (_SC_OPEN_MAX)-(fd-2); // portable getdtablesize()
|
2011-04-04 16:33:27 +00:00
|
|
|
if (dup2 (1, backup_fd) == -1) {
|
2011-11-16 01:06:46 +00:00
|
|
|
#endif
|
2011-04-04 16:33:27 +00:00
|
|
|
eprintf ("Cannot dup stdout to %d\n", backup_fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close (1);
|
2010-08-23 04:39:23 +00:00
|
|
|
dup2 (fd, 1);
|
2009-02-05 21:08:46 +00:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2010-07-12 19:37:40 +00:00
|
|
|
R_API void r_cons_pipe_close(int fd) {
|
2009-02-05 21:08:46 +00:00
|
|
|
if (fd == -1)
|
|
|
|
return;
|
2011-04-04 16:33:27 +00:00
|
|
|
close (fd);
|
2011-11-15 23:44:18 +00:00
|
|
|
if (backup_fd != -1) {
|
|
|
|
dup2 (backup_fd, 1);
|
|
|
|
close (backup_fd);
|
|
|
|
backup_fd = -1;
|
|
|
|
}
|
2009-02-05 21:08:46 +00:00
|
|
|
}
|