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