1+ use core:: str;
12use std:: fs:: { self , File , FileType } ;
23use std:: io:: { self , prelude:: * } ;
34use std:: { string:: String , vec:: Vec } ;
@@ -69,7 +70,10 @@ const fn file_perm_to_rwx(mode: u32) -> [u8; 9] {
6970}
7071
7172fn do_ls ( args : & str ) {
72- let current_dir = std:: env:: current_dir ( ) . unwrap ( ) ;
73+ let current_dir = match std:: env:: current_dir ( ) {
74+ Ok ( current_dir) => current_dir,
75+ Err ( err) => return println ! ( "Failed to access the current directory: {err}" ) ,
76+ } ;
7377 let args = if args. is_empty ( ) {
7478 path_to_str ( & current_dir)
7579 } else {
@@ -83,8 +87,8 @@ fn do_ls(args: &str) {
8387 let file_type = metadata. file_type ( ) ;
8488 let file_type_char = file_type_to_char ( file_type) ;
8589 let rwx = file_perm_to_rwx ( metadata. permissions ( ) . mode ( ) ) ;
86- let rwx = unsafe { core :: str:: from_utf8_unchecked ( & rwx) } ;
87- println ! ( "{}{ } {:>8} {}" , file_type_char , rwx , size , entry ) ;
90+ let rwx = str:: from_utf8 ( & rwx) . unwrap ( ) ;
91+ println ! ( "{file_type_char}{rwx } {size :>8} {entry}" ) ;
8892 Ok ( ( ) )
8993 }
9094
@@ -95,7 +99,7 @@ fn do_ls(args: &str) {
9599 }
96100
97101 if print_name {
98- println ! ( "{}:" , name ) ;
102+ println ! ( "{name }:" ) ;
99103 }
100104 let mut entries = fs:: read_dir ( name) ?
101105 . filter_map ( |e| e. ok ( ) )
@@ -176,7 +180,7 @@ fn do_echo(args: &str) {
176180 print_err ! ( "echo" , fname, e) ;
177181 }
178182 } else {
179- println ! ( "{}" , args )
183+ println ! ( "{args}" )
180184 }
181185}
182186
@@ -241,35 +245,34 @@ fn do_cd(mut args: &str) {
241245}
242246
243247fn do_pwd ( _args : & str ) {
244- let pwd = std:: env:: current_dir ( ) . unwrap ( ) ;
245- println ! ( "{}" , path_to_str( & pwd) ) ;
248+ match std:: env:: current_dir ( ) {
249+ Ok ( pwd) => println ! ( "{}" , path_to_str( & pwd) ) ,
250+ Err ( err) => println ! ( "Failed to access the current directory: {err}" ) ,
251+ }
246252}
247253
248254fn do_uname ( _args : & str ) {
249255 let arch = option_env ! ( "AX_ARCH" ) . unwrap_or ( "" ) ;
250256 let platform = option_env ! ( "AX_PLATFORM" ) . unwrap_or ( "" ) ;
251257 #[ cfg( feature = "axstd" ) ]
252- let smp = if std:: thread:: available_parallelism ( ) . unwrap ( ) . get ( ) == 1 {
258+ let smp = if std:: thread:: available_parallelism ( )
259+ . map ( |n| n. get ( ) == 1 )
260+ . unwrap_or ( true )
261+ {
253262 ""
254263 } else {
255264 " SMP"
256265 } ;
257266 #[ cfg( not( feature = "axstd" ) ) ]
258267 let smp = "" ;
259268 let version = option_env ! ( "CARGO_PKG_VERSION" ) . unwrap_or ( "0.1.0" ) ;
260- println ! (
261- "ArceOS {ver}{smp} {arch} {plat}" ,
262- ver = version,
263- smp = smp,
264- arch = arch,
265- plat = platform,
266- ) ;
269+ println ! ( "ArceOS {version}{smp} {arch} {platform}" ) ;
267270}
268271
269272fn do_help ( _args : & str ) {
270273 println ! ( "Available commands:" ) ;
271274 for ( name, _) in CMD_TABLE {
272- println ! ( " {}" , name ) ;
275+ println ! ( " {name}" ) ;
273276 }
274277}
275278
@@ -279,7 +282,10 @@ fn do_exit(_args: &str) {
279282}
280283
281284pub fn run_cmd ( line : & [ u8 ] ) {
282- let line_str = unsafe { core:: str:: from_utf8_unchecked ( line) } ;
285+ let Ok ( line_str) = str:: from_utf8 ( line) else {
286+ println ! ( "Please enter a valid utf-8 string as the command." ) ;
287+ return ;
288+ } ;
283289 let ( cmd, args) = split_whitespace ( line_str) ;
284290 if !cmd. is_empty ( ) {
285291 for ( name, func) in CMD_TABLE {
@@ -288,7 +294,7 @@ pub fn run_cmd(line: &[u8]) {
288294 return ;
289295 }
290296 }
291- println ! ( "{}: command not found" , cmd ) ;
297+ println ! ( "{cmd }: command not found" ) ;
292298 }
293299}
294300
0 commit comments