Patch for the debian-cvs package, ip* applets in /bin, patch from Bastian Blank
[oweals/busybox.git] / console-tools / openvt.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *  openvt.c - open a vt to run a command.
4  *
5  *  busyboxed by Quy Tonthat <quy@signal3.com>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21
22 /* getopt not needed */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <sys/types.h>
30 #include <ctype.h>
31
32 #include "busybox.h"
33
34 #define VTNAME "/dev/tty%d"
35
36 int openvt_main(int argc, char **argv)
37 {
38         int pid;
39         int fd;
40         int vtno;
41         char vtname[sizeof VTNAME + 2];
42         char * cmd = NULL;
43         char * cmd_args = NULL;
44
45         if (argc < 3)
46         show_usage();
47
48         if (!isdigit(argv[1][0]))
49                 show_usage();
50
51         vtno = (int) atol(argv[1]);
52
53         /* if (vtno <= 0 || vtno > 63) */
54         if (vtno <= 0 || vtno > 12)
55                 error_msg_and_die("Illegal vt number (%d)", vtno);        
56
57         sprintf(vtname, VTNAME, vtno);
58
59         cmd = argv[2];
60         cmd_args = xmalloc(80);
61         cmd_args[0] = '\0';
62
63         if((pid = fork()) == 0) {
64                 /* leave current vt */
65
66 #ifdef   ESIX_5_3_2_D
67                 if (setpgrp() < 0) {
68 #else
69                 if (setsid() < 0) {
70 #endif
71
72                         perror_msg_and_die("Unable to set new session");          
73                 }
74                 close(0);                       /* so that new vt becomes stdin */
75
76                 /* and grab new one */
77                 if ((fd = open(vtname, O_RDWR)) == -1)
78                         perror_msg_and_die("could not open %s", vtname);          
79
80                 /* Reassign stdout and sterr */
81                 close(1);
82                 close(2);
83                 dup(fd);
84                 dup(fd);
85
86                 execvp(cmd, &argv[2]);
87                 /*execlp(cmd, cmd_args);*/
88                 _exit(1);
89         }
90         return EXIT_SUCCESS;
91 }
92
93 /*
94 Local Variables:
95 c-file-style: "linux"
96 c-basic-offset: 4
97 tab-width: 4
98 End:
99 */