ReactOS 0.4.15-dev-8231-g29a56f3
popen.c File Reference
#include <precomp.h>
#include <tchar.h>
Include dependency graph for popen.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define sT   "s"
 
#define MK_STR(s)   #s
 

Functions

int msvcrt_alloc_fd (HANDLE hand, int flag)
 
unsigned split_oflags (unsigned oflags)
 
void msvcrt_free_popen_data (void)
 
FILE_tpopen (const _TCHAR *cm, const _TCHAR *md)
 
int CDECL _pclose (FILE *file)
 

Variables

struct popen_handlepopen_handles = NULL
 
DWORD popen_handles_size = 0
 

Macro Definition Documentation

◆ MK_STR

#define MK_STR (   s)    #s

Definition at line 20 of file popen.c.

◆ sT

#define sT   "s"

Definition at line 17 of file popen.c.

Function Documentation

◆ _pclose()

int CDECL _pclose ( FILE file)

Definition at line 175 of file popen.c.

176{
177 HANDLE h;
178 DWORD i;
179
180 if (!MSVCRT_CHECK_PMT(file != NULL)) return -1;
181
183 for(i=0; i<popen_handles_size; i++)
184 {
185 if (popen_handles[i].f == file)
186 break;
187 }
188 if(i == popen_handles_size)
189 {
191 *_errno() = EBADF;
192 return -1;
193 }
194
195 h = popen_handles[i].proc;
196 popen_handles[i].f = NULL;
198
199 fclose(file);
201 {
203 CloseHandle(h);
204 return -1;
205 }
206
207 CloseHandle(h);
208 return i;
209}
#define EBADF
Definition: acclib.h:82
#define NULL
Definition: types.h:112
#define CloseHandle
Definition: compat.h:739
BOOL WINAPI GetExitCodeProcess(IN HANDLE hProcess, IN LPDWORD lpExitCode)
Definition: proc.c:1168
#define INFINITE
Definition: serial.h:102
unsigned long DWORD
Definition: ntddk_ex.h:95
GLfloat f
Definition: glext.h:7540
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:7723
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
#define _POPEN_LOCK
Definition: mtdll.h:53
#define _mlock(locknum)
Definition: mtdll.h:32
#define _munlock(locknum)
Definition: mtdll.h:33
#define MSVCRT_CHECK_PMT(x)
Definition: mbstowcs_s.c:26
void _dosmaperr(unsigned long oserrcode)
Definition: errno.c:79
_CRTIMP int *__cdecl _errno(void)
Definition: errno.c:17
DWORD popen_handles_size
Definition: popen.c:27
struct popen_handle * popen_handles
Definition: popen.c:26
Definition: fci.c:127
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition: synch.c:82
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define WAIT_FAILED
Definition: winbase.h:413

◆ _tpopen()

FILE * _tpopen ( const _TCHAR cm,
const _TCHAR md 
)

Definition at line 38 of file popen.c.

39{
40 _TCHAR *szCmdLine=NULL;
41 _TCHAR *szComSpec=NULL;
42 _TCHAR *s;
43 FILE *ret;
44 HANDLE hReadPipe, hWritePipe;
46 STARTUPINFO StartupInfo;
47 PROCESS_INFORMATION ProcessInformation;
49 struct popen_handle *container;
50 DWORD i;
51
52 TRACE(MK_STR(_tpopen)"('%"sT"', '%"sT"')\n", cm, md);
53
54 if (cm == NULL)
55 return NULL;
56
57 szComSpec = _tgetenv(_T("COMSPEC"));
58 if (szComSpec == NULL)
59 {
60 szComSpec = _T("cmd.exe");
61 }
62
63 s = max(_tcsrchr(szComSpec, '\\'), _tcsrchr(szComSpec, '/'));
64 if (s == NULL)
65 s = szComSpec;
66 else
67 s++;
68
69 szCmdLine = malloc((_tcslen(s) + 4 + _tcslen(cm) + 1) * sizeof(_TCHAR));
70 if (szCmdLine == NULL)
71 {
72 return NULL;
73 }
74
75 _tcscpy(szCmdLine, s);
76 s = _tcsrchr(szCmdLine, '.');
77 if (s)
78 *s = 0;
79 _tcscat(szCmdLine, _T(" /C "));
80 _tcscat(szCmdLine, cm);
81
82 if ( !CreatePipe(&hReadPipe,&hWritePipe,&sa,1024))
83 {
84 free (szCmdLine);
85 return NULL;
86 }
87
88 memset(&ProcessInformation, 0, sizeof(ProcessInformation));
89 memset(&StartupInfo, 0, sizeof(STARTUPINFO));
90 StartupInfo.cb = sizeof(STARTUPINFO);
91
92 if (*md == 'r' ) {
94 StartupInfo.hStdOutput = hWritePipe;
95 StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
96 }
97 else if ( *md == 'w' ) {
98 StartupInfo.hStdInput = hReadPipe;
100 StartupInfo.dwFlags |= STARTF_USESTDHANDLES;
101 }
102
103 if (StartupInfo.dwFlags & STARTF_USESTDHANDLES)
105
106 result = CreateProcess(szComSpec,
107 szCmdLine,
108 NULL,
109 NULL,
110 TRUE,
111 0,
112 NULL,
113 NULL,
114 &StartupInfo,
115 &ProcessInformation);
116 free (szCmdLine);
117
118 if (result == FALSE)
119 {
120 CloseHandle(hReadPipe);
121 CloseHandle(hWritePipe);
122 return NULL;
123 }
124
125 CloseHandle(ProcessInformation.hThread);
126
128 for(i=0; i<popen_handles_size; i++)
129 {
130 if (!popen_handles[i].f)
131 break;
132 }
134 {
137 if (!container) goto error;
138
143 }
145
146 if ( *md == 'r' )
147 {
148 ret = _tfdopen(msvcrt_alloc_fd(hReadPipe, split_oflags(_fmode)) , _T("r"));
149 CloseHandle(hWritePipe);
150 }
151 else
152 {
153 ret = _tfdopen( msvcrt_alloc_fd(hWritePipe, split_oflags(_fmode)) , _T("w"));
154 CloseHandle(hReadPipe);
155 }
156
157 container->f = ret;
158 container->proc = ProcessInformation.hProcess;
160
161 return ret;
162
163error:
165 if (ProcessInformation.hProcess != 0)
166 CloseHandle(ProcessInformation.hProcess);
167 return NULL;
168}
static struct sockaddr_in sa
Definition: adnsresfilter.c:69
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:203
#define md
Definition: compat-1.3.h:2013
#define realloc
Definition: debug_ros.c:6
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
struct _SECURITY_ATTRIBUTES SECURITY_ATTRIBUTES
unsigned int BOOL
Definition: ntddk_ex.h:94
GLdouble s
Definition: gl.h:2039
GLuint64EXT * result
Definition: glext.h:11304
_CRTIMP int _fmode
Definition: fmode.c:4
#define _tcscat
Definition: tchar.h:622
#define _tcscpy
Definition: tchar.h:623
#define _tfdopen
Definition: tchar.h:692
#define _tpopen
Definition: tchar.h:698
char _TCHAR
Definition: tchar.h:1392
#define _tgetenv
Definition: tchar.h:680
#define error(str)
Definition: mkdosfs.c:1605
#define _tcsrchr
Definition: utility.h:116
BOOL WINAPI CreatePipe(PHANDLE hReadPipe, PHANDLE hWritePipe, LPSECURITY_ATTRIBUTES lpPipeAttributes, DWORD nSize)
Definition: npipe.c:117
#define sT
Definition: popen.c:17
#define MK_STR(s)
Definition: popen.c:20
unsigned split_oflags(unsigned oflags)
Definition: file.c:1671
int msvcrt_alloc_fd(HANDLE hand, int flag)
Definition: file.c:343
#define memset(x, y, z)
Definition: compat.h:39
#define TRACE(s)
Definition: solgame.cpp:4
HANDLE hStdOutput
Definition: winbase.h:847
HANDLE hStdError
Definition: winbase.h:848
DWORD dwFlags
Definition: winbase.h:842
DWORD cb
Definition: winbase.h:831
HANDLE hStdInput
Definition: winbase.h:846
#define max(a, b)
Definition: svc.c:63
#define _T(x)
Definition: vfdio.h:22
int ret
#define CreateProcess
Definition: winbase.h:3758
#define STD_OUTPUT_HANDLE
Definition: winbase.h:268
#define STD_INPUT_HANDLE
Definition: winbase.h:267
STARTUPINFOA STARTUPINFO
Definition: winbase.h:3719
#define STD_ERROR_HANDLE
Definition: winbase.h:269
#define STARTF_USESTDHANDLES
Definition: winbase.h:499
#define _tcslen
Definition: xmlstorage.h:198

◆ msvcrt_alloc_fd()

int msvcrt_alloc_fd ( HANDLE  hand,
int  flag 
)

Definition at line 343 of file file.c.

344{
345 int fd;
347
348 TRACE(":handle (%p) allocating fd (%d)\n", hand, fd);
349
350 if(info == &__badioinfo)
351 return -1;
352
353 msvcrt_set_fd(info, hand, flag);
355 return fd;
356}
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean flag
Definition: glfuncs.h:52
void release_ioinfo(ioinfo *info)
Definition: file.c:263
static void msvcrt_set_fd(ioinfo *fdinfo, HANDLE hand, int flag)
Definition: file.c:325
ioinfo __badioinfo
Definition: file.c:126
static ioinfo * get_ioinfo_alloc(int *fd)
Definition: file.c:230
static int fd
Definition: io.c:51
Definition: file.c:40

Referenced by _open_osfhandle(), _pipe(), _tpopen(), and _wsopen_s().

◆ msvcrt_free_popen_data()

void msvcrt_free_popen_data ( void  )

Definition at line 29 of file popen.c.

30{
32}

Referenced by DllMain(), and msvcrt_init_exception().

◆ split_oflags()

unsigned split_oflags ( unsigned  oflags)

Definition at line 1671 of file file.c.

1672{
1673 int wxflags = 0;
1674 unsigned unsupp; /* until we support everything */
1675
1676 if (oflags & _O_APPEND) wxflags |= WX_APPEND;
1677 if (oflags & _O_BINARY) {/* Nothing to do */}
1678 else if (oflags & _O_TEXT) wxflags |= WX_TEXT;
1679 else if (oflags & _O_WTEXT) wxflags |= WX_TEXT;
1680 else if (oflags & _O_U16TEXT) wxflags |= WX_TEXT;
1681 else if (oflags & _O_U8TEXT) wxflags |= WX_TEXT;
1682 else if (*__p__fmode() & _O_BINARY) {/* Nothing to do */}
1683 else wxflags |= WX_TEXT; /* default to TEXT*/
1684 if (oflags & _O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1685
1686 if ((unsupp = oflags & ~(
1693 )))
1694 ERR(":unsupported oflags 0x%04x\n",unsupp);
1695
1696 return wxflags;
1697}
#define ERR(fmt,...)
Definition: debug.h:113
#define _O_SEQUENTIAL
Definition: cabinet.h:43
#define _O_RDWR
Definition: cabinet.h:39
#define _O_SHORT_LIVED
Definition: cabinet.h:49
#define _O_BINARY
Definition: cabinet.h:51
#define _O_NOINHERIT
Definition: cabinet.h:45
#define _O_TEMPORARY
Definition: cabinet.h:44
#define _O_APPEND
Definition: cabinet.h:41
#define _O_TEXT
Definition: cabinet.h:50
#define _O_TRUNC
Definition: cabinet.h:47
#define _O_CREAT
Definition: cabinet.h:46
#define _O_EXCL
Definition: cabinet.h:48
#define _O_RANDOM
Definition: cabinet.h:42
#define _O_WRONLY
Definition: cabinet.h:38
#define _O_WTEXT
Definition: fcntl.h:20
#define _O_U8TEXT
Definition: fcntl.h:22
#define _O_U16TEXT
Definition: fcntl.h:21
int * __p__fmode(void)
Definition: fmode.c:9
#define WX_DONTINHERIT
Definition: file.c:97
#define WX_TEXT
Definition: file.c:100
#define WX_APPEND
Definition: file.c:98

Referenced by _open_osfhandle(), _pipe(), _tpopen(), and _wsopen_s().

Variable Documentation

◆ popen_handles

struct popen_handle* popen_handles = NULL

Definition at line 26 of file popen.c.

Referenced by _pclose(), _tpopen(), and msvcrt_free_popen_data().

◆ popen_handles_size

DWORD popen_handles_size = 0

Definition at line 27 of file popen.c.

Referenced by _pclose(), and _tpopen().