2 * screendump.c - aeb 950214
4 * [Note: similar functionality can be found in setterm]
6 * Call: "screendump N" when the screen of /dev/ttyN has to be dumped
8 * On Linux up to 1.1.91 there is an ioctl that will do the dumping.
9 * Because of problems with security this has been scrapped.
10 * From 1.1.92 on, make devices "virtual console screen" and
11 * "virtual console screen with attributes" by (fill in the ellipses):
13 * for i in 0 1 2 3 ...; do
15 * mknod vcsa$i c 7 `expr 128 + $i`
17 * and give them your favourite owners and permissions.
24 #include <sys/ioctl.h>
30 main(int argc, char **argv) {
33 unsigned char header[4];
34 unsigned int rows, cols;
37 char *inbuf, *outbuf, *p, *q;
39 set_progname(argv[0]);
41 setlocale(LC_ALL, "");
42 bindtextdomain(PACKAGE_NAME, LOCALEDIR);
43 textdomain(PACKAGE_NAME);
45 if (argc == 2 && !strcmp(argv[1], "-V"))
46 print_version_and_exit();
49 fprintf(stderr, _("usage: screendump [n]\n"));
53 cons = (argc == 2) ? atoi(argv[1]) : 0;
55 sprintf(infile, "/dev/vcsa%d", cons);
56 fd = open(infile, O_RDONLY);
57 if (fd < 0 && cons == 0 && errno == ENOENT) {
58 sprintf(infile, "/dev/vcsa");
59 fd = open(infile, O_RDONLY);
61 if (fd < 0 && errno == ENOENT) {
62 sprintf(infile, "/dev/vcs/a%d", cons);
63 fd = open(infile, O_RDONLY);
65 if (fd < 0 && cons == 0 && errno == ENOENT) {
66 sprintf(infile, "/dev/vcs/a");
67 fd = open(infile, O_RDONLY);
69 if (fd < 0 || read(fd, header, 4) != 4)
75 inbuf = xmalloc(rows*cols*2);
76 outbuf = xmalloc(rows*(cols+1));
78 if (read(fd, inbuf, rows*cols*2) != (ssize_t) (rows*cols*2)) {
79 fprintf(stderr, _("Error reading %s\n"), infile);
84 for(i=0; i<rows; i++) {
85 for(j=0; j<cols; j++) {
89 while(j-- > 0 && q[-1] == ' ')
98 char consnam[20], devfsconsnam[20];
99 unsigned char *screenbuf;
101 sprintf(consnam, "/dev/tty%d", cons);
102 fd = open(consnam, O_RDONLY);
103 if (fd < 0 && errno == ENOENT) {
104 sprintf(devfsconsnam, "/dev/vc/%d", cons);
105 fd = open(devfsconsnam, O_RDONLY);
114 if (ioctl(fd, TIOCGWINSZ, &win)) {
115 perror("TIOCGWINSZ");
119 screenbuf = xmalloc(2 + win.ws_row * win.ws_col);
121 screenbuf[1] = (unsigned char) cons;
123 if (ioctl(fd,TIOCLINUX,screenbuf) &&
124 (!fd || ioctl(0,TIOCLINUX,screenbuf))) {
127 fprintf(stderr,_("couldn't read %s, and cannot ioctl dump\n"),
130 /* we tried this just to be sure, but TIOCLINUX
131 function 0 has been disabled since 1.1.92
132 Do not mention `ioctl dump' in error msg */
133 fprintf(stderr,_("couldn't read %s\n"), infile);
140 if (rows != win.ws_row || cols != win.ws_col) {
142 _("Strange ... screen is both %dx%d and %dx%d ??\n"),
143 win.ws_col, win.ws_row, cols, rows);
147 outbuf = xmalloc(rows*(cols+1));
148 p = ((char *)screenbuf) + 2;
150 for (i=0; i<rows; i++) {
151 for (j=0; j<cols; j++)
153 while (j-- > 0 && (q[-1] == ' '))
159 if (write(1, outbuf, q-outbuf) != q-outbuf) {
160 fprintf(stderr, _("Error writing screendump\n"));