more correction for getopt_ulflags() documentation by author of this fuck logic
[oweals/busybox.git] / console-tools / openvt.c
index 269dfc01a48997c37b8aba2ad3e07ebb74d00ae5..d9f49bbead77d4203b86ed82fab081295ae5288b 100644 (file)
 
 #include "busybox.h"
 
-#define VTNAME "/dev/tty%d"
-
 int openvt_main(int argc, char **argv)
 {
-       int pid;
        int fd;
-       int vtno;
-       char vtname[sizeof VTNAME + 2];
+       char vtname[sizeof VC_FORMAT + 2];
 
 
        if (argc < 3)
         bb_show_usage();
 
-       if (!isdigit(argv[1][0]))
-               bb_show_usage();
-
-       vtno = (int) atol(argv[1]);
-
-       /* if (vtno <= 0 || vtno > 63) */
-       if (vtno <= 0 || vtno > 12)
-               bb_error_msg_and_die("Illegal vt number (%d)", vtno);     
-
-       sprintf(vtname, VTNAME, vtno);
+       /* check for Illegal vt number: < 1 or > 12 */
+       sprintf(vtname, VC_FORMAT,(int)bb_xgetlarg(argv[1], 10, 1, 12));
 
        argv+=2;
        argc-=2;
 
-       if((pid = fork()) == 0) {
+       if(fork() == 0) {
                /* leave current vt */
 
 #ifdef   ESIX_5_3_2_D
@@ -68,19 +56,16 @@ int openvt_main(int argc, char **argv)
                if (setsid() < 0) {
 #endif
 
-                       bb_perror_msg_and_die("Unable to set new session");       
+                       bb_perror_msg_and_die("Unable to set new session");     
                }
                close(0);                       /* so that new vt becomes stdin */
 
                /* and grab new one */
-               if ((fd = open(vtname, O_RDWR)) == -1)
-                       bb_perror_msg_and_die("could not open %s", vtname);       
+               fd = bb_xopen(vtname, O_RDWR);
 
                /* Reassign stdout and sterr */
-               close(1);
-               close(2);
-               dup(fd);
-               dup(fd);
+               dup2(fd, STDOUT_FILENO);
+               dup2(fd, STDERR_FILENO);
 
                execvp(argv[0], argv);
                _exit(1);