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, wlen, wstat oconv canonicalized fadvise Fadvise FADV DONTNEED ESPIPE bufferedoutput, SETFL
77
8+ #![ cfg_attr( all( target_os = "wasi" , nightly) , feature( wasi_ext) ) ]
9+
810mod blocks;
911mod bufferedoutput;
1012mod conversion_tables;
@@ -31,19 +33,20 @@ use uucore::translate;
3133use std:: cmp;
3234use std:: env;
3335use std:: ffi:: OsString ;
34- #[ cfg( unix) ]
36+ #[ cfg( any ( unix, all ( target_os = "wasi" , nightly ) ) ) ]
3537use std:: fs:: Metadata ;
3638use std:: fs:: { File , OpenOptions } ;
3739use std:: io:: { self , Read , Seek , SeekFrom , Write } ;
3840#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
3941use std:: os:: fd:: AsFd ;
42+ #[ cfg( any( unix, all( target_os = "wasi" , nightly) ) ) ]
43+ use std:: os:: fd:: { AsRawFd , FromRawFd } ;
44+ #[ cfg( unix) ]
45+ use std:: os:: unix:: fs:: FileTypeExt ;
4046#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
4147use std:: os:: unix:: fs:: OpenOptionsExt ;
42- #[ cfg( unix) ]
43- use std:: os:: unix:: {
44- fs:: FileTypeExt ,
45- io:: { AsRawFd , FromRawFd } ,
46- } ;
48+ #[ cfg( all( target_os = "wasi" , nightly) ) ]
49+ use std:: os:: wasi:: fs:: FileTypeExt ;
4750#[ cfg( windows) ]
4851use std:: os:: windows:: { fs:: MetadataExt , io:: AsHandle } ;
4952use std:: path:: Path ;
@@ -60,9 +63,11 @@ use nix::{
6063 fcntl:: { PosixFadviseAdvice , posix_fadvise} ,
6164} ;
6265use uucore:: display:: Quotable ;
63- use uucore:: error:: { FromIo , UResult } ;
6466#[ cfg( unix) ]
65- use uucore:: error:: { USimpleError , set_exit_code} ;
67+ use uucore:: error:: USimpleError ;
68+ #[ cfg( any( unix, all( target_os = "wasi" , nightly) ) ) ]
69+ use uucore:: error:: set_exit_code;
70+ use uucore:: error:: { FromIo , UResult } ;
6671#[ cfg( target_os = "linux" ) ]
6772use uucore:: show_if_err;
6873use uucore:: { format_usage, show_error} ;
@@ -211,14 +216,14 @@ fn read_and_discard<R: Read>(reader: &mut R, n: u64, buf_size: usize) -> io::Res
211216/// fine-grained access to reading from stdin.
212217enum Source {
213218 /// Input from stdin.
214- #[ cfg( not( unix) ) ]
219+ #[ cfg( not( any ( unix, all ( target_os = "wasi" , nightly ) ) ) ) ]
215220 Stdin ( io:: Stdin ) ,
216221
217222 /// Input from a file.
218223 File ( File ) ,
219224
220225 /// Input from stdin, opened from its file descriptor.
221- #[ cfg( unix) ]
226+ #[ cfg( any ( unix, all ( target_os = "wasi" , nightly ) ) ) ]
222227 StdinFile ( File ) ,
223228
224229 /// Input from a named pipe, also known as a FIFO.
@@ -234,7 +239,7 @@ impl Source {
234239 /// the [`File`] parameter. You can use this instead of
235240 /// `Source::Stdin` to allow reading from stdin without consuming
236241 /// the entire contents of stdin when this process terminates.
237- #[ cfg( unix) ]
242+ #[ cfg( any ( unix, all ( target_os = "wasi" , nightly ) ) ) ]
238243 fn stdin_as_file ( ) -> Self {
239244 let fd = io:: stdin ( ) . as_raw_fd ( ) ;
240245 let f = unsafe { File :: from_raw_fd ( fd) } ;
@@ -243,7 +248,7 @@ impl Source {
243248
244249 fn skip ( & mut self , n : u64 , ibs : usize ) -> io:: Result < u64 > {
245250 match self {
246- #[ cfg( not( unix) ) ]
251+ #[ cfg( not( any ( unix, all ( target_os = "wasi" , nightly ) ) ) ) ]
247252 Self :: Stdin ( stdin) => {
248253 let m = read_and_discard ( stdin, n, ibs) ?;
249254 if m < n {
@@ -254,7 +259,7 @@ impl Source {
254259 }
255260 Ok ( m)
256261 }
257- #[ cfg( unix) ]
262+ #[ cfg( any ( unix, all ( target_os = "wasi" , nightly ) ) ) ]
258263 Self :: StdinFile ( f) => {
259264 if let Ok ( Some ( len) ) = try_get_len_of_block_device ( f)
260265 && len < n
@@ -331,10 +336,10 @@ impl Source {
331336impl Read for Source {
332337 fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
333338 match self {
334- #[ cfg( not( unix) ) ]
339+ #[ cfg( not( any ( unix, all ( target_os = "wasi" , nightly ) ) ) ) ]
335340 Self :: Stdin ( stdin) => stdin. read ( buf) ,
336341 Self :: File ( f) => f. read ( buf) ,
337- #[ cfg( unix) ]
342+ #[ cfg( any ( unix, all ( target_os = "wasi" , nightly ) ) ) ]
338343 Self :: StdinFile ( f) => f. read ( buf) ,
339344 #[ cfg( unix) ]
340345 Self :: Fifo ( f) => f. read ( buf) ,
@@ -378,9 +383,9 @@ impl<'a> Input<'a> {
378383 Source :: Stdin ( io:: stdin ( ) )
379384 }
380385 } ;
381- #[ cfg( all( not( unix) , not( windows) ) ) ]
386+ #[ cfg( all( not( unix) , not( windows) , not ( all ( target_os = "wasi" , nightly ) ) ) ) ]
382387 let mut src = Source :: Stdin ( io:: stdin ( ) ) ;
383- #[ cfg( unix) ]
388+ #[ cfg( any ( unix, all ( target_os = "wasi" , nightly ) ) ) ]
384389 let mut src = Source :: stdin_as_file ( ) ;
385390 #[ cfg( unix) ]
386391 if let Source :: StdinFile ( f) = & src
@@ -1490,7 +1495,7 @@ fn is_stdout_redirected_to_seekable_file() -> bool {
14901495}
14911496
14921497/// Try to get the len if it is a block device
1493- #[ cfg( unix) ]
1498+ #[ cfg( any ( unix, all ( target_os = "wasi" , nightly ) ) ) ]
14941499fn try_get_len_of_block_device ( file : & mut File ) -> io:: Result < Option < u64 > > {
14951500 let ftype = file. metadata ( ) ?. file_type ( ) ;
14961501 if !ftype. is_block_device ( ) {
0 commit comments