Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libcs/cspeek.c
1808 views
1
/***********************************************************************
2
* *
3
* This software is part of the ast package *
4
* Copyright (c) 1990-2011 AT&T Intellectual Property *
5
* and is licensed under the *
6
* Eclipse Public License, Version 1.0 *
7
* by AT&T Intellectual Property *
8
* *
9
* A copy of the License is available at *
10
* http://www.eclipse.org/org/documents/epl-v10.html *
11
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
12
* *
13
* Information and Software Systems Research *
14
* AT&T Research *
15
* Florham Park NJ *
16
* *
17
* Glenn Fowler <[email protected]> *
18
* *
19
***********************************************************************/
20
#pragma prototyped
21
22
#include "cslib.h"
23
24
#if _socket_peek && !CS_LIB_SOCKET
25
#include <sys/socket.h>
26
#endif
27
28
#if _stream_peek && !CS_LIB_STREAM
29
#include <stropts.h>
30
#endif
31
32
/*
33
* peek up to siz chars from fd into buf
34
* -1 returned if fd not peekable
35
*
36
* NOTE: cspeek() caches the peek state of fd
37
* call cspeek(-1,...) to flush the cache
38
*/
39
40
ssize_t
41
cspeek(register Cs_t* state, int fd, void* buf, size_t siz)
42
{
43
register int n;
44
#if _stream_peek
45
struct strpeek pk;
46
#endif
47
48
if (fd == -1)
49
{
50
#if _stream_peek
51
state->nostream = -1;
52
#endif
53
#if _socket_peek
54
state->nosocket = -1;
55
#endif
56
return -1;
57
}
58
#if _stream_peek
59
if (fd != state->nostream)
60
{
61
pk.flags = 0;
62
pk.ctlbuf.maxlen = -1;
63
pk.ctlbuf.len = 0;
64
pk.ctlbuf.buf = 0;
65
pk.databuf.maxlen = siz;
66
pk.databuf.buf = (char*)buf;
67
pk.databuf.len = 0;
68
if ((n = ioctl(fd, I_PEEK, &pk)) >= 0)
69
{
70
state->nostream = -1;
71
if (n > 0) n = pk.databuf.len;
72
return n;
73
}
74
messagef((state->id, NiL, -1, "peek: %d: ioctl I_PEEK error", fd));
75
state->nostream = fd;
76
}
77
#endif
78
#if _socket_peek
79
if (fd != state->nosocket)
80
{
81
if ((n = recv(fd, (char*)buf, siz, MSG_PEEK)) >= 0)
82
{
83
state->nosocket = -1;
84
return n;
85
}
86
messagef((state->id, NiL, -1, "peek: %d: recv MSG_PEEK error", fd));
87
state->nosocket = fd;
88
}
89
#endif
90
messagef((state->id, NiL, -1, "peek: %d: no peek", fd));
91
return -1;
92
}
93
94
ssize_t
95
_cs_peek(int fd, void* buf, size_t siz)
96
{
97
return cspeek(&cs, fd, buf, siz);
98
}
99
100