+ in the interest of robustness, I added
[oweals/busybox.git] / deallocvt.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * disalloc.c - aeb - 940501 - Disallocate virtual terminal(s)
4  * Renamed deallocvt.
5  */
6 #include "internal.h"
7 #include <stdlib.h>
8 #include <fcntl.h>
9 #include <sys/types.h>
10 #include <sys/ioctl.h>
11 #include <linux/vt.h>
12 #include <stdio.h>
13
14 extern int getfd(void);
15 char *progname;
16
17 int deallocvt_main(int argc, char *argv[])
18 {
19         int fd, num, i;
20
21         if ((argc != 2) || (**(argv + 1) == '-')) {
22                 usage
23                         ("deallocvt N\n\nDeallocate unused virtual terminal /dev/ttyN\n");
24         }
25
26         progname = argv[0];
27
28         fd = get_console_fd("/dev/console");
29
30         if (argc == 1) {
31                 /* deallocate all unused consoles */
32                 if (ioctl(fd, VT_DISALLOCATE, 0)) {
33                         perror("VT_DISALLOCATE");
34                         exit(1);
35                 }
36         } else
37                 for (i = 1; i < argc; i++) {
38                         num = atoi(argv[i]);
39                         if (num == 0)
40                                 fprintf(stderr, "%s: 0: illegal VT number\n", progname);
41                         else if (num == 1)
42                                 fprintf(stderr, "%s: VT 1 cannot be deallocated\n",
43                                                 progname);
44                         else if (ioctl(fd, VT_DISALLOCATE, num)) {
45                                 perror("VT_DISALLOCATE");
46                                 fprintf(stderr, "%s: could not deallocate console %d\n",
47                                                 progname, num);
48                                 exit(1);
49                         }
50                 }
51         exit(0);
52 }