Skip to content
Snippets Groups Projects
Commit 64f93fe6 authored by AdminXVII's avatar AdminXVII
Browse files

Implement the truncate function

parent 5156a13b
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ use core::{convert::TryFrom, mem, ptr, slice}; ...@@ -4,7 +4,7 @@ use core::{convert::TryFrom, mem, ptr, slice};
use crate::{ use crate::{
c_str::CStr, c_str::CStr,
header::{errno, limits, stdlib::getenv, sys_ioctl, sys_time, termios, time::timespec}, header::{errno, limits, fcntl::sys::O_WRONLY, stdlib::getenv, sys_ioctl, sys_time, termios, time::timespec},
platform::{self, types::*, Pal, Sys}, platform::{self, types::*, Pal, Sys},
}; };
use alloc::collections::LinkedList; use alloc::collections::LinkedList;
...@@ -647,9 +647,19 @@ pub extern "C" fn tcsetpgrp(fd: c_int, pgrp: pid_t) -> c_int { ...@@ -647,9 +647,19 @@ pub extern "C" fn tcsetpgrp(fd: c_int, pgrp: pid_t) -> c_int {
pgrp pgrp
} }
// #[no_mangle] #[no_mangle]
pub extern "C" fn truncate(path: *const c_char, length: off_t) -> c_int { pub extern "C" fn truncate(path: *const c_char, length: off_t) -> c_int {
unimplemented!(); let file = unsafe { CStr::from_ptr(path) };
let fd = Sys::open(file, O_WRONLY, 0);
if fd < 0 {
return -1;
}
let res = ftruncate(fd, length);
Sys::close(fd);
res
} }
#[no_mangle] #[no_mangle]
......
...@@ -17,4 +17,8 @@ int main(void) { ...@@ -17,4 +17,8 @@ int main(void) {
int c = close(fd); int c = close(fd);
ERROR_IF(close, c, == -1); ERROR_IF(close, c, == -1);
UNEXP_IF(close, c, != 0); UNEXP_IF(close, c, != 0);
status = truncate("ftruncate.out", 100);
ERROR_IF(truncate, status, == -1);
UNEXP_IF(truncate, status, != 0);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment