aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2014-06-24 06:42:08 -0500
committerRob Landley <rob@landley.net>2014-06-24 06:42:08 -0500
commitd3cd45babc2e3bc72b53c08e48fac7f94887b72b (patch)
tree13dd2ae9977a7cd53b5d096e80c831bb9ab09d75
parentfc49761a9c992a120a27cca016d6a3ed31b1f3d3 (diff)
downloadtoybox-d3cd45babc2e3bc72b53c08e48fac7f94887b72b.tar.gz
Move deallocvt.c into openvt.c, cleanup both.
-rw-r--r--toys/pending/deallocvt.c37
-rw-r--r--toys/pending/openvt.c56
2 files changed, 35 insertions, 58 deletions
diff --git a/toys/pending/deallocvt.c b/toys/pending/deallocvt.c
deleted file mode 100644
index a8067d59..00000000
--- a/toys/pending/deallocvt.c
+++ /dev/null
@@ -1,37 +0,0 @@
-/* deallocvt.c - Deallocate virtual terminal(s)
- *
- * Copyright 2014 Vivek Kumar Bhagat <vivek.bhagat89@gmail.com>
- *
- * No Standard.
-
-USE_DEALLOCVT(NEWTOY(deallocvt, ">1", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_NEEDROOT))
-
-config DEALLOCVT
- bool "deallocvt"
- depends on OPENVT
- default n
- help
- usage: deallocvt [N]
-
- Deallocate unused virtual terminal /dev/ttyN
- default value of N is 0, deallocate all unused consoles
-*/
-
-#include "toys.h"
-#include <linux/vt.h>
-
-void deallocvt_main(void)
-{
- int fd;
-
- // 0 : deallocate all unused consoles
- int vt_num = 0;
-
- if (toys.optargs[0])
- vt_num = atolx_range(toys.optargs[0], 1, 63);
-
- fd = find_console_fd();
- if (fd < 0) error_exit("can't open console");
-
- xioctl(fd, VT_DISALLOCATE, (void *)(ptrdiff_t)vt_num);
-}
diff --git a/toys/pending/openvt.c b/toys/pending/openvt.c
index be3070e2..0eb0c25a 100644
--- a/toys/pending/openvt.c
+++ b/toys/pending/openvt.c
@@ -5,20 +5,29 @@
* No Standard
USE_OPENVT(NEWTOY(openvt, "c#<1>63sw", TOYFLAG_BIN|TOYFLAG_NEEDROOT))
+USE_DEALLOCVT(NEWTOY(deallocvt, ">1", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_NEEDROOT))
config OPENVT
bool "openvt"
default n
help
- usage: openvt [-c N] [-s] [-w] [--] [command [command_options]]
+ usage: openvt [-c N] [-sw] [command [command_options]]
start a program on a new virtual terminal (VT)
-c N Use VT N
-s Switch to new VT
-w Wait for command to exit
- if -s and -w option used together, switch back
- to originating VT when command completes
+
+ if -sw used together, switch back to originating VT when command completes
+
+config DEALLOCVT
+ bool "deallocvt"
+ default n
+ help
+ usage: deallocvt [N]
+
+ Deallocate unused virtual terminal /dev/ttyN, or all unused consoles.
*/
#define FOR_openvt
@@ -30,32 +39,24 @@ GLOBALS(
unsigned long vt_num;
)
-int find_console_fd(void)
+int open_console(void)
{
- char *console_name[] = {"/dev/tty", "/dev/tty0", "/dev/console"};
- int i;
- int fd;
- char arg;
-
- for (i = 0; i < 3; i++) {
- fd = open(console_name[i], O_RDONLY);
- if (fd < 0 && errno == EACCES)
- fd = open(console_name[i], O_WRONLY);
+ char arg, *console_name[] = {"/dev/tty", "/dev/tty0", "/dev/console"};
+ int i, fd;
+ for (i = 0; i < ARRAY_LEN(console_name); i++) {
+ fd = open(console_name[i], O_RDWR);
if (fd >= 0) {
arg = 0;
- if (0 == ioctl(fd, KDGKBTYPE, &arg))
- return fd;
- else
- close(fd);
+ if (!ioctl(fd, KDGKBTYPE, &arg)) return fd;
+ close(fd);
}
}
/* check std fd 0, 1 and 2 */
for (fd = 0; fd < 3; fd++) {
arg = 0;
- if (0 == ioctl(fd, KDGKBTYPE, &arg))
- return fd;
+ if (0 == ioctl(fd, KDGKBTYPE, &arg)) return fd;
}
return -1;
@@ -73,8 +74,9 @@ int xvtnum(int fd)
void openvt_main(void)
{
- int fd = -1, vt_fd = -1, pid, ret = 0;
+ int fd, vt_fd, ret = 0;
struct vt_stat vstate;
+ pid_t pid;
if (!(toys.optflags & FLAG_c)) {
// check if fd 0,1 or 2 is already opened
@@ -93,7 +95,7 @@ void openvt_main(void)
}
sprintf(toybuf, "/dev/tty%lu", TT.vt_num);
- fd = find_console_fd();
+ fd = open_console();
xioctl(fd, VT_GETSTATE, &vstate);
close(0); //new vt becomes stdin
@@ -128,3 +130,15 @@ void openvt_main(void)
}
}
}
+
+void deallocvt_main(void)
+{
+ long vt_num = 0; // 0 deallocates all unused consoles
+ int fd;
+
+ if (*toys.optargs) vt_num = atolx_range(*toys.optargs, 1, 63);
+
+ if ((fd = open_console()) < 0) error_exit("can't open console");
+ xioctl(fd, VT_DISALLOCATE, (void *)vt_num);
+ if (CFG_TOYBOX_FREE) close(fd);
+}