@@ -5,6 +5,7 @@ use alloc::ffi::CString;
5
5
use core:: alloc:: { GlobalAlloc , Layout } ;
6
6
use core:: ffi:: { CStr , c_char} ;
7
7
use core:: marker:: PhantomData ;
8
+ use core:: ptr:: null;
8
9
9
10
use dirent_display:: Dirent64Display ;
10
11
use hermit_sync:: Lazy ;
@@ -21,7 +22,7 @@ pub use self::system::*;
21
22
pub use self :: tasks:: * ;
22
23
pub use self :: timer:: * ;
23
24
use crate :: env;
24
- use crate :: errno:: Errno ;
25
+ use crate :: errno:: { Errno , ToErrno } ;
25
26
use crate :: executor:: block_on;
26
27
use crate :: fd:: {
27
28
self , AccessOption , AccessPermission , EventFlags , FileDescriptor , OpenOption , PollFd ,
@@ -348,27 +349,40 @@ pub unsafe extern "C" fn sys_open(name: *const c_char, flags: i32, mode: u32) ->
348
349
349
350
#[ hermit_macro:: system]
350
351
#[ unsafe( no_mangle) ]
351
- pub unsafe extern "C" fn sys_getcwd ( buf : * mut c_char , size : usize ) -> i32 {
352
- let cwd = crate :: fs:: get_cwd ( ) ;
352
+ pub unsafe extern "C" fn sys_getcwd ( buf : * mut c_char , size : usize ) -> * const c_char {
353
+ let error = |e : Errno | {
354
+ e. set_errno ( ) ;
355
+ null :: < c_char > ( )
356
+ } ;
357
+
358
+ if size == 0 {
359
+ return error ( Errno :: Inval ) ;
360
+ }
361
+
362
+ let cwd = fs:: get_cwd ( ) ;
353
363
if let Err ( e) = cwd {
354
- return -i32 :: from ( e) ;
364
+ return error ( e) ;
355
365
}
356
366
357
367
let Ok ( cwd) = cwd else { unreachable ! ( ) } ;
358
368
359
369
let Ok ( cwd) = CString :: new ( cwd) else {
360
- return -i32 :: from ( Errno :: Noent ) ; // extremely unlikely
370
+ return error ( Errno :: Noent ) ;
361
371
} ;
362
372
363
373
if ( cwd. count_bytes ( ) + 1 ) > size {
364
- return -i32:: from ( Errno :: Range ) ;
374
+ return error ( Errno :: Range ) ;
375
+ }
376
+
377
+ if buf. is_null ( ) {
378
+ return cwd. into_raw ( ) ;
365
379
}
366
380
367
381
unsafe {
368
382
buf. copy_from ( cwd. as_ptr ( ) , size) ;
369
383
}
370
384
371
- i32 :: try_from ( cwd . count_bytes ( ) + 1 ) . unwrap_or ( -i32 :: from ( Errno :: Range ) )
385
+ buf
372
386
}
373
387
374
388
#[ hermit_macro:: system]
0 commit comments