0239fec948548d780807119f1d4c78b5d7c0234e
[vsys.git] / vsyssh / vsyssh.c
1 /* gcc -Wall -O2 -g chpid.c -o chpid */
2 #define _XOPEN_SOURCE
3 #define _XOPEN_SOURCE_EXTENDED
4 #define _SVID_SOURCE
5 #define _GNU_SOURCE
6 #include <stdio.h>
7 #include <errno.h>
8 #include <string.h>
9 #include <signal.h>
10 #include <stdlib.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <sys/syscall.h>
14 #include <sys/wait.h>
15 #include <sys/time.h>
16 #include <sys/select.h>
17 #include <sys/resource.h>
18 #include <sys/mount.h>
19 #include <sys/vfs.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <sched.h>
23 #include <stdarg.h>
24 #include <dirent.h>
25
26 void pipe_handler (int sig) {
27         printf("SIGPIPE");
28 }
29
30 int main(int argc, char **argv, char **envp)
31 {
32         if (argc<2) {
33                 printf("Usage: vsyssh <vsys entry> [cmd]\n");
34                 exit(1);
35         }
36         else {
37                 int vfd0,vfd1;
38                 char *inf,*outf;
39                 struct timeval tv;
40
41                 signal(SIGPIPE,pipe_handler);
42                 inf=(char *)malloc(strlen(argv[1])+3);
43                 outf=(char *)malloc(strlen(argv[1])+4);
44                 strcpy(inf,argv[1]);
45                 strcpy(outf,argv[1]);
46                 strcat(inf,".in");
47                 strcat(outf,".out");
48
49                 vfd0 = open(outf,O_RDONLY|O_NONBLOCK);
50                 printf("Out file: %d\n",vfd0);
51                 vfd1 = open(inf,O_WRONLY);
52                 printf("In file: %d\n",vfd1);
53
54                 if (vfd0==-1 || vfd1 == -1) {
55                         printf("Error opening vsys entry %s (%s)\n", argv[1],strerror(errno));
56                         exit(1);
57                 }
58
59                 if (argc<3) {
60                         fd_set set;
61                         FD_ZERO(&set);
62                         FD_SET(0, &set);
63                         FD_SET(vfd0, &set);
64
65                         while (1)
66                          {
67                                 int ret;
68                                 printf("vsys>");fflush(stdout);
69                                 FD_SET(0, &set);
70                                 FD_SET(vfd0, &set);
71                                 ret = select(vfd0+1, &set, NULL, NULL, NULL);
72                                 if (FD_ISSET(0,&set)) {
73                                         char lineread[2048];
74                                         int ret;
75                                         ret=read(0,lineread,2048);
76                                         lineread[ret]='\0';
77                                         printf ("writing %s\n",lineread);
78                                         write(vfd1,lineread,ret);
79                                         FD_CLR(0,&set);
80                                 } if (FD_ISSET(vfd0,&set)) {
81                                         char lineread[2048];
82                                         int ret;
83                                         ret=read(vfd0,lineread,2048);
84                                         write(1,lineread,ret);
85                                         FD_CLR(vfd0,&set);
86                                 }
87                         }
88
89                 }
90                 else {
91                         close(0);
92                         close(1);
93
94                         dup2(vfd0,0);
95                         dup2(vfd1,1);
96                         execve(argv[3],argv+3,envp);
97                 }
98        }
99
100        return;
101
102 }