github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/imports/wasi_snapshot_preview1/testdata/cargo-wasi/wasi.rs (about)

     1  use std::env;
     2  use std::fs;
     3  use std::io;
     4  use std::io::{Read,Write};
     5  use std::net::{TcpListener};
     6  use std::os::wasi::io::FromRawFd;
     7  use std::process::exit;
     8  use std::str::from_utf8;
     9  
    10  // Until NotADirectory is implemented, read the underlying error raised by
    11  // wasi-libc. See https://github.com/rust-lang/rust/issues/86442
    12  use libc::ENOTDIR;
    13  
    14  fn main() {
    15      let args: Vec<String> = env::args().collect();
    16  
    17      match args[1].as_str() {
    18          "ls" => {
    19              main_ls(&args[2]);
    20              if args.len() == 4 && args[3].as_str() == "repeat" {
    21                  main_ls(&args[2]);
    22              }
    23          }
    24          "stat" => main_stat(),
    25          "sock" => main_sock(),
    26          _ => {
    27              writeln!(io::stderr(), "unknown command: {}", args[1]).unwrap();
    28              exit(1);
    29          }
    30      }
    31  }
    32  
    33  fn main_ls(dir_name: &String) {
    34      match fs::read_dir(dir_name) {
    35          Ok(paths) => {
    36              for ent in paths.into_iter() {
    37                  println!("{}", ent.unwrap().path().display());
    38              }
    39          }
    40          Err(e) => {
    41              if let Some(error_code) = e.raw_os_error() {
    42                  if error_code == ENOTDIR {
    43                      println!("ENOTDIR");
    44                  } else {
    45                      println!("errno=={}", error_code);
    46                  }
    47              } else {
    48                  writeln!(io::stderr(), "failed to read directory: {}", e).unwrap();
    49              }
    50          }
    51      }
    52  }
    53  
    54  extern crate libc;
    55  
    56  fn main_stat() {
    57      unsafe {
    58          println!("stdin isatty: {}", libc::isatty(0) != 0);
    59          println!("stdout isatty: {}", libc::isatty(1) != 0);
    60          println!("stderr isatty: {}", libc::isatty(2) != 0);
    61          println!("/ isatty: {}", libc::isatty(3) != 0);
    62      }
    63  }
    64  
    65  fn main_sock() {
    66      // Get a listener from the pre-opened file descriptor.
    67      // The listener is the first pre-open, with a file-descriptor of 3.
    68      let listener = unsafe { TcpListener::from_raw_fd(3) };
    69      for conn in listener.incoming() {
    70          match conn {
    71              Ok(mut conn) => {
    72                  // Do a blocking read of up to 32 bytes.
    73                  // Note: the test should write: "wazero", so that's all we should read.
    74                  let mut data = [0 as u8; 32];
    75                  match conn.read(&mut data) {
    76                      Ok(size) => {
    77                          let text = from_utf8(&data[0..size]).unwrap();
    78                          println!("{}", text);
    79  
    80                          // Exit instead of accepting another connection.
    81                          exit(0);
    82                      },
    83                      Err(e) => writeln!(io::stderr(), "failed to read data: {}", e).unwrap(),
    84                  } {}
    85              }
    86              Err(e) => writeln!(io::stderr(), "failed to read connection: {}", e).unwrap(),
    87          }
    88      }
    89  }