aacdec: detect buffer overrun and return an errror.
[paraslash.git] / stdin.c
1 #include "para.h"
2 #include "string.h"
3 #include "list.h"
4 #include "sched.h"
5 #include "fd.h"
6 #include "error.h"
7 #include "stdin.h"
8
9 void stdin_pre_select(struct sched *s, struct task *t)
10 {
11 struct stdin_task *sit = t->private_data;
12 if (sit->loaded < sit->bufsize)
13 para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
14 t->ret = 1; /* success */
15 }
16
17 static void stdin_default_event_handler(struct task *t)
18 {
19 PARA_NOTICE_LOG("%p: %s\n", t, PARA_STRERROR(-t->ret));
20 unregister_task(t);
21 }
22
23 void stdin_post_select(struct sched *s, struct task *t)
24 {
25 struct stdin_task *sit = t->private_data;
26 ssize_t ret;
27
28 t->ret = 1;
29 if (sit->loaded >= sit->bufsize)
30 return;
31 if (!FD_ISSET(STDIN_FILENO, &s->rfds))
32 return;
33 ret = read(STDIN_FILENO, sit->buf + sit->loaded, sit->bufsize - sit->loaded);
34 if (ret < 0)
35 t->ret = -E_STDIN_READ;
36 else if (ret > 0) {
37 sit->loaded += ret;
38 t->ret = ret;
39 } else
40 t->ret = -E_STDIN_EOF;
41 if (t->ret < 0)
42 sit->eof = 1;
43 }
44
45 void stdin_set_defaults(struct stdin_task *sit)
46 {
47 sit->bufsize = 16 * 1024,
48 sit->loaded = 0,
49 sit->eof = 0,
50 sit->task.flags = 0,
51 sit->task.pre_select = stdin_pre_select;
52 sit->task.post_select = stdin_post_select;
53 sit->task.event_handler = stdin_default_event_handler;
54 sit->task.private_data = sit;
55 sprintf(sit->task.status, "stdin reader");
56 }