Skip to content

Commit e9042a7

Browse files
committed
get F_SETFL before setting nonblocking
1 parent a995c38 commit e9042a7

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

kirc.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ connection_initialize(void) {
120120
return -1;
121121
}
122122

123-
fcntl(conn, F_SETFL, O_NONBLOCK);
123+
int flags = fcntl(conn, F_GETFL, 0);
124+
flags |= O_NONBLOCK;
125+
fcntl(conn, F_SETFL, flags);
126+
124127
return 0;
125128
}
126129

@@ -231,26 +234,26 @@ handle_server_message(void) {
231234
static void
232235
handle_user_input(void) {
233236
char usrin[MSG_MAX], v1[MSG_MAX - CHA_MAX], v2[CHA_MAX], c1;
234-
if (fgets(usrin, MSG_MAX, stdin) != NULL &&
235-
(sscanf(usrin, "/%[m] %s %[^\n]\n", &c1, v2, v1) > 2 ||
236-
sscanf(usrin, "/%[a-zA-Z] %[^\n]\n", &c1, v1) > 0)) {
237+
fgets(usrin, MSG_MAX, stdin);
238+
if (sscanf(usrin, "/%[m] %s %[^\n]\n", &c1, v2, v1) > 2 ||
239+
sscanf(usrin, "/%[a-zA-Z] %[^\n]\n", &c1, v1) > 0) {
237240
switch (c1) {
238241
case 'x': raw("%s\r\n", v1); break;
239242
case 'q': raw("quit\r\n"); break;
240-
case 'u': strcpy(chan, v1); break;
241243
case 'Q': raw("quit %s\r\n", v1); break;
242244
case 'j': raw("join %s\r\n", v1); break;
243245
case 'p': raw("part %s\r\n", v1); break;
244246
case 'n': raw("names #%s\r\n", chan); break;
245247
case 'M': raw("privmsg nickserv :%s\r\n", v1); break;
246248
case 'm': raw("privmsg %s :%s\r\n", v2, v1); break;
249+
case 'u': strcpy(chan, v1); break;
247250
default : puts(HELP); break;
248251
}
249252
} else {
250253
size_t msg_len = strlen(usrin);
251254
if (usrin[msg_len - 1] == '\n') {
252255
usrin[msg_len - 1] = '\0';
253-
}
256+
}
254257
raw("privmsg #%s :%s\r\n", chan, usrin);
255258
}
256259
}
@@ -303,12 +306,10 @@ main(int argc, char **argv) {
303306

304307
for (;;) {
305308
int poll_res = poll(fds, 2, -1);
306-
307309
if (poll_res != -1) {
308310
if (fds[0].revents & POLLIN) {
309311
handle_user_input();
310312
}
311-
312313
if (fds[1].revents & POLLIN) {
313314
int rc = handle_server_message();
314315
if (rc != 0) {

0 commit comments

Comments
 (0)