File: main_unix.c

package info (click to toggle)
nodejs 20.19.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 219,072 kB
  • sloc: cpp: 1,277,408; javascript: 565,332; ansic: 129,476; python: 58,536; sh: 3,841; makefile: 2,725; asm: 1,732; perl: 248; lisp: 222; xml: 42
file content (51 lines) | stat: -rw-r--r-- 1,095 bytes parent folder | download | duplicates (6)
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <errno.h>
#include <unistd.h>

static size_t r(char* buf, size_t buf_size) {
  ssize_t read_count;
  do
    read_count = read(0, buf, buf_size);
  while (read_count < 0 && errno == EINTR);
  if (read_count <= 0)
    abort();
  return (size_t)read_count;
}

static void w(const char* buf, size_t count) {
  const char* end = buf + count;

  while (buf < end) {
    ssize_t write_count;
    do
      write_count = write(1, buf, count);
    while (write_count < 0 && errno == EINTR);
    if (write_count <= 0)
      abort();
    buf += write_count;
  }

  fprintf(stderr, "%zu", count);
  fflush(stderr);
}

int main(void) {
  w("0", 1);

  while (1) {
    char buf[256];
    size_t read_count = r(buf, sizeof(buf));
    // The JS part (test-child-process-stdio-overlapped.js) only writes the
    // "exit" string when the buffer is empty, so the read is guaranteed to be
    // atomic due to it being less than PIPE_BUF.
    if (!strncmp(buf, "exit", read_count)) {
      break;
    }
    w(buf, read_count);
  }

  return 0;
}