ReactOS  0.4.11-dev-946-g431643b
fake.c
Go to the documentation of this file.
1 #include "precomp.h"
2 
3 #include <wincon.h>
4 
5 #define MAX_ASCII 100
6 
7 int checkRecv(SOCKET s);
8 
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 
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))
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  {
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 }
Definition: winsock.h:66
#define SOCKET_ERROR
Definition: winsock.h:333
int tz_dsttime
Definition: fake.h:16
VOID WINAPI GetSystemTimeAsFileTime(OUT PFILETIME lpFileTime)
Definition: time.c:128
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
_CRTIMP char *__cdecl gets(char *_Buffer)
Definition: file.c:3643
_CRTIMP void __cdecl _tzset(void)
Definition: timezone.c:92
GLuint GLuint GLsizei count
Definition: gl.h:1545
#define free
Definition: debug_ros.c:5
BOOL WINAPI GetConsoleMode(HANDLE hConsoleHandle, LPDWORD lpMode)
Definition: console.c:1571
unsigned long tv_sec
Definition: linux.h:1738
void blkfree(char **av0)
Definition: fake.c:28
GLdouble GLdouble t
Definition: gl.h:2047
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:152
GLuint buffer
Definition: glext.h:5915
DWORD WINAPI GetFileType(HANDLE hFile)
Definition: fileinfo.c:419
__u16 time
Definition: mkdosfs.c:366
#define ERROR(err)
Definition: vbo_exec_api.c:413
#define FD_ZERO(set)
Definition: winsock.h:96
const char * filename
Definition: ioapi.h:135
#define FD_SET(fd, set)
Definition: winsock.h:89
char * fgetsSocket(int s, char *string)
Definition: fake.c:168
const char * fputsSocket(const char *format, int s)
Definition: fake.c:128
#define CLK_TCK
Definition: time.h:435
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: gl.h:1546
FILE * stdout
DWORD dwHighDateTime
Definition: mapidefs.h:66
char * getpass(const char *prompt)
Definition: fake.c:231
GLenum GLclampf GLint i
Definition: glfuncs.h:14
#define MAX_ASCII
Definition: fake.c:5
#define va_end(ap)
Definition: acmsvcex.h:90
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
INT WSAAPI select(IN INT s, IN OUT LPFD_SET readfds, IN OUT LPFD_SET writefds, IN OUT LPFD_SET exceptfds, IN CONST struct timeval *timeout)
Definition: select.c:41
#define FILE_TYPE_CHAR
Definition: winbase.h:257
BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleMode(HANDLE hConsoleHandle, DWORD dwMode)
Definition: console.c:1608
int fputSocket(int s, char *buffer, int len)
Definition: fake.c:151
int checkRecv(SOCKET s)
Definition: fake.c:9
smooth NULL
Definition: ftsmooth.c:416
unsigned long tv_usec
Definition: linux.h:1739
char * va_list
Definition: acmsvcex.h:78
GLuint index
Definition: glext.h:6031
#define STD_INPUT_HANDLE
Definition: winbase.h:264
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
int fputcSocket(int s, char putChar)
Definition: fake.c:135
INT WSAAPI WSAGetLastError(VOID)
Definition: dllmain.c:112
#define INVALID_SOCKET
Definition: winsock.h:332
int fgetcSocket(int s)
Definition: fake.c:89
INT WSAAPI send(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags)
Definition: send.c:23
#define STD_ERROR_HANDLE
Definition: winbase.h:266
unsigned long
Definition: typeof.h:99
unsigned long DWORD
Definition: ntddk_ex.h:95
#define ENABLE_LINE_INPUT
Definition: wincon.h:76
int sleep(int time)
Definition: fake.c:41
int herror(char *string)
Definition: fake.c:46
int _daylight
Definition: timezone.c:30
ULONG LowPart
Definition: typedefs.h:104
GLenum GLsizei len
Definition: glext.h:6722
GLdouble s
Definition: gl.h:2039
#define err(...)
long _timezone
Definition: timezone.c:46
char string[160]
Definition: util.h:11
GLfloat CONST GLvector4f * in
Definition: m_xform.h:122
DWORD zone
Definition: sec_mgr.c:1760
#define ENABLE_PROCESSED_INPUT
Definition: wincon.h:75
GLenum GLenum GLenum input
Definition: glext.h:9031
const GLdouble * v
Definition: gl.h:2040
__kernel_time_t time_t
Definition: linux.h:252
GLuint GLint GLboolean GLint GLenum access
Definition: glext.h:7866
unsigned char dummy
Definition: maze.c:118
int __cdecl vsprintf(char *_Dest, const char *_Format, va_list _Args)
Definition: sprintf.c:733
char ** glob(const char *v)
Definition: fake.c:36
GLbyte GLbyte tz
Definition: glext.h:8756
#define va_start(ap, A)
Definition: acmsvcex.h:91
_Check_return_ _CRTIMP clock_t __cdecl clock(void)
Definition: clock.c:23
#define EOF
Definition: stdio.h:24
#define c
Definition: ke_i.h:80
FILE * stderr
UINT_PTR SOCKET
Definition: winsock.h:47
Definition: fake.h:14
const char * fprintfSocket(int s, const char *format,...)
Definition: fake.c:114
BOOL WINAPI ReadFile(IN HANDLE hFile, IN LPVOID lpBuffer, IN DWORD nNumberOfBytesToRead, OUT LPDWORD lpNumberOfBytesRead OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:123
#define EPOCHFILETIME
Definition: fake.c:303
INT WSAAPI recv(IN SOCKET s, OUT CHAR FAR *buf, IN INT len, IN INT flags)
Definition: recv.c:23
DWORD dwLowDateTime
Definition: mapidefs.h:65
#define __int64
Definition: basetyps.h:16
LONGLONG QuadPart
Definition: typedefs.h:112
int tz_minuteswest
Definition: fake.h:15
#define printf
Definition: config.h:203
int gettimeofday(struct timeval *tv, struct timezone *tz)
Definition: fake.c:308