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