- fixed poll emulation management
[vde.git] / vde-2 / unixterm / unixterm.c
blobfba29c7d6726c8e437594b7114da51796aecb5f9
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 "compat/poll.h"
12 #include <sys/socket.h>
13 #include <sys/un.h>
15 #include <config.h>
17 #include <vde.h>
19 #define BUFSIZE 1024
20 char buf[BUFSIZE];
22 int main(int argc,char *argv[])
24 struct sockaddr_un sun;
25 int fd;
26 int rv;
27 static struct pollfd pfd[]={
28 {STDIN_FILENO,POLLIN | POLLHUP,0},
29 {STDIN_FILENO,POLLIN | POLLHUP,0}};
30 static int fileout[]={STDOUT_FILENO,STDOUT_FILENO};
31 sun.sun_family=PF_UNIX;
32 snprintf(sun.sun_path,sizeof(sun.sun_path),"%s",argv[1]);
33 if((fd=socket(PF_UNIX,SOCK_STREAM,0))<0) {
34 perror("Socket opening error");
35 exit(-1);
37 if ((rv=connect(fd,(struct sockaddr *)(&sun),sizeof(sun))) < 0) {
38 perror("Socket connecting error");
39 exit(-1);
41 pfd[1].fd=fileout[0]=fd;
42 while(1) {
43 int m,i,n=poll(pfd,2,-1);
44 for(i=0;n>0;i++) {
45 if(pfd[i].revents & POLLHUP)
46 exit(0);
47 if(pfd[i].revents & POLLIN) {
48 n--;
49 if((m=read(pfd[i].fd,buf,BUFSIZE)) == 0)
50 exit(0);
51 write(fileout[i],buf,m);