#include #include #include int ctlfd=-1; int loop = 1; long n=0; int fs=1024*1024; Biobuf *b; char *buf; char c; void rfile(char *file, Biobuf *b, char *buf) { if((b=Bopen(file, OREAD)) == nil) sysfatal("Bopen: %r"); n=Bread(b, buf, fs-1); buf[n]=0; Bterm(b); } void wfile(char *file, Biobuf *b, char *buf) { if((b=Bopen(file, OWRITE | OTRUNC)) == nil) sysfatal("Bopen: %r"); if((n=Bwrite(b, buf, fs)) < 0) sysfatal("Write file: %r"); Bterm(b); } void main(int argc, char *argv[]) { /* validate args */ if(argc != 2) sysfatal("usage: %s file", argv[0]); /* go into raw mode */ if((ctlfd=open("/dev/consctl", OWRITE)) < 0) sysfatal("Open consctl: %r"); if(write(ctlfd, "rawon", 5) != 5) sysfatal("Rawmode: %r"); /* allocate file buffer */ buf=malloc(fs); /* open the file and read it into the buffer then close again */ rfile(argv[1], b, buf); /* open /dev/text and write file content to it then close*/ wfile("/dev/text", b, buf); /* main loop */ while(loop) { read(0, &c, 1); switch(c) { case 0x11: /* code for ctrl+q */ loop = 0; break; case 'w': /* open /dev/text for reading and read content to buffer */ rfile("/dev/text", b, buf); /* open file and write buffer to it */ wfile(argv[1], b, buf); break; case 'q': loop = 0; break; } } free(buf); close(ctlfd); exits(nil); }