b77d7f2a7c54ef7349e4bba32f6ddcc8fa201389
[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
43                 vfd0 = open(outf,O_RDONLY|O_NONBLOCK);
44                 printf("Out file: %d\n",vfd0);
45                 vfd1 = open(inf,O_WRONLY);
46                 printf("In file: %d\n",vfd1);
47
48                 if (vfd0==-1 || vfd1 == -1) {
49                         printf("Error opening vsys entry %s (%s)\n", argv[1],strerror(errno));
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                                 FD_SET(0, &set);
63                                 FD_SET(vfd0, &set);
64                                 ret = select(vfd0+1, &set, NULL, NULL, NULL);
65                                 if (FD_ISSET(0,&set)) {
66                                         char lineread[2048];
67                                         int ret;
68                                         ret=read(0,lineread,2048);
69                                         write(vfd1,lineread,ret);
70                                         FD_CLR(0,&set);
71                                 } if (FD_ISSET(vfd0,&set)) {
72                                         char lineread[2048];
73                                         int ret;
74                                         ret=read(vfd0,lineread,2048);
75                                         write(1,lineread,ret);
76                                         FD_CLR(vfd0,&set);
77                                 }
78                         }
79
80                 }
81                 else {
82                         close(0);
83                         close(1);
84
85                         dup2(vfd0,0);
86                         dup2(vfd1,1);
87                         execve(argv[3],argv+3,envp);
88                 }
89        }
90
91        return;
92
93 }