aboutsummaryrefslogtreecommitdiffstats
path: root/erts/emulator/test/async_ports_SUITE_data/cport.c
blob: 179cdc4b8bfa469cfbe546913a5541f31c7097f2 (plain) (blame)
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

typedef unsigned char byte;

int read_cmd(byte *buf)
{
  int len;
  if (read_exact(buf, 4) != 4)
      return(-1);

  len = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3];
  return read_exact(buf, len);
}

int write_cmd(byte *buf, int len)
{
  byte li[4];
  li[0] = (len >> 24) & 0xff;
  li[1] = (len >> 16) & 0xff;
  li[2] = (len >> 8)  & 0xff;
  li[3] = len  & 0xff;
  write_exact(&li, 4);

  return write_exact(buf, len);
}

int read_exact(byte *buf, int len)
{
  int i, got=0;
  do {
    if ((i = read(0, buf+got, len-got)) <= 0)
      {
	return(i);
      }
    got += i;
  } while (got<len);
  return len;
}

int write_exact(byte *buf, int len)
{
  int i, wrote = 0;
  do {
    if ((i = write(1, buf+wrote, len-wrote)) < 0)
      return (i);
    wrote += i;
  } while (wrote<len);
  return len;
}

int main(int argc, char **argv) {
  int sleep_time = atoi(argv[1]);
  int fn, arg, res;
  byte *buf = malloc(31457280); // 30 mb
  int len = 0;
  while ((len = read_cmd(buf)) > 0) {
    usleep(sleep_time);
    write_cmd(buf, len);
  }
}