Various application fixes by Jan Roeloffzen, bug #5182, part 3
[reactos.git] / reactos / base / applications / network / ftp / fake.c
1 #include <stdio.h>
2 #include <time.h>
3 #include <winsock.h>
4 #include "fake.h"
5 #include "prototypes.h"
6
7 #define MAX_ASCII 100
8
9 int checkRecv(SOCKET s);
10
11 int checkRecv(SOCKET s)
12 {
13 int testVal;
14 fd_set sSet;
15 struct timeval timeout;
16 timeout.tv_sec = 60;
17
18 FD_ZERO(&sSet);
19
20 FD_SET(s, &sSet);
21
22 testVal = select(0, &sSet, NULL, NULL, &timeout);
23
24 if (testVal == SOCKET_ERROR)
25 fprintf(stderr, "Socket Error");
26
27 return testVal;
28 }
29
30 void blkfree(char **av0)
31 {
32 register char **av = av0;
33
34 while (*av)
35 free(*av++);
36 }
37
38 char **glob(register char *v)
39 {
40 return NULL;
41 }
42
43 int sleep(int time)
44 {
45 return time;
46 }
47
48 int herror(char *string)
49 {
50 return 0;
51 }
52
53 #if 0
54 int gettimeofday(struct timeval *timenow,
55 struct timezone *zone)
56 {
57 time_t t;
58
59 t = clock();
60
61 timenow->tv_usec = t;
62 timenow->tv_sec = t / CLK_TCK;
63
64 return 0;
65 }
66
67 int fgetcSocket(int s)
68 {
69 int c;
70 char buffer[10];
71
72 // checkRecv(s);
73
74 c = recv(s, buffer, 1, 0);
75
76 #ifdef DEBUG_IN
77 printf("%c", buffer[0]);
78 #endif
79
80 if (c == INVALID_SOCKET)
81 return c;
82
83 if (c == 0)
84 return EOF;
85
86 return buffer[0];
87 }
88
89 #else
90
91 int fgetcSocket(int s)
92 {
93 static int index = 0;
94 static int total = 0;
95 static char buffer[4096];
96
97 if (index == total)
98 {
99 index = 0;
100 total = recv(s, buffer, sizeof(buffer), 0);
101
102 if (total == SOCKET_ERROR)
103 {
104 total = 0;
105 return ERROR;
106 }
107
108 if (total == 0)
109 return EOF;
110 }
111 return buffer[index++];
112 }
113
114 #endif
115
116 const char *fprintfSocket(int s, const char *format, ...)
117 {
118 va_list argptr;
119 char buffer[10009];
120
121 va_start(argptr, format);
122 vsprintf(buffer, format, argptr);
123 va_end(argptr);
124
125 send(s, buffer, strlen(buffer), 0);
126
127 return NULL;
128 }
129
130 const char *fputsSocket(const char *format, int s)
131 {
132 send(s, format, strlen(format), 0);
133
134 return NULL;
135 }
136
137 int fputcSocket(int s, char putChar)
138 {
139 char buffer[2];
140
141 buffer[0] = putChar;
142 buffer[1] = '\0';
143
144 if(SOCKET_ERROR==send(s, buffer, 1, 0)) {
145 int iret=WSAGetLastError ();
146 fprintf(stdout,"fputcSocket: %d\n",iret);
147 return 0;
148 }
149 else {
150 return putChar;
151 }
152 }
153 int fputSocket(int s, char *buffer, int len)
154 {
155 int iret;
156 while(len) {
157 if(SOCKET_ERROR==(iret=send(s, buffer, len, 0)))
158 {
159 iret=WSAGetLastError ();
160 fprintf(stdout,"fputcSocket: %d\n",iret);
161 return 0;
162 }
163 else {
164 return len-=iret;
165 }
166 }
167 return 0;
168 }
169
170 char *fgetsSocket(int s, char *string)
171 {
172 char buffer[2] = {0};
173 int i, count;
174
175 for (i = 0, count = 1; count != 0 && buffer[0] != '\n'; i++)
176 {
177 checkRecv(s);
178
179 count = recv(s, buffer, 1, 0);
180
181 if (count == SOCKET_ERROR)
182 {
183 printf("Error in fgetssocket");
184 return NULL;
185 }
186
187 if (count == 1)
188 {
189 string[i] = buffer[0];
190
191 if (i == MAX_ASCII - 3)
192 {
193 count = 0;
194 string[++i] = '\n';
195 string[++i] = '\0';
196 }
197 }
198 else
199 {
200 if (i == 0)
201 return NULL;
202 else
203 {
204 string[i] = '\n';
205 string[i + 1] = '\0'; // This is risky
206 return string;
207 }
208
209 }
210
211 }
212 string[i] = '\0';
213
214 #ifdef DEBUG_IN
215 printf("%s", string);
216 #endif
217 return string;
218 }
219
220
221 #if 0
222 char *getpass(const char *prompt)
223 {
224 static char string[64];
225
226 printf("%s", prompt);
227
228 gets(string);
229
230 return string;
231 }
232 #endif
233 char *getpass (const char * prompt)
234 {
235 static char input[256];
236 HANDLE in;
237 HANDLE err;
238 DWORD count;
239
240 in = GetStdHandle (STD_INPUT_HANDLE);
241 err = GetStdHandle (STD_ERROR_HANDLE);
242
243 if (in == INVALID_HANDLE_VALUE || err == INVALID_HANDLE_VALUE)
244 return NULL;
245
246 if (WriteFile (err, prompt, strlen (prompt), &count, NULL))
247 {
248 int istty = (GetFileType (in) == FILE_TYPE_CHAR);
249 DWORD old_flags;
250 int rc;
251
252 if (istty)
253 {
254 if (GetConsoleMode (in, &old_flags))
255 SetConsoleMode (in, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
256 else
257 istty = 0;
258 }
259 /* Need to read line one byte at time to avoid blocking, if not a
260 tty, so always do it this way. */
261 count = 0;
262 while (1)
263 {
264 DWORD dummy;
265 char one_char;
266
267 rc = ReadFile (in, &one_char, 1, &dummy, NULL);
268 if (rc == 0)
269 break;
270 if (one_char == '\r')
271 {
272 /* CR is always followed by LF if reading from tty. */
273 if (istty)
274 continue;
275 else
276 break;
277 }
278 if (one_char == '\n')
279 break;
280 /* Silently truncate password string if overly long. */
281 if (count < sizeof (input) - 1)
282 input[count++] = one_char;
283 }
284 input[count] = '\0';
285
286 WriteFile (err, "\r\n", 2, &count, NULL);
287 if (istty)
288 SetConsoleMode (in, old_flags);
289 if (rc)
290 return input;
291 }
292
293 return NULL;
294 }
295
296 #if 0
297 // Stubbed out here. Should be changed in Source code...
298 int access(const char *filename, int accessmethod)
299 {
300 return 0;
301 }
302 #endif
303
304 #ifndef __GNUC__
305 #define EPOCHFILETIME (116444736000000000i64)
306 #else
307 #define EPOCHFILETIME (116444736000000000LL)
308 #endif
309
310 int gettimeofday(struct timeval *tv, struct timezone *tz)
311 {
312 FILETIME ft;
313 LARGE_INTEGER li;
314 __int64 t;
315 static int tzflag;
316
317 if (tv)
318 {
319 GetSystemTimeAsFileTime(&ft);
320 li.LowPart = ft.dwLowDateTime;
321 li.HighPart = ft.dwHighDateTime;
322 t = li.QuadPart; /* In 100-nanosecond intervals */
323 t -= EPOCHFILETIME; /* Offset to the Epoch time */
324 t /= 10; /* In microseconds */
325 tv->tv_sec = (long)(t / 1000000);
326 tv->tv_usec = (long)(t % 1000000);
327 }
328
329 if (tz)
330 {
331 if (!tzflag)
332 {
333 _tzset();
334 tzflag++;
335 }
336 tz->tz_minuteswest = _timezone / 60;
337 tz->tz_dsttime = _daylight;
338 }
339
340 return 0;
341 }