From b38100974030617ec4fe5b20b38a045df6e92d2d Mon Sep 17 00:00:00 2001
From: Mike Frysinger
Date: Fri, 1 Jul 2005 01:29:44 +0000
Subject: 2005-06-30  Shaun Jackman  <sjackman@gmail.com>

        * loginutils/getty.c: (open_tty): Use dup2 instead of close/dup.

---
 loginutils/getty.c | 30 ++++++++++++++----------------
 1 file changed, 14 insertions(+), 16 deletions(-)

(limited to 'loginutils')

diff --git a/loginutils/getty.c b/loginutils/getty.c
index d123d49..9648a17 100644
--- a/loginutils/getty.c
+++ b/loginutils/getty.c
@@ -25,13 +25,14 @@
 #include <fcntl.h>
 #include <stdarg.h>
 #include <ctype.h>
-#ifdef CONFIG_FEATURE_UTMP
-#include <utmp.h>
-#endif
 #include <getopt.h>
 #include <termios.h>
 #include "busybox.h"
 
+#ifdef CONFIG_FEATURE_UTMP
+#include <utmp.h>
+#endif
+
 #define _PATH_LOGIN     "/bin/login"
 
  /* If USE_SYSLOG is undefined all diagnostics go directly to /dev/console. */
@@ -153,7 +154,7 @@ struct options {
 
 /* Storage for things detected while the login name was read. */
 
-static struct chardata {
+struct chardata {
 	int erase;					/* erase character */
 	int kill;					/* kill character */
 	int eol;					/* end-of-line character */
@@ -233,9 +234,11 @@ static int caps_lock(const char *s);
 static int bcode(char *s);
 static void error(const char *fmt, ...) __attribute__ ((noreturn));
 
+#ifdef	SYSV_STYLE
 #ifdef CONFIG_FEATURE_UTMP
 static void update_utmp(char *line);
 #endif
+#endif
 
 /* The following is used for understandable diagnostics. */
 
@@ -552,16 +555,11 @@ static void update_utmp(char *line)
 /* open_tty - set up tty as standard { input, output, error } */
 static void open_tty(char *tty, struct termio *tp, int local)
 {
-	/* Get rid of the present standard { output, error} if any. */
-
-	(void) close(1);
-	(void) close(2);
-	errno = 0;					/* ignore above errors */
-
 	/* Set up new standard input, unless we are given an already opened port. */
 
 	if (strcmp(tty, "-")) {
 		struct stat st;
+		int fd;
 
 		/* Sanity checks... */
 
@@ -574,12 +572,11 @@ static void open_tty(char *tty, struct termio *tp, int local)
 
 		/* Open the tty as standard input. */
 
-		(void) close(0);
-		errno = 0;				/* ignore close(2) errors */
-
 		debug("open(2)\n");
-		if (open(tty, O_RDWR | O_NONBLOCK, 0) != 0)
+		fd = open(tty, O_RDWR | O_NONBLOCK, 0);
+		if (dup2(fd, STDIN_FILENO) == -1)
 			error("/dev/%s: cannot open as standard input: %m", tty);
+		close(fd);
 
 	} else {
 
@@ -592,9 +589,10 @@ static void open_tty(char *tty, struct termio *tp, int local)
 			error("%s: not open for read/write", tty);
 	}
 
-	/* Set up standard output and standard error file descriptors. */
+	/* Replace current standard output/error fd's with new ones */
 	debug("duping\n");
-	if (dup(0) != 1 || dup(0) != 2)	/* set up stdout and stderr */
+	if (dup2(STDIN_FILENO, STDOUT_FILENO) == -1 ||
+	    dup2(STDIN_FILENO, STDERR_FILENO) == -1)
 		error("%s: dup problem: %m", tty);	/* we have a problem */
 
 	/*
-- 
cgit v1.1