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