Standardize on the vi editing directives being on the first line.
[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  *  hacked by Tito <farmatito@tiscali.it>
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9  */
10
11 /* getopt not needed */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <string.h>
18 #include <sys/types.h>
19 #include <ctype.h>
20
21 #include "busybox.h"
22
23 int openvt_main(int argc, char **argv)
24 {
25         int fd;
26         char vtname[sizeof(VC_FORMAT) + 2];
27
28
29         if (argc < 3) {
30                 bb_show_usage();
31         }
32         /* check for Illegal vt number: < 1 or > 12 */
33         sprintf(vtname, VC_FORMAT, (int)bb_xgetlarg(argv[1], 10, 1, 12));
34
35         if (fork() == 0) {
36                 /* leave current vt */
37                 if (setsid() < 0) {
38                         bb_perror_msg_and_die("setsid");
39                 }
40                 close(0);                       /* so that new vt becomes stdin */
41
42                 /* and grab new one */
43                 fd = bb_xopen(vtname, O_RDWR);
44
45                 /* Reassign stdout and sterr */
46                 dup2(fd, STDOUT_FILENO);
47                 dup2(fd, STDERR_FILENO);
48
49                 execvp(argv[2], &argv[2]);
50                 _exit(1);
51         }
52         return EXIT_SUCCESS;
53 }