File: screendump.c

package info (click to toggle)
kbd 2.0.4-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 14,136 kB
  • sloc: sh: 12,838; ansic: 12,531; pascal: 472; lex: 466; makefile: 394; yacc: 356; perl: 126; sed: 16
file content (163 lines) | stat: -rw-r--r-- 3,910 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * screendump.c - aeb 950214
 *
 * [Note: similar functionality can be found in setterm]
 *
 * Call: "screendump N" when the screen of /dev/ttyN has to be dumped
 *
 * On Linux up to 1.1.91 there is an ioctl that will do the dumping.
 * Because of problems with security this has been scrapped.
 * From 1.1.92 on, make devices "virtual console screen" and
 * "virtual console screen with attributes" by (fill in the ellipses):
 *	cd /dev
 *	for i in 0 1 2 3 ...; do
 *		mknod vcs$i c 7 $i
 *		mknod vcsa$i c 7 `expr 128 + $i`
 *	done
 * and give them your favourite owners and permissions.
 */
#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include "xmalloc.h"
#include "nls.h"
#include "version.h"
#include "kbd_error.h"

int main(int argc, char **argv)
{
	int cons = 0;
	char infile[20];
	unsigned char header[4];
	unsigned int rows, cols;
	int fd;
	unsigned int i, j;
	char *inbuf, *outbuf, *p, *q;

	set_progname(argv[0]);

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE_NAME, LOCALEDIR);
	textdomain(PACKAGE_NAME);

	if (argc == 2 && !strcmp(argv[1], "-V"))
		print_version_and_exit();

	if (argc > 2) {
		fprintf(stderr, _("usage: screendump [n]\n"));
		exit(EXIT_FAILURE);
	}

	cons = (argc == 2) ? atoi(argv[1]) : 0;

	sprintf(infile, "/dev/vcsa%d", cons);
	fd = open(infile, O_RDONLY);
	if (fd < 0 && cons == 0 && errno == ENOENT) {
		sprintf(infile, "/dev/vcsa");
		fd = open(infile, O_RDONLY);
	}
	if (fd < 0 && errno == ENOENT) {
		sprintf(infile, "/dev/vcs/a%d", cons);
		fd = open(infile, O_RDONLY);
	}
	if (fd < 0 && cons == 0 && errno == ENOENT) {
		sprintf(infile, "/dev/vcs/a");
		fd = open(infile, O_RDONLY);
	}
	if (fd < 0 || read(fd, header, 4) != 4)
		goto try_ioctl;
	rows = header[0];
	cols = header[1];
	if (rows * cols == 0)
		goto try_ioctl;
	inbuf  = xmalloc(rows * cols * 2);
	outbuf = xmalloc(rows * (cols + 1));

	if (read(fd, inbuf, rows * cols * 2) != (ssize_t)(rows * cols * 2)) {
		kbd_error(EXIT_FAILURE, errno, _("Error reading %s"), infile);
	}
	p = inbuf;
	q = outbuf;
	for (i = 0; i < rows; i++) {
		for (j = 0; j < cols; j++) {
			*q++ = *p;
			p += 2;
		}
		while (j-- > 0 && q[-1] == ' ')
			q--;
		*q++ = '\n';
	}
	goto done;

try_ioctl : {
	struct winsize win;
	char consnam[20], devfsconsnam[20];
	unsigned char *screenbuf;

	sprintf(consnam, "/dev/tty%d", cons);
	fd = open(consnam, O_RDONLY);
	if (fd < 0 && errno == ENOENT) {
		sprintf(devfsconsnam, "/dev/vc/%d", cons);
		fd = open(devfsconsnam, O_RDONLY);
		if (fd < 0)
			errno = ENOENT;
	}
	if (fd < 0) {
		perror(consnam);
		fd = 0;
	}

	if (ioctl(fd, TIOCGWINSZ, &win)) {
		kbd_error(EXIT_FAILURE, errno, "ioctl TIOCGWINSZ");
	}

	screenbuf    = xmalloc(2 + win.ws_row * win.ws_col);
	screenbuf[0] = 0;
	screenbuf[1] = (unsigned char)cons;

	if (ioctl(fd, TIOCLINUX, screenbuf) &&
	    (!fd || ioctl(0, TIOCLINUX, screenbuf))) {
#if 0
	    perror("TIOCLINUX");
	    fprintf(stderr,_("couldn't read %s, and cannot ioctl dump\n"),
		    infile);
#else
		/* we tried this just to be sure, but TIOCLINUX
	       function 0 has been disabled since 1.1.92
	       Do not mention `ioctl dump' in error msg */
		kbd_warning(0, _("couldn't read %s\n"), infile);
#endif
		return EXIT_FAILURE;
	}

	rows = screenbuf[0];
	cols = screenbuf[1];
	if (rows != win.ws_row || cols != win.ws_col) {
		kbd_error(EXIT_FAILURE, 0,
		          _("Strange ... screen is both %dx%d and %dx%d ??\n"),
		          win.ws_col, win.ws_row, cols, rows);
	}

	outbuf = xmalloc(rows * (cols + 1));
	p      = ((char *)screenbuf) + 2;
	q      = outbuf;
	for (i = 0; i < rows; i++) {
		for (j = 0; j < cols; j++)
			*q++ = *p++;
		while (j-- > 0 && (q[-1] == ' '))
			q--;
		*q++ = '\n';
	}
}
done:
	if (write(1, outbuf, q - outbuf) != q - outbuf) {
		kbd_error(EXIT_FAILURE, 0, _("Error writing screendump\n"));
	}

	return EXIT_SUCCESS;
}