File: io.c

package info (click to toggle)
libowfat 0.22-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,148 kB
  • ctags: 976
  • sloc: ansic: 10,424; makefile: 42
file content (35 lines) | stat: -rw-r--r-- 804 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "io.h"

char buf[65536];
int64 readpos = 0;
int64 writepos = 0;
int flageof = 0;

int main()
{
  int64 r;

  if (!io_fd(0)) return 111;
  if (!io_fd(1)) return 111;

  for (;;) {
    if (flageof && writepos >= readpos) return 0;

    if (!flageof && readpos < sizeof buf) {
      r = io_tryread(0,buf + readpos,sizeof buf - readpos);
      if (r <= -2) return 111; /* read error other than EAGAIN */
      if (r == 0) flageof = 1;
      if (r > 0) readpos += r;
    }

    if (writepos < readpos) {
      r = io_trywrite(1,buf + writepos,readpos - writepos);
      if (r <= -2) return 111; /* write error other than EAGAIN */
      if (r > 0) {
	writepos += r;
	if (writepos == readpos) readpos = writepos = 0;
	/* if writepos is big, might want to left-shift buffer here */
      }
    }
  }
}