55
66// spell-checker:ignore fname, ftype, tname, fpath, specfile, testfile, unspec, ifile, ofile, outfile, fullblock, urand, fileio, atoe, atoibm, behaviour, bmax, bremain, cflags, creat, ctable, ctty, datastructures, doesnt, etoa, fileout, fname, gnudd, iconvflags, iseek, nocache, noctty, noerror, nofollow, nolinks, nonblock, oconvflags, oseek, outfile, parseargs, rlen, rmax, rremain, rsofar, rstat, sigusr, virtio, wlen, wstat, zram, oconv canonicalized FADV DONTNEED ESPIPE SPIPE bufferedoutput, SETFL
77
8+ // TODO: Remove once https://github.com/rust-lang/rust/issues/71213 is stabilized.
9+ #![ cfg_attr( all( nightly, target_os = "wasi" ) , feature( wasi_ext) ) ]
10+
811mod blocks;
912mod bufferedoutput;
1013mod conversion_tables;
@@ -28,17 +31,18 @@ use uucore::translate;
2831use std:: cmp;
2932use std:: env;
3033use std:: ffi:: OsString ;
31- #[ cfg( unix) ]
34+ #[ cfg( any ( unix, all ( nightly , target_os = "wasi" ) ) ) ]
3235use std:: fs:: Metadata ;
3336use std:: fs:: { File , OpenOptions } ;
3437use std:: io:: { self , Read , Seek , SeekFrom , Write } ;
38+ #[ cfg( any( unix, all( nightly, target_os = "wasi" ) ) ) ]
39+ use std:: os:: fd:: { AsRawFd , FromRawFd } ;
40+ #[ cfg( unix) ]
41+ use std:: os:: unix:: fs:: FileTypeExt ;
3542#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
3643use std:: os:: unix:: fs:: OpenOptionsExt ;
37- #[ cfg( unix) ]
38- use std:: os:: unix:: {
39- fs:: FileTypeExt ,
40- io:: { AsRawFd , FromRawFd } ,
41- } ;
44+ #[ cfg( all( nightly, target_os = "wasi" ) ) ]
45+ use std:: os:: wasi:: fs:: FileTypeExt ;
4246#[ cfg( windows) ]
4347use std:: os:: windows:: { fs:: MetadataExt , io:: AsHandle } ;
4448use std:: path:: Path ;
@@ -50,9 +54,11 @@ use std::time::{Duration, Instant};
5054use clap:: { Arg , Command } ;
5155use num_integer:: Integer ;
5256use uucore:: display:: Quotable ;
53- use uucore:: error:: { FromIo , UResult } ;
5457#[ cfg( unix) ]
55- use uucore:: error:: { USimpleError , set_exit_code} ;
58+ use uucore:: error:: USimpleError ;
59+ #[ cfg( any( unix, all( nightly, target_os = "wasi" ) ) ) ]
60+ use uucore:: error:: set_exit_code;
61+ use uucore:: error:: { FromIo , UResult } ;
5662#[ cfg( any( target_os = "linux" , target_os = "android" , target_os = "freebsd" ) ) ]
5763use uucore:: show_if_err;
5864use uucore:: { format_usage, show_error} ;
@@ -213,14 +219,14 @@ impl AlignedBuf {
213219/// fine-grained access to reading from stdin.
214220enum Source {
215221 /// Input from stdin.
216- #[ cfg( not( unix) ) ]
222+ #[ cfg( not( any ( unix, all ( nightly , target_os = "wasi" ) ) ) ) ]
217223 Stdin ( io:: Stdin ) ,
218224
219225 /// Input from a file.
220226 File ( File ) ,
221227
222228 /// Input from stdin, opened from its file descriptor.
223- #[ cfg( unix) ]
229+ #[ cfg( any ( unix, all ( nightly , target_os = "wasi" ) ) ) ]
224230 StdinFile ( File ) ,
225231
226232 /// Input from a named pipe, also known as a FIFO.
@@ -236,7 +242,7 @@ impl Source {
236242 /// the [`File`] parameter. You can use this instead of
237243 /// `Source::Stdin` to allow reading from stdin without consuming
238244 /// the entire contents of stdin when this process terminates.
239- #[ cfg( unix) ]
245+ #[ cfg( any ( unix, all ( nightly , target_os = "wasi" ) ) ) ]
240246 fn stdin_as_file ( ) -> Self {
241247 let fd = io:: stdin ( ) . as_raw_fd ( ) ;
242248 let f = unsafe { File :: from_raw_fd ( fd) } ;
@@ -245,7 +251,7 @@ impl Source {
245251
246252 fn skip ( & mut self , n : u64 , ibs : usize ) -> io:: Result < u64 > {
247253 match self {
248- #[ cfg( not( unix) ) ]
254+ #[ cfg( not( any ( unix, all ( nightly , target_os = "wasi" ) ) ) ) ]
249255 Self :: Stdin ( stdin) => {
250256 let m = uucore:: io:: read_and_discard ( stdin, n, ibs) ?;
251257 if m < n {
@@ -256,7 +262,7 @@ impl Source {
256262 }
257263 Ok ( m)
258264 }
259- #[ cfg( unix) ]
265+ #[ cfg( any ( unix, all ( nightly , target_os = "wasi" ) ) ) ]
260266 Self :: StdinFile ( f) => {
261267 if let Ok ( Some ( len) ) = try_get_len_of_block_device ( f)
262268 && len < n
@@ -335,10 +341,10 @@ impl Source {
335341impl Read for Source {
336342 fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
337343 match self {
338- #[ cfg( not( unix) ) ]
344+ #[ cfg( not( any ( unix, all ( nightly , target_os = "wasi" ) ) ) ) ]
339345 Self :: Stdin ( stdin) => stdin. read ( buf) ,
340346 Self :: File ( f) => f. read ( buf) ,
341- #[ cfg( unix) ]
347+ #[ cfg( any ( unix, all ( nightly , target_os = "wasi" ) ) ) ]
342348 Self :: StdinFile ( f) => f. read ( buf) ,
343349 #[ cfg( unix) ]
344350 Self :: Fifo ( f) => f. read ( buf) ,
@@ -376,9 +382,9 @@ impl<'a> Input<'a> {
376382 _ => Source :: Stdin ( io:: stdin ( ) ) ,
377383 }
378384 } ;
379- #[ cfg( all( not( unix) , not( windows) ) ) ]
385+ #[ cfg( all( not( unix) , not( windows) , not ( all ( nightly , target_os = "wasi" ) ) ) ) ]
380386 let mut src = Source :: Stdin ( io:: stdin ( ) ) ;
381- #[ cfg( unix) ]
387+ #[ cfg( any ( unix, all ( nightly , target_os = "wasi" ) ) ) ]
382388 let mut src = Source :: stdin_as_file ( ) ;
383389 #[ cfg( unix) ]
384390 if let Source :: StdinFile ( f) = & src
@@ -1538,7 +1544,7 @@ fn is_stdout_redirected_to_seekable_file() -> bool {
15381544}
15391545
15401546/// Try to get the len if it is a block device
1541- #[ cfg( unix) ]
1547+ #[ cfg( any ( unix, all ( nightly , target_os = "wasi" ) ) ) ]
15421548fn try_get_len_of_block_device ( file : & mut File ) -> io:: Result < Option < u64 > > {
15431549 let ftype = file. metadata ( ) ?. file_type ( ) ;
15441550 if !ftype. is_block_device ( ) {
0 commit comments