wa-lang.org/wazero@v1.0.2/imports/wasi_snapshot_preview1/example/testdata/zig-cc/cat.c (about)

     1  #include <fcntl.h> // constants
     2  #include <unistd.h> // posix
     3  #include <stdio.h> // fprintf
     4  
     5  const int BUF_LEN = 512;
     6  
     7  // main is the same as wasi _start: "concatenate and print files."
     8  int main(int argc, char** argv)
     9  {
    10    unsigned char buf[BUF_LEN];
    11    int fd = 0;
    12    int len = 0;
    13  
    14    // Start at arg[1] because args[0] is the program name.
    15    for (int i = 1; i < argc; i++) {
    16      int fd = open(argv[i], O_RDONLY);
    17      if (fd < 0) {
    18        fprintf(stderr, "error opening %s: %d\n", argv[i], fd);
    19        return 1;
    20      }
    21  
    22      for (;;) {
    23        len = read(fd, &buf[0], BUF_LEN);
    24        if (len > 0) {
    25          write(STDOUT_FILENO, buf, len);
    26        } else if (len == 0) {
    27          break;
    28        } else {
    29          fprintf(stderr, "error reading %s\n", argv[i]);
    30          return 1;
    31        }
    32      }
    33      close(fd);
    34    }
    35  
    36    return 0;
    37  }