586c6a7dadb3c8afe8f8604cee7208d54c5ce8c9
[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 <stdlib.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <sys/syscall.h>
13 #include <sys/wait.h>
14 #include <sys/time.h>
15 #include <sys/select.h>
16 #include <sys/resource.h>
17 #include <sys/mount.h>
18 #include <sys/vfs.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <sched.h>
22 #include <stdarg.h>
23 #include <dirent.h>
24
25 int main(int argc, char **argv, char **envp)
26 {
27         if (argc<2) {
28                 printf("Usage: vsyssh <vsys entry> [cmd]\n");
29                 exit(1);
30         }
31         else {
32                 int vfd0,vfd1;
33                 char *inf,*outf;
34                 struct timeval tv;
35
36                 inf=(char *)malloc(strlen(argv[1])+3);
37                 outf=(char *)malloc(strlen(argv[1])+4);
38                 strcpy(inf,argv[1]);
39                 strcpy(outf,argv[1]);
40                 strcat(inf,".in");
41                 strcat(outf,".out");
42                 tv.tv_sec = 100;
43                 tv.tv_usec = 0;
44
45                 vfd1 = open(inf,O_WRONLY|O_NONBLOCK);
46                 vfd0 = open(outf,O_RDONLY|O_NONBLOCK);
47
48                 if (vfd0==-1 || vfd1 == -1) {
49                         printf("Error opening vsys entry %s\n", argv[1]);
50                         exit(1);
51                 }
52
53                 if (argc<3) {
54                         fd_set set;
55                         FD_ZERO(&set);
56                         FD_SET(0, &set);
57                         FD_SET(vfd0, &set);
58
59                         while (1) {
60                                 int ret;
61                                 printf("vsys>");fflush(stdout);
62                                 ret = select(vfd0+1, &set, NULL, NULL, &tv);
63                                 FD_SET(0, &set);
64                                 FD_SET(vfd0, &set);
65                                 if (FD_ISSET(0,&set)) {
66                                         char lineread[2048];
67                                         int ret;
68                                         printf("Here\n");
69                                         ret=read(0,lineread,2048);
70                                         write(vfd1,lineread,ret);
71                                         FD_CLR(0,&set);
72                                 }
73                                 if (FD_ISSET(vfd0,&set)) {
74                                         char lineread[2048];
75                                         int ret;
76                                         printf("Here2\n");
77                                         ret=read(vfd0,lineread,2048);
78                                         write(1,lineread,ret);
79                                         printf("Here3\n");
80                                         FD_CLR(vfd0,&set);
81                                 }
82                         }
83
84                 }
85                 else {
86                         close(0);
87                         close(1);
88
89                         dup2(vfd0,0);
90                         dup2(vfd1,1);
91                         execve(argv[3],argv+3,envp);
92                 }
93        }
94
95        return;
96
97 }