[BusyBox] getty: dup2 [PATCH]

Shaun Jackman sjackman at gmail.com
Thu Jun 30 22:36:16 UTC 2005


This patch uses dup2 instead of close/dup, which could reduce code
size by a few bytes. It also uses a couple ifdef armours to help it
build on a system without utmp.h.

Cheers,
Shaun

2005-06-30  Shaun Jackman  <sjackman at gmail.com>

	* loginutils/getty.c: Include utmp.h only if
	CONFIG_FEATURE_U_W_TMP is defined.
	(getty_main): Use ISSUE only if it is defined.
	(open_tty): Use dup2 instead of close/dup.

--- loginutils/getty.c
+++ loginutils/getty.c
@@ -25,7 +25,9 @@
 #include <fcntl.h>
 #include <stdarg.h>
 #include <ctype.h>
+#ifdef CONFIG_FEATURE_U_W_TMP
 #include <utmp.h>
+#endif
 #include <getopt.h>
 #include <termios.h>
 #include "busybox.h"
@@ -260,7 +262,11 @@
 		_PATH_LOGIN,			/* default login program */
 		"tty1",					/* default tty line */
 		"",						/* modem init string */
+#ifdef ISSUE
 		ISSUE,					/* default issue file */
+#else
+		NULL,
+#endif
 		0,						/* no baud rates known yet */
 	};
 
@@ -547,16 +553,11 @@
 /* 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... */
 
@@ -569,12 +570,11 @@
 
 		/* 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 {
 
@@ -587,9 +587,11 @@
 			error("%s: not open for read/write", tty);
 	}
 
-	/* Set up standard output and standard error file descriptors. */
+	/* Get rid of the present standard { output, error} if any and
+	 * set up standard output and standard error file descriptors. */
 	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 */
 
 	/*



More information about the busybox mailing list