tweaks: use the standard symbols for the three standard file descriptors
Also, take into account (as elsewhere) that dup2() could fail, and harmonize their error checks.master
parent
bc14fc35d7
commit
e1601d2eb5
|
@ -135,8 +135,6 @@
|
|||
#define BAD_COLOR -2
|
||||
#endif
|
||||
|
||||
#define STANDARD_INPUT 0
|
||||
|
||||
/* Enumeration types. */
|
||||
typedef enum {
|
||||
NIX_FILE, DOS_FILE, MAC_FILE
|
||||
|
|
13
src/files.c
13
src/files.c
|
@ -722,7 +722,7 @@ void read_file(FILE *f, int fd, const char *filename, bool undoable)
|
|||
|
||||
/* When reading from stdin, restore the terminal and reenter curses mode. */
|
||||
if (isendwin()) {
|
||||
if (!isatty(STANDARD_INPUT))
|
||||
if (!isatty(STDIN_FILENO))
|
||||
reconnect_and_store_state();
|
||||
terminal_init();
|
||||
doupdate();
|
||||
|
@ -966,13 +966,16 @@ bool execute_command(const char *command)
|
|||
close(from_fd[0]);
|
||||
|
||||
/* Connect the write end of the output pipe to the process' output streams. */
|
||||
dup2(from_fd[1], fileno(stdout));
|
||||
dup2(from_fd[1], fileno(stderr));
|
||||
if (dup2(from_fd[1], STDOUT_FILENO) < 0)
|
||||
exit(3);
|
||||
if (dup2(from_fd[1], STDERR_FILENO) < 0)
|
||||
exit(4);
|
||||
|
||||
/* If the parent sends text, connect the read end of the
|
||||
* feeding pipe to the child's input stream. */
|
||||
if (should_pipe) {
|
||||
dup2(to_fd[0], fileno(stdin));
|
||||
if (dup2(to_fd[0], STDIN_FILENO) < 0)
|
||||
exit(5);
|
||||
close(to_fd[1]);
|
||||
}
|
||||
|
||||
|
@ -980,7 +983,7 @@ bool execute_command(const char *command)
|
|||
execl(theshell, tail(theshell), "-c", should_pipe ? &command[1] : command, NULL);
|
||||
|
||||
/* If the exec call returns, there was an error. */
|
||||
exit(1);
|
||||
exit(6);
|
||||
}
|
||||
|
||||
/* Parent: close the unused write end of the pipe. */
|
||||
|
|
17
src/nano.c
17
src/nano.c
|
@ -225,7 +225,7 @@ void restore_terminal(void)
|
|||
printf("\x1B[?2004l");
|
||||
fflush(stdout);
|
||||
#endif
|
||||
tcsetattr(0, TCSANOW, &original_state);
|
||||
tcsetattr(STDIN_FILENO, TCSANOW, &original_state);
|
||||
}
|
||||
|
||||
/* Exit normally: restore terminal state and report any startup errors. */
|
||||
|
@ -813,15 +813,14 @@ void reconnect_and_store_state(void)
|
|||
{
|
||||
int thetty = open("/dev/tty", O_RDONLY);
|
||||
|
||||
if (thetty < 1)
|
||||
if (thetty < 0 || dup2(thetty, STDIN_FILENO) < 0)
|
||||
die(_("Could not reconnect stdin to keyboard\n"));
|
||||
|
||||
dup2(thetty, STANDARD_INPUT);
|
||||
close(thetty);
|
||||
|
||||
/* If input was not cut short, store the current state of the terminal. */
|
||||
if (!control_C_was_pressed)
|
||||
tcgetattr(0, &original_state);
|
||||
tcgetattr(STDIN_FILENO, &original_state);
|
||||
}
|
||||
|
||||
/* Read whatever comes from standard input into a new buffer. */
|
||||
|
@ -832,7 +831,7 @@ bool scoop_stdin(void)
|
|||
restore_terminal();
|
||||
|
||||
/* When input comes from a terminal, show a helpful message. */
|
||||
if (isatty(STANDARD_INPUT))
|
||||
if (isatty(STDIN_FILENO))
|
||||
fprintf(stderr, _("Reading data from keyboard; "
|
||||
"type ^D or ^D^D to finish.\n"));
|
||||
|
||||
|
@ -1801,16 +1800,16 @@ int main(int argc, char **argv)
|
|||
struct vt_stat dummy;
|
||||
|
||||
/* Check whether we're running on a Linux console. */
|
||||
on_a_vt = (ioctl(1, VT_GETSTATE, &dummy) == 0);
|
||||
on_a_vt = (ioctl(STDOUT_FILENO, VT_GETSTATE, &dummy) == 0);
|
||||
#endif
|
||||
|
||||
/* Back up the terminal settings so that they can be restored. */
|
||||
tcgetattr(0, &original_state);
|
||||
tcgetattr(STDIN_FILENO, &original_state);
|
||||
|
||||
/* Get the state of standard input and ensure it uses blocking mode. */
|
||||
stdin_flags = fcntl(0, F_GETFL, 0);
|
||||
stdin_flags = fcntl(STDIN_FILENO, F_GETFL, 0);
|
||||
if (stdin_flags != -1)
|
||||
fcntl(0, F_SETFL, stdin_flags & ~O_NONBLOCK);
|
||||
fcntl(STDIN_FILENO, F_SETFL, stdin_flags & ~O_NONBLOCK);
|
||||
|
||||
#ifdef ENABLE_UTF8
|
||||
/* If setting the locale is successful and it uses UTF-8, we need
|
||||
|
|
16
src/text.c
16
src/text.c
|
@ -2340,11 +2340,11 @@ void do_int_speller(const char *tempfile_name)
|
|||
exit(6);
|
||||
|
||||
/* Connect standard input to the temporary file. */
|
||||
if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO)
|
||||
if (dup2(tempfile_fd, STDIN_FILENO) < 0)
|
||||
exit(7);
|
||||
|
||||
/* Connect standard output to the write end of the first pipe. */
|
||||
if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
|
||||
if (dup2(spell_fd[1], STDOUT_FILENO) < 0)
|
||||
exit(8);
|
||||
|
||||
close(tempfile_fd);
|
||||
|
@ -2365,11 +2365,11 @@ void do_int_speller(const char *tempfile_name)
|
|||
/* Fork a process to run sort in. */
|
||||
if ((pid_sort = fork()) == 0) {
|
||||
/* Connect standard input to the read end of the first pipe. */
|
||||
if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO)
|
||||
if (dup2(spell_fd[0], STDIN_FILENO) < 0)
|
||||
exit(7);
|
||||
|
||||
/* Connect standard output to the write end of the second pipe. */
|
||||
if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
|
||||
if (dup2(sort_fd[1], STDOUT_FILENO) < 0)
|
||||
exit(8);
|
||||
|
||||
close(spell_fd[0]);
|
||||
|
@ -2387,10 +2387,10 @@ void do_int_speller(const char *tempfile_name)
|
|||
|
||||
/* Fork a process to run uniq in. */
|
||||
if ((pid_uniq = fork()) == 0) {
|
||||
if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO)
|
||||
if (dup2(sort_fd[0], STDIN_FILENO) < 0)
|
||||
exit(7);
|
||||
|
||||
if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
|
||||
if (dup2(uniq_fd[1], STDOUT_FILENO) < 0)
|
||||
exit(8);
|
||||
|
||||
close(sort_fd[0]);
|
||||
|
@ -2607,9 +2607,9 @@ void do_linter(void)
|
|||
/* Fork a process to run the linter in. */
|
||||
if ((pid_lint = fork()) == 0) {
|
||||
/* Redirect standard output and standard error into the pipe. */
|
||||
if (dup2(lint_fd[1], STDOUT_FILENO) != STDOUT_FILENO)
|
||||
if (dup2(lint_fd[1], STDOUT_FILENO) < 0)
|
||||
exit(7);
|
||||
if (dup2(lint_fd[1], STDERR_FILENO) != STDERR_FILENO)
|
||||
if (dup2(lint_fd[1], STDERR_FILENO) < 0)
|
||||
exit(8);
|
||||
|
||||
close(lint_fd[0]);
|
||||
|
|
Loading…
Reference in New Issue