File: screen.c

package info (click to toggle)
vlock 1.3-5.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 168 kB
  • ctags: 45
  • sloc: ansic: 433; makefile: 64; sh: 44
file content (65 lines) | stat: -rw-r--r-- 1,145 bytes parent folder | download | duplicates (6)
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include "vlock.h"

static unsigned char lines, columns;
static void *screen_buf = NULL;
static int vcs;

void clear_screen(void)
{
	struct stat s_buf;
	int i;
	char path[16];

	if (fstat(0, &s_buf)==-1) {
		return;
	}
	if (!S_ISCHR(s_buf.st_mode)) {
		return;
	}
	if ((s_buf.st_rdev / 256) != 4) {
		return;
	}
	i = s_buf.st_rdev % 256;
	sprintf(path, "/dev/vcsa%d", i);
	vcs = open(path, O_RDWR);
	if (vcs<0) {
		return;
	}
	if (read(vcs, &columns, 1)!=1) {
		close(vcs);
		return;
	}
	if (read(vcs, &lines, 1)!=1) {
		close(vcs);
		return;
	}
	screen_buf = malloc(2*lines*columns+2);
	if (!screen_buf) {
		close(vcs);
		return;
	}
	if (read(vcs, screen_buf, 2*lines*columns+2) != 2*lines*columns+2) {
		free(screen_buf);
		screen_buf=NULL;
		close(vcs);
		return;
	}
	write(1, "\33[H\33[J", 6);	/* There's no need to use ncurses */
	fflush(stdout);
}

void restore_screen(void)
{
	if (screen_buf) {
		lseek(vcs, 0, SEEK_SET);
		write(vcs, &columns, 1);
		write(vcs, &lines, 1);
		write(vcs, screen_buf, 2*lines*columns+2);
		close(vcs);
	}
}