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} ;
@@ -212,14 +217,14 @@ fn read_and_discard<R: Read>(reader: &mut R, n: u64, buf_size: usize) -> io::Res
212217/// fine-grained access to reading from stdin.
213218enum Source {
214219 /// Input from stdin.
215- #[ cfg( not( unix) ) ]
220+ #[ cfg( not( any ( unix, all ( target_os = "wasi" , nightly ) ) ) ) ]
216221 Stdin ( io:: Stdin ) ,
217222
218223 /// Input from a file.
219224 File ( File ) ,
220225
221226 /// Input from stdin, opened from its file descriptor.
222- #[ cfg( unix) ]
227+ #[ cfg( any ( unix, all ( target_os = "wasi" , nightly ) ) ) ]
223228 StdinFile ( File ) ,
224229
225230 /// Input from a named pipe, also known as a FIFO.
@@ -235,7 +240,7 @@ impl Source {
235240 /// the [`File`] parameter. You can use this instead of
236241 /// `Source::Stdin` to allow reading from stdin without consuming
237242 /// the entire contents of stdin when this process terminates.
238- #[ cfg( unix) ]
243+ #[ cfg( any ( unix, all ( target_os = "wasi" , nightly ) ) ) ]
239244 fn stdin_as_file ( ) -> Self {
240245 let fd = io:: stdin ( ) . as_raw_fd ( ) ;
241246 let f = unsafe { File :: from_raw_fd ( fd) } ;
@@ -244,7 +249,7 @@ impl Source {
244249
245250 fn skip ( & mut self , n : u64 , ibs : usize ) -> io:: Result < u64 > {
246251 match self {
247- #[ cfg( not( unix) ) ]
252+ #[ cfg( not( any ( unix, all ( target_os = "wasi" , nightly ) ) ) ) ]
248253 Self :: Stdin ( stdin) => {
249254 let m = read_and_discard ( stdin, n, ibs) ?;
250255 if m < n {
@@ -255,7 +260,7 @@ impl Source {
255260 }
256261 Ok ( m)
257262 }
258- #[ cfg( unix) ]
263+ #[ cfg( any ( unix, all ( target_os = "wasi" , nightly ) ) ) ]
259264 Self :: StdinFile ( f) => {
260265 if let Ok ( Some ( len) ) = try_get_len_of_block_device ( f)
261266 && len < n
@@ -332,10 +337,10 @@ impl Source {
332337impl Read for Source {
333338 fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
334339 match self {
335- #[ cfg( not( unix) ) ]
340+ #[ cfg( not( any ( unix, all ( target_os = "wasi" , nightly ) ) ) ) ]
336341 Self :: Stdin ( stdin) => stdin. read ( buf) ,
337342 Self :: File ( f) => f. read ( buf) ,
338- #[ cfg( unix) ]
343+ #[ cfg( any ( unix, all ( target_os = "wasi" , nightly ) ) ) ]
339344 Self :: StdinFile ( f) => f. read ( buf) ,
340345 #[ cfg( unix) ]
341346 Self :: Fifo ( f) => f. read ( buf) ,
@@ -379,9 +384,9 @@ impl<'a> Input<'a> {
379384 Source :: Stdin ( io:: stdin ( ) )
380385 }
381386 } ;
382- #[ cfg( all( not( unix) , not( windows) ) ) ]
387+ #[ cfg( all( not( unix) , not( windows) , not ( all ( target_os = "wasi" , nightly ) ) ) ) ]
383388 let mut src = Source :: Stdin ( io:: stdin ( ) ) ;
384- #[ cfg( unix) ]
389+ #[ cfg( any ( unix, all ( target_os = "wasi" , nightly ) ) ) ]
385390 let mut src = Source :: stdin_as_file ( ) ;
386391 #[ cfg( unix) ]
387392 if let Source :: StdinFile ( f) = & src
@@ -1491,7 +1496,7 @@ fn is_stdout_redirected_to_seekable_file() -> bool {
14911496}
14921497
14931498/// Try to get the len if it is a block device
1494- #[ cfg( unix) ]
1499+ #[ cfg( any ( unix, all ( target_os = "wasi" , nightly ) ) ) ]
14951500fn try_get_len_of_block_device ( file : & mut File ) -> io:: Result < Option < u64 > > {
14961501 let ftype = file. metadata ( ) ?. file_type ( ) ;
14971502 if !ftype. is_block_device ( ) {
0 commit comments