2 * I/O functions for judges
4 * (c) 2007 Martin Mares <mj@ucw.cz>
16 struct stream *sopen_fd(char *name, int fd)
18 struct stream *s = xmalloc(sizeof(*s) + BUFSIZE + strlen(name) + 1);
20 s->pos = s->stop = s->buf;
21 s->end = s->buf + BUFSIZE;
23 strcpy(s->name, name);
28 struct stream *sopen_read(char *name)
30 int fd = open(name, O_RDONLY);
32 die("Unable to open %s for reading: %m", name);
33 return sopen_fd(name, fd);
36 struct stream *sopen_write(char *name)
38 int fd = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0666);
40 die("Unable to open %s for writing: %m", name);
41 return sopen_fd(name, fd);
44 void sflush(struct stream *s)
49 int len = s->pos - s->buf;
52 int c = write(s->fd, p, len);
54 die("Error writing %s: %m", s->name);
59 s->pos = s->stop = s->buf;
62 void sclose(struct stream *s)
69 static int srefill(struct stream *s)
71 int len = read(s->fd, s->buf, BUFSIZE);
73 die("Error reading %s: %m", s->name);
75 s->stop = s->buf + len;
79 int sgetc_slow(struct stream *s)
81 return (srefill(s) ? *s->pos++ : -1);
84 int speekc_slow(struct stream *s)
86 return (srefill(s) ? *s->pos : -1);