ReactOS  0.4.13-dev-259-g5ca9c9c
SReadline.c File Reference
#include "syshdrs.h"
Include dependency graph for SReadline.c:

Go to the source code of this file.

Functions

void FlushSReadlineInfo (SReadlineInfo *srl)
 
int InitSReadlineInfo (SReadlineInfo *srl, int fd, char *buf, size_t bsize, int tlen, int requireEOLN)
 
void DisposeSReadlineInfo (SReadlineInfo *srl)
 
int SReadline (SReadlineInfo *srl, char *const linebuf, size_t linebufsize)
 

Function Documentation

◆ DisposeSReadlineInfo()

void DisposeSReadlineInfo ( SReadlineInfo srl)

Definition at line 57 of file SReadline.c.

58 {
59  memset(srl->buf, 0, srl->bufSizeMax);
60  if (srl->malloc != 0)
61  free(srl->buf);
62  memset(srl, 0, sizeof(SReadlineInfo));
63 
64  /* Note: it does not close(srl->fd). */
65 } /* DisposeSReadlineInfo */
size_t bufSizeMax
Definition: ncftp.h:105
#define free
Definition: debug_ros.c:5
int malloc
Definition: ncftp.h:106
char * buf
Definition: ncftp.h:101
#define memset(x, y, z)
Definition: compat.h:39

Referenced by CloseControlConnection(), FTPDeallocateHost(), FTPList(), and FTPListToMemory2().

◆ FlushSReadlineInfo()

void FlushSReadlineInfo ( SReadlineInfo srl)

Definition at line 4 of file SReadline.c.

5 {
6  /* Discards any input left in the current buffer,
7  * and resets the buffer and its pointer.
8  */
9  srl->bufSize = srl->bufSizeMax;
10  memset(srl->buf, 0, srl->bufSize);
11  srl->bufLim = srl->buf + srl->bufSizeMax;
12 
13  /* This line sets the buffer pointer
14  * so that the first thing to do is reset and fill the buffer
15  * using real I/O.
16  */
17  srl->bufPtr = srl->bufLim;
18 } /* FlushSReadlineInfo */
size_t bufSizeMax
Definition: ncftp.h:105
char * bufLim
Definition: ncftp.h:103
size_t bufSize
Definition: ncftp.h:104
char * buf
Definition: ncftp.h:101
char * bufPtr
Definition: ncftp.h:102
#define memset(x, y, z)
Definition: compat.h:39

◆ InitSReadlineInfo()

int InitSReadlineInfo ( SReadlineInfo srl,
int  fd,
char buf,
size_t  bsize,
int  tlen,
int  requireEOLN 
)

Definition at line 24 of file SReadline.c.

25 {
26  if (buf == NULL) {
27  if (bsize < 512)
28  bsize = 512; /* Pointless, otherwise. */
29  buf = (char *) malloc(bsize);
30  if (buf == NULL)
31  return (-1);
32  srl->malloc = 1;
33  } else {
34  srl->malloc = 0;
35  }
36  memset(buf, 0, bsize);
37  srl->buf = buf;
38  srl->bufSize = bsize;
39  srl->bufSizeMax = bsize;
40  srl->bufLim = srl->buf + bsize;
41  srl->fd = fd;
42  srl->timeoutLen = tlen;
43  srl->requireEOLN = requireEOLN;
44 
45  /* This line sets the buffer pointer
46  * so that the first thing to do is reset and fill the buffer
47  * using real I/O.
48  */
49  srl->bufPtr = srl->bufLim;
50  return (0);
51 } /* InitSReadlineInfo */
size_t bufSizeMax
Definition: ncftp.h:105
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
static int fd
Definition: io.c:51
int requireEOLN
Definition: ncftp.h:109
smooth NULL
Definition: ftsmooth.c:416
int timeoutLen
Definition: ncftp.h:108
char * bufLim
Definition: ncftp.h:103
int malloc
Definition: ncftp.h:106
size_t bufSize
Definition: ncftp.h:104
char * buf
Definition: ncftp.h:101
char * bufPtr
Definition: ncftp.h:102
#define malloc
Definition: debug_ros.c:4
#define memset(x, y, z)
Definition: compat.h:39

Referenced by FTPList(), FTPListToMemory2(), FTPRebuildConnectionInfo(), OpenControlConnection(), and ServeOneClient().

◆ SReadline()

int SReadline ( SReadlineInfo srl,
char *const  linebuf,
size_t  linebufsize 
)

Definition at line 76 of file SReadline.c.

77 {
78  int err;
79  char *src;
80  char *dst;
81  char *dstlim;
82  int len;
83  int nr;
84  int requireEOLN;
85  int illegals;
86 
87  illegals = 0;
88  err = 0;
89  dst = linebuf;
90  dstlim = dst + linebufsize - 1; /* Leave room for NUL. */
91  src = srl->bufPtr;
92  requireEOLN = srl->requireEOLN;
93  if (requireEOLN)
94  dstlim--;
95  if (dstlim <= dst)
96  return (-1); /* Buffer too small. */
97 
98  forever {
99  if ((requireEOLN == 0) && (dst >= dstlim))
100  break;
101  if (src >= srl->bufLim) {
102  /* Fill the buffer. */
103  if (illegals > 1) {
104  /* Probable DOS -- return now and give you an
105  * opportunity to handle bogus input.
106  */
107  goto done;
108  }
109  nr = SRead(srl->fd, srl->buf, srl->bufSizeMax, srl->timeoutLen, 0);
110  if (nr == 0) {
111  /* EOF. */
112  goto done;
113  } else if (nr < 0) {
114  /* Error. */
115  err = nr;
116  goto done;
117  }
118  srl->bufPtr = src = srl->buf;
119  srl->bufLim = srl->buf + nr;
120  }
121  if (*src == '\0') {
122  ++src;
123  illegals++;
124  } else if (*src == '\r') {
125  ++src;
126  /* If the next character is a \n that is valid,
127  * otherwise treat a stray \r as an illegal character.
128  */
129  if ((src < srl->bufLim) && (*src != '\n'))
130  illegals++;
131  } else {
132  if (*src == '\n') {
133  if (dst < dstlim)
134  *dst++ = *src++;
135  else
136  src++;
137  goto done;
138  }
139  if (dst < dstlim)
140  *dst++ = *src++;
141  else
142  src++;
143  }
144  }
145 
146 done:
147  srl->bufPtr = src;
148  if ((requireEOLN != 0) && (dst != linebuf) && (dst[-1] != '\n'))
149  *dst++ = '\n';
150  if ((requireEOLN != 0) && (dst == linebuf) && (illegals > 0))
151  *dst++ = '\n';
152  *dst = '\0';
153  len = (int) (dst - linebuf);
154  if (err < 0)
155  return (err);
156  return (len);
157 } /* SReadline */
size_t bufSizeMax
Definition: ncftp.h:105
ULONG nr
Definition: thread.c:7
int requireEOLN
Definition: ncftp.h:109
int timeoutLen
Definition: ncftp.h:108
char * bufLim
Definition: ncftp.h:103
GLenum GLsizei len
Definition: glext.h:6722
#define forever
Definition: ncftp.h:73
GLenum src
Definition: glext.h:6340
#define err(...)
GLenum GLenum dst
Definition: glext.h:6340
char * buf
Definition: ncftp.h:101
char * bufPtr
Definition: ncftp.h:102
int SRead(int, char *const, size_t, int, int)
Definition: SRead.c:118
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31

Referenced by FTPList(), FTPListToMemory2(), GetResponse(), and ServeOneClient().