[NTDLL]
[reactos.git] / reactos / base / applications / network / net / process.c
1
2 /*
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS net command
5 * FILE:
6 * PURPOSE:
7 *
8 * PROGRAMMERS: Magnus Olsen (greatlord@reactos.org)
9 */
10
11 #include "net.h"
12
13 BOOL myCreateProcessStartGetSzie(CHAR *cmdline, LONG *size)
14 {
15 HANDLE hChildStdinRd;
16 HANDLE hChildStdinWr;
17 HANDLE hChildStdoutRd;
18 HANDLE hChildStdoutWr;
19 SECURITY_ATTRIBUTES saAttr;
20
21 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
22 saAttr.bInheritHandle = TRUE;
23 saAttr.lpSecurityDescriptor = NULL;
24
25 if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
26 {
27 return FALSE;
28 }
29
30 if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
31 {
32 return FALSE;
33 }
34
35 myCreateProcess(hChildStdoutWr, hChildStdinRd,"rpcclient -c \"service enum\"");
36 *size = ReadPipeSize(hChildStdoutWr, hChildStdoutRd);
37 return TRUE;
38 }
39
40 BOOL myCreateProcessStart(CHAR *cmdline, CHAR *srvlst, LONG size)
41 {
42 HANDLE hChildStdinRd;
43 HANDLE hChildStdinWr;
44 HANDLE hChildStdoutRd;
45 HANDLE hChildStdoutWr;
46 SECURITY_ATTRIBUTES saAttr;
47
48 saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
49 saAttr.bInheritHandle = TRUE;
50 saAttr.lpSecurityDescriptor = NULL;
51
52 if (! CreatePipe(&hChildStdoutRd, &hChildStdoutWr, &saAttr, 0))
53 {
54 return FALSE;
55 }
56
57 if (! CreatePipe(&hChildStdinRd, &hChildStdinWr, &saAttr, 0))
58 {
59 return FALSE;
60 }
61
62 myCreateProcess(hChildStdoutWr, hChildStdinRd,"rpcclient -c \"service enum\"");
63
64 return ReadPipe(hChildStdoutWr, hChildStdoutRd, srvlst, size);
65 }
66
67 BOOL myCreateProcess(HANDLE hStdoutWr, HANDLE hStdinRd, CHAR *cmdline)
68 {
69 PROCESS_INFORMATION piProcInfo;
70 STARTUPINFO siStartInfo;
71 BOOL status = FALSE;
72
73 ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
74 ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
75 siStartInfo.cb = sizeof(STARTUPINFO);
76 siStartInfo.hStdError = hStdoutWr;
77 siStartInfo.hStdOutput = hStdoutWr;
78 siStartInfo.hStdInput = hStdinRd;
79 siStartInfo.dwFlags |= STARTF_USESTDHANDLES;
80 status = CreateProcess(NULL, cmdline, NULL, NULL,
81 TRUE, 0, NULL, NULL, &siStartInfo, &piProcInfo);
82
83 if (status != 0)
84 {
85 CloseHandle(piProcInfo.hProcess);
86 CloseHandle(piProcInfo.hThread);
87 return status;
88 }
89 return status;
90 }
91
92 LONG ReadPipeSize(HANDLE hStdoutWr, HANDLE hStdoutRd)
93 {
94 CHAR chBuf[2];
95 LONG pos=0;
96
97 if (!CloseHandle(hStdoutWr))
98 {
99 return 0; /* fail */
100 }
101
102 for (;;)
103 {
104 long dwRead;
105 if( !ReadFile( hStdoutRd, chBuf, 1, (LPDWORD)&dwRead, NULL) || dwRead == 0)
106 {
107 break;
108 }
109 else
110 {
111 pos+=dwRead;
112 }
113 }
114 return pos;
115 }
116
117 LONG ReadPipe(HANDLE hStdoutWr, HANDLE hStdoutRd, CHAR *srvlst, LONG size)
118 {
119 CHAR chBuf[2];
120 LONG pos;
121
122 pos=0;
123
124 if (!CloseHandle(hStdoutWr))
125 {
126 return 0; /* fail */
127 }
128
129 for (;;)
130 {
131 LONG dwRead;
132 if( !ReadFile( hStdoutRd, chBuf, 1, (LPDWORD)&dwRead, NULL) || dwRead == 0)
133 {
134 break;
135 }
136 else
137 {
138 srvlst[pos++] = chBuf[0] ;
139 }
140 }
141 return 0;
142 }
143
144 INT row_scanner_service(CHAR *buffer, LONG* pos, LONG size,
145 CHAR *name,CHAR *save)
146 {
147 LONG get_semi;
148 LONG t;
149 LONG row_size=0;
150 LONG start_pos;
151
152 start_pos = *pos;
153
154 if (*pos>=size)
155 {
156 return 0;
157 }
158
159
160 /* get row start */
161 for (t=start_pos;t<size;t++)
162 {
163 if (buffer[t]=='\n')
164 {
165 buffer[t]='\0';
166 if (buffer[t-1]==0x09)
167 {
168 buffer[t-1]='\0';
169 }
170 if (buffer[t-1]==0x0d)
171 {
172 buffer[t-1]='\0';
173 }
174 *pos = t+1;
175 row_size = t;
176 break;
177 }
178 }
179
180 /* get : */
181 get_semi=-1;
182 for (t=start_pos;t<row_size;t++)
183 {
184 if (buffer[t]==':')
185 {
186 get_semi=t;
187 break;
188 }
189 }
190
191 if (get_semi==-1)
192 {
193 return 0;
194 }
195
196 /* lock for space */
197 for (t=get_semi+1;t<row_size;t++)
198 {
199 if (!isspace(buffer[t]))
200 {
201 break;
202 }
203 }
204 if (t==0)
205 {
206 /* : not found next row*/
207 return 0;
208 }
209
210 /* Compare now */
211 if (_strnicmp(name,&buffer[t],strlen(&buffer[t]))==0)
212 {
213 if (save != NULL)
214 {
215 /* lock for space */
216 for (t=start_pos;t<get_semi;t++)
217 {
218 if (!isspace(buffer[t]))
219 {
220 break;
221 }
222 }
223
224 memcpy(save,&buffer[t],get_semi-t);
225 }
226 return 1;
227 }
228 return 0;
229 }