Skip to content

Commit 7c9356c

Browse files
committed
address POSIX equivalent of FIONREAD
1 parent 864d648 commit 7c9356c

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

kirc.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#include <stdlib.h>
88
#include <string.h>
99
#include <netdb.h>
10-
#include <sys/ioctl.h>
1110
#include <fcntl.h>
11+
#include <sys/select.h>
1212
#include <sys/wait.h>
1313
#include <termios.h>
1414

@@ -29,14 +29,17 @@ kbhit(void)
2929
{
3030
int byteswaiting;
3131
struct termios term;
32-
32+
fd_set fds;
33+
struct timespec ts = {0};
3334
tcgetattr(0, &term);
3435

3536
struct termios term2 = term;
3637

3738
term2.c_lflag &= ~ICANON;
3839
tcsetattr(0, TCSANOW, &term2);
39-
ioctl(0, FIONREAD, &byteswaiting);
40+
FD_ZERO(&fds);
41+
FD_SET(0, &fds);
42+
byteswaiting = pselect(1, &fds, NULL, NULL, &ts, NULL);
4043
tcsetattr(0, TCSANOW, &term);
4144

4245
return byteswaiting > 0;
@@ -58,11 +61,11 @@ raw(char *fmt, ...) {
5861
static void
5962
con(void)
6063
{
61-
struct addrinfo hints, *res;
64+
struct addrinfo *res, hints = {
65+
.ai_family = AF_INET,
66+
.ai_socktype = SOCK_STREAM
67+
};
6268

63-
memset(&hints, 0, sizeof hints);
64-
hints.ai_family = AF_INET;
65-
hints.ai_socktype = SOCK_STREAM;
6669
getaddrinfo(host, port, &hints, &res);
6770
conn = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
6871
connect(conn, res->ai_addr, res->ai_addrlen);
@@ -85,7 +88,6 @@ printw(const char *format, ...)
8588
if (strlen(line) <= CMAX) printf("%s", line);
8689
else if (strlen(line) > CMAX)
8790
{
88-
8991
for (i = 0; i < CMAX; i++)
9092
{
9193
if (line[i] == ' ') s1 = i;
@@ -106,7 +108,7 @@ printw(const char *format, ...)
106108
}
107109
}
108110
}
109-
111+
110112
}
111113

112114
static void
@@ -231,11 +233,11 @@ main(int argc, char **argv)
231233
{
232234
case 'q':
233235
write(fd[1], "quit", sizeof("quit"));
234-
break;
236+
break;
235237
case 'm':
236238
while (isspace(*cmd_val)) cmd_val++;
237239
dprintf(fd[1], "privmsg #%s :%s", chan, cmd_val);
238-
break;
240+
break;
239241
}
240242
}
241243
else
@@ -245,6 +247,5 @@ main(int argc, char **argv)
245247
fflush(stdout);
246248
}
247249
}
248-
249250
return 0;
250251
}

0 commit comments

Comments
 (0)