c24c3788ec66b802bce03232cedd324cd9002e83
[reactos.git] / lib / crtdll / stdio / popen.c
1
2 #include <windows.h>
3 #include <msvcrt/io.h>
4 #include <msvcrt/errno.h>
5 #include <msvcrt/stdio.h>
6 #include <msvcrt/stdlib.h>
7 #include <msvcrt/string.h>
8 #include <msvcrt/internal/file.h>
9
10
11 FILE *_popen (const char *cm, const char *md) /* program name, pipe mode */
12 {
13   FILE *pf;
14   HANDLE hReadPipe, hWritePipe;
15   STARTUPINFO StartupInfo;
16   PROCESS_INFORMATION ProcessInformation;
17
18   // fixme CreatePipe
19
20 //  if ( !CreatePipe(&hReadPipe,&hWritePipe,NULL,1024))
21 //              return NULL;    
22
23   StartupInfo.cb = sizeof(STARTUPINFO);
24   if ( md == "r" ) {
25         StartupInfo.hStdOutput = hWritePipe;
26   }
27   else if ( md == "w" ) {
28         StartupInfo.hStdInput = hReadPipe;
29   }
30         
31   if (CreateProcessA("cmd.exe",(char *)cm,NULL,NULL,TRUE,
32                      CREATE_NEW_CONSOLE,NULL,NULL,
33                      &StartupInfo,
34                      &ProcessInformation) == FALSE)
35     return NULL;
36
37
38   if ( *md == 'r' ) {
39         pf =  _fdopen( __fileno_alloc(hReadPipe,  _fmode) , "r" );
40   }
41   else {
42         pf =  _fdopen( __fileno_alloc(hWritePipe, _fmode) , "w" );
43   }
44
45   pf->_name_to_remove = ProcessInformation.hProcess;
46
47   return pf;
48         
49
50 }
51
52
53 int
54 _pclose (FILE *pp)
55 {
56         
57         fclose(pp);
58         printf("Terminate Process\n");
59 //      if (!TerminateProcess(pp->_name_to_remove,0))
60 //              return -1;
61         return 0;
62 }