2 Copyright (C) 2006-2008 Canonical Ltd.
4 clear_console and it's man page are free software; you can redistribute it
5 and/or modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2, or (at your
7 option) any later version.
17 #include <sys/ioctl.h>
21 #elif defined(__FreeBSD_kernel__)
22 #include <sys/consio.h>
36 fprintf(stderr, "Usage: %s [option]\n", progname);
37 fprintf(stderr, "valid options are:\n");
38 fprintf(stderr, "\t-q --quiet don't print error messages\n");
39 fprintf(stderr, "\t-h --help display this help text and exit\n");
40 fprintf(stderr, "\t-V --version display version information and exit\n");
43 const struct option opts[] =
46 {"help", no_argument, 0, 'h'},
47 {"version", no_argument, 0, 'V'},
48 {"quiet", no_argument, 0, 'q'},
52 static int putch(int c)
58 /* taken from console-utils, lib/misc-console-utils.c */
60 int is_a_console(int fd)
62 #if defined(__linux__)
64 #elif defined(__FreeBSD_kernel__)
69 return (ioctl(fd, KDGKBTYPE, &arg) == 0
70 && ((arg == KB_OTHER) || (arg == KB_101) || (arg == KB_84)));
73 static int open_a_console(char *fnam)
78 fd = open(fnam, O_RDWR);
80 /* if failed, try read-only */
81 if (fd < 0 && errno == EACCES)
82 fd = open(fnam, O_RDONLY);
84 /* if failed, try write-only */
85 if (fd < 0 && errno == EACCES)
86 fd = open(fnam, O_WRONLY);
92 /* if not a console, fail */
93 if (! is_a_console(fd))
104 * Get an fd for use with kbd/console ioctls.
105 * We try several things because opening /dev/console will fail
106 * if someone else used X (which does a chown on /dev/console).
108 * if tty_name is non-NULL, try this one instead.
111 int get_console_fd(char* tty_name)
117 if (-1 == (fd = open_a_console(tty_name)))
123 fd = open_a_console("/dev/tty");
127 fd = open_a_console("/dev/tty0");
131 fd = open_a_console("/dev/console");
135 for (fd = 0; fd < 3; fd++)
136 if (is_a_console(fd))
141 _("Couldnt get a file descriptor referring to the console\n"));
143 return -1; /* total failure */
147 int is_pseudo_tty(int fd)
149 char *tty = ttyname(fd);
158 if (strlen(tty) >= 9 && !strncmp(tty, "/dev/pts/", 9))
161 if (strlen(tty) >= 8 && !strncmp(tty, "/dev/tty", 8)
162 && tty[8] >= 'a' && tty[8] <= 'z')
168 int clear_console(int fd)
171 #if defined(__linux__)
172 struct vt_stat vtstat;
176 setupterm((char *) 0, 1, (int *) 0);
177 if (tputs(clear_screen, lines > 0 ? lines : 1, putch) == ERR)
182 if (is_pseudo_tty(STDIN_FILENO))
185 if (!strcmp(getenv("TERM"), "screen"))
189 #if defined(__linux__)
190 if (ioctl(fd, VT_GETSTATE, &vtstat) < 0)
191 #elif defined(__FreeBSD_kernel__)
192 if (ioctl(fd, VT_ACTIVATE, &num) < 0)
196 fprintf(stderr, "%s: cannot get VTstate\n", progname);
199 #if defined(__linux__)
200 num = vtstat.v_active;
202 tmp_num = (num == 1 ? 2 : 1);
204 /* switch vt to clear the scrollback buffer */
205 if (ioctl(fd, VT_ACTIVATE, tmp_num))
208 perror("chvt: VT_ACTIVATE");
212 if (ioctl(fd, VT_WAITACTIVE, tmp_num))
215 perror("VT_WAITACTIVE");
220 if (ioctl(fd, VT_ACTIVATE, num))
223 perror("chvt: VT_ACTIVATE");
227 if (ioctl(fd, VT_WAITACTIVE, num))
230 perror("VT_WAITACTIVE");
236 int main (int argc, char* argv[])
239 int result; /* option handling */
242 if ((progname = strrchr(argv[0], '/')) == NULL)
249 result = getopt_long(argc, argv, "Vhq", opts, &an_option);
257 fprintf(stdout, "%s: Version %s\n", progname, VERSION);
271 fprintf(stderr, "%s: no non-option arguments are valid", progname);
275 if ((fd = get_console_fd(NULL)) == -1)
278 fprintf(stderr, "%s: terminal is not a console\n", progname);