README: acknowledge added.
[vde.git] / vde-2 / unixterm.c
blob970cd2732559392218a734299e0e59bc9547c73f
1 /* Copyright 2005 Renzo Davoli VDE-2
2 * Licensed under the GPLv2
4 * Minimal terminal emulator on a UNIX stream socket
5 */
7 #include <stdio.h>
8 #include <fcntl.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <sys/poll.h>
12 #include <sys/socket.h>
13 #include <sys/un.h>
15 #include <vde.h>
17 #define BUFSIZE 1024
18 char buf[BUFSIZE];
20 int main(int argc,char *argv[])
22 struct sockaddr_un sun;
23 int fd;
24 int rv;
25 static struct pollfd pfd[]={
26 {STDIN_FILENO,POLLIN | POLLHUP,0},
27 {STDIN_FILENO,POLLIN | POLLHUP,0}};
28 static int fileout[]={STDOUT_FILENO,STDOUT_FILENO};
29 sun.sun_family=PF_UNIX;
30 snprintf(sun.sun_path,sizeof(sun.sun_path),"%s",argv[1]);
31 fd=socket(PF_UNIX,SOCK_STREAM,0);
32 rv=connect(fd,(struct sockaddr *)(&sun),sizeof(sun));
33 pfd[1].fd=fileout[0]=fd;
34 while(1) {
35 int m,i,n=poll(pfd,2,-1);
36 for(i=0;n>0;i++) {
37 if(pfd[i].revents & POLLHUP)
38 exit(0);
39 if(pfd[i].revents & POLLIN) {
40 n--;
41 if((m=read(pfd[i].fd,buf,BUFSIZE)) == 0)
42 exit(0);
43 write(fileout[i],buf,m);