Watt-32 tcp/ip  2.2 dev-rel.10
sock_scn.c
Go to the documentation of this file.
1 
5 #include <stdio.h>
6 #include <stdarg.h>
7 #include <string.h>
8 
9 #include "copyrigh.h"
10 #include "wattcp.h"
11 #include "misc.h"
12 #include "pctcp.h"
13 
14 #if defined(__WATCOMC__) && (__WATCOMC__ >= 1250) /* OW 1.5+ */
15  #define OPENWATCOM_15
16 #endif
17 
18 #if defined(__MINGW32__) || defined(__MINGW64__) || defined(__CYGWIN__) || defined(OPENWATCOM_15)
19  #define HAVE_VSSCANF
20 #endif
21 
22 #if defined(_MSC_VER) && defined(WIN32)
23 /*
24  * Converted to MSVC-asm by Gisle Vanem.
25  * Originally written for gcc-asm:
26  * By aaronwl 2003-01-28 for mingw-msvcrt.
27  * Public domain: all copyrights disclaimed, absolutely no warranties.
28  */
29 __declspec(naked) static int vsscanf_msvc_asm (const char *s, const char *format, va_list arg)
30 {
31 #if 0
32  int ret;
33 
34  __asm__(
35 
36  // allocate stack (esp += frame - arg3 - (8[arg1,2] + 12))
37  "movl %%esp, %%ebx\n\t"
38  "lea 0xFFFFFFEC(%%esp, %6), %%esp\n\t"
39  "subl %5, %%esp\n\t"
40 
41  // set up stack
42  "movl %1, 0xC(%%esp)\n\t" // s
43  "movl %2, 0x10(%%esp)\n\t" // format
44  "lea 0x14(%%esp), %%edi\n\t"
45  "movl %%edi, (%%esp)\n\t" // memcpy dest
46  "movl %5, 0x4(%%esp)\n\t" // memcpy src
47  "movl %5, 0x8(%%esp)\n\t"
48  "subl %6, 0x8(%%esp)\n\t" // memcpy len
49  "call _memcpy\n\t"
50  "addl $12, %%esp\n\t"
51 
52  "call _sscanf\n\t"
53 
54  "movl %%ebx, %%esp\n\t" // restore stack
55 
56  : "=a"(ret), "=c"(s), "=d"(format)
57  : "1"(s), "2"(format), "S"(arg),
58  "a"(&ret)
59  : "ebx");
60 
61  return ret;
62 
63 #else
64  _asm {
65  mov ebx, esp
66  lea esp, [esp+6-24]
67  sub esp, 5
68 
69  mov eax, [ebx+4]
70  mov [esp+0Ch], eax // s
71  mov eax, [ebx+8]
72  mov [esp+10h], eax // format
73 
74  }
75 #endif
76 }
77 #endif /*_MSC_VER && WIN32 */
78 
79 
80 /*
81  * MSC/DMC/DJGPP 2.01 doesn't have vsscanf().
82  * On Windows (MSVC) there's nothing to be done yet :-(
83  */
84 #if defined(WIN32) && !defined(HAVE_VSSCANF)
85  static int vsscanf (const char *buf, const char *fmt, va_list arglist)
86  {
87  #if defined(_MSC_VER) && 0
88  return vsscanf_msvc_asm (buf, fmt, arglist);
89  #else
90  ARGSUSED (buf);
91  ARGSUSED (fmt);
92  ARGSUSED (arglist);
93  UNIMPLEMENTED();
94  return (0);
95  #endif
96  }
97 #elif defined(__DJGPP__) && (DJGPP_MINOR < 2)
98  static int vsscanf (const char *buf, const char *fmt, va_list arglist)
99  {
100  FILE *fil = stdin;
101  fil->_ptr = (char*) buf;
102  return _doscan (fil, fmt, arglist);
103  }
104 #elif defined(_MSC_VER) || defined(__DMC__) /* MSVC (DOS) or DMC (any) */
105  static int vsscanf (const char *buf, const char *fmt, va_list arglist)
106  {
107  extern _input (FILE*, const char*, va_list);
108  FILE *fil = stdin;
109  fil->_ptr = (char*) buf;
110  return _input (fil, fmt, arglist);
111  }
112 #endif
113 
114 /*
115  * sock_scanf - Read a line and return number of fields matched.
116  *
117  * BIG WARNING: Don't use this for packetised protocols like
118  * SSH. Only suitable for ASCII orientented protocols
119  * like POP3/SMTP/NNTP etc.
120  *
121  * NOTE: Don't use {\r\n} in 'fmt' (it's stripped in sock_gets).
122  */
123 int MS_CDECL sock_scanf (sock_type *s, const char *fmt, ...)
124 {
125  char buf [tcp_MaxBufSize];
126  int fields = 0;
127  int status, len;
128 
129  while ((status = sock_dataready(s)) == 0)
130  {
131  if (status == -1)
132  return (-1);
133 
134  len = sock_gets (s, (BYTE*)buf, sizeof(buf));
135  if (len > 0)
136  {
137  va_list args;
138  va_start (args, fmt);
139  fields = vsscanf (buf, fmt, args);
140  va_end (args);
141  break;
142  }
143  }
144  return (fields);
145 }
146 
Core definitions.
Definition: esp.h:48
WORD W32_CALL sock_dataready(sock_type *s)
sock_dataready - returns number of bytes waiting to be read.
Definition: sock_io.c:246