Debian Codegraph @

COSMIC/Sscanf.c

raw
1 /* Sscanf.c:
2 * Authors: Charles E. Campbell, Jr.
3 * Terry McRoberts
4 * Copyright: Copyright (C) 1999-2005 Charles E. Campbell, Jr. {{{1
5 * Permission is hereby granted to use and distribute this code,
6 * with or without modifications, provided that this copyright
7 * notice is copied with it. Like anything else that's free,
8 * Sscanf.c is provided *as is* and comes with no warranty
9 * of any kind, either expressed or implied. By using this
10 * software, you agree that in no event will the copyright
11 * holder be liable for any damages resulting from the use
12 * of this software.
13 * Date: Aug 22, 2005
14 */
15 #include <stdio.h>
16 #include <ctype.h>
17 #include "sockets.h"
18
19 /* -----------------------------------------------------------------------
20 * Local Definitions:
21 */
22 #define BUFSIZE 256
23
24 /* ----------------------------------------------------------------------- */
25
26 /* Sscanf: */
27 /*VARARGS*/
28 #ifdef __PROTOTYPE__
29 int Sscanf(Socket *skt,char *fmt,...)
30 #else
31 int Sscanf(skt,va_alist)
32 Socket *skt;
33 va_dcl
34 #endif
35 {
36 va_list args;
37 #ifdef MSDOS
38 char *arg;
39 #else
40 void *arg;
41 #endif
42 char *b = NULL;
43 char *bufend = NULL;
44 char *f = NULL;
45 char *snglfmt = NULL;
46 int buflen;
47 int cnt = 0;
48 static char buf[PM_BIGBUF];
49
50 #ifndef __PROTOTYPE__
51 char *fmt;
52 #endif
53
54 #ifdef SSLNOPEEK
55 char *bb;
56 #endif
57
58
59 /* sanity check */
60 if(!skt) {
61 return -1;
62 }
63
64 #ifdef __PROTOTYPE__
65 va_start(args,fmt);
66 #else
67 va_start(args);
68 fmt = va_arg(args,char *);
69 #endif
70
71 /* process argument string, one argument at a time */
72 for(f= fmt, buflen= 1; buflen > 0; f= NULL) {
73
74 /* get next argument from stack. According to the usual
75 * definition of scanf-like functions, it must be a
76 * pointer. The associated %... format code will tell
77 * Sscanf the type of pointer.
78 */
79
80 arg= va_arg(args,void *);
81 if(!arg) break;
82
83 /* get associated snglfmt */
84 snglfmt= strnxtfmt(f);
85 if(!snglfmt) break;
86
87 /* wait until something shows up on the Socket */
88 Swait(skt);
89
90 /* peek at socket data, and keep at it until at least one byte is
91 * not processed by the snglfmt
92 */
93 #ifdef SSLNOPEEK
94 bb= buf;
95 #endif
96
97 do {
98
99 #ifdef SSLNOPEEK
100 /* read socket buffer one byte at a time :-( */
101 if(recv(skt->skt,bb,(unsigned) 1,0) <= 0) {
102 va_end(args);
103 return cnt;
104 }
105 b = bb;
106 buflen= bb - buf + 1;
107 if(*(bb++) == '\0') break;
108
109
110 #else /* #ifdef SSLNOPEEK ... #else ... #endif */
111
112 /* peek at socket buffer */
113 buflen = Speek(skt,buf,PM_BIGBUF-1);
114 /* on error, return count of Sscanf'd args */
115 if(buflen < 0) {
116 va_end(args);
117 return cnt;
118 }
119 #endif /* #ifdef SSLNOPEEK ... #else ... #endif */
120
121 /* terminate buffer appropriately with a null byte */
122 bufend = buf + buflen;
123 *bufend= '\0';
124
125 /* get a pointer to the character in buf just beyond that the
126 * snglfmt will process. If that character is bufend, then
127 * it is possible that the snglfmt could process more data than
128 * is currently in buf. Otherwise, b < bufend and so all the
129 * data that snglbuf will process is available.
130 */
131 b= stpnxt(buf,snglfmt);
132 } while(b && b == bufend);
133
134 /* read (b - buf) bytes from the socket */
135 if(b && !*b) ++b; /* read in null byte, if one is present */
136 #ifndef SSLNOPEEK
137 buflen= Sreadbytes(skt,buf,(int) (b - buf));
138 #endif
139 if(buflen < 0) break; /* whoops - got an error! */
140 buf[buflen]= '\0'; /* terminate buf properly */
141
142 /* sscanf the buf to set up the arg */
143 if(sscanf(buf,snglfmt,arg)) ++cnt;
144 else break;
145 }
146
147 /* leave Sscanf */
148 va_end(args);
149
150 return cnt;
151 }
152
153 /* ---------------------------------------------------------------------
154 * vim: ts=4
155 */