relibc/header/sys_time/mod.rs
1//! `sys/time.h` implementation.
2//!
3//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/sys_time.h.html>.
4//!
5//! Note that since the Open Group Base Specifications Issue 8, the
6//! [`timeval`] struct has been moved to
7//! the [`sys/select.h`](crate::header::sys_select) header.
8
9use crate::{
10 c_str::CStr,
11 error::ResultExt,
12 header::{sys_select::timeval, time::timespec},
13 out::Out,
14 platform::{
15 Pal, PalSignal, Sys,
16 types::{c_char, c_int, c_long},
17 },
18};
19
20/// See <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_time.h.html>.
21///
22/// # Deprecation
23/// The `ITIMER_REAL` symbolic constant was marked obsolescent in the Open
24/// Group Base Specifications Issue 7, and removed in Issue 8.
25#[deprecated]
26pub const ITIMER_REAL: c_int = 0;
27
28/// See <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_time.h.html>.
29///
30/// # Deprecation
31/// The `ITIMER_VIRTUAL` symbolic constant was marked obsolescent in the Open
32/// Group Base Specifications Issue 7, and removed in Issue 8.
33#[deprecated]
34pub const ITIMER_VIRTUAL: c_int = 1;
35
36/// See <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_time.h.html>.
37///
38/// # Deprecation
39/// The `ITIMER_PROF` symbolic constant was marked obsolescent in the Open
40/// Group Base Specifications Issue 7, and removed in Issue 8.
41#[deprecated]
42pub const ITIMER_PROF: c_int = 2;
43
44/// See <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sys_time.h.html>.
45///
46/// # Deprecation
47/// The `itimerval` struct was marked obsolescent in the Open Group Base
48/// Specifications Issue 7, and removed in Issue 8.
49#[deprecated]
50#[repr(C)]
51#[derive(Default)]
52pub struct itimerval {
53 pub it_interval: timeval,
54 pub it_value: timeval,
55}
56
57/// Non-POSIX, see <https://www.man7.org/linux/man-pages/man2/gettimeofday.2.html>.
58#[repr(C)]
59#[derive(Default)]
60pub struct timezone {
61 pub tz_minuteswest: c_int,
62 pub tz_dsttime: c_int,
63}
64
65/// See <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getitimer.html>.
66///
67/// # Deprecation
68/// The `getitimer()` function was marked obsolescent in the Open Group Base
69/// Specifications Issue 7, and removed in Issue 8.
70#[deprecated]
71#[allow(deprecated)]
72#[unsafe(no_mangle)]
73pub unsafe extern "C" fn getitimer(which: c_int, value: *mut itimerval) -> c_int {
74 Sys::getitimer(which, unsafe { &mut *value })
75 .map(|()| 0)
76 .or_minus_one_errno()
77}
78
79/// See <https://pubs.opengroup.org/onlinepubs/9699919799/functions/gettimeofday.html>.
80///
81/// See also <https://www.man7.org/linux/man-pages/man2/gettimeofday.2.html>
82/// for further details on the `tzp` argument.
83///
84/// # Deprecation
85/// The `gettimeofday()` function was marked obsolescent in the Open Group Base
86/// Specifications Issue 7, and removed in Issue 8.
87#[deprecated]
88#[unsafe(no_mangle)]
89pub unsafe extern "C" fn gettimeofday(tp: *mut timeval, tzp: *mut timezone) -> c_int {
90 Sys::gettimeofday(unsafe { Out::nonnull(tp) }, unsafe { Out::nullable(tzp) })
91 .map(|()| 0)
92 .or_minus_one_errno()
93}
94
95// `select()` declared in `sys/select.h`, as specified in modern POSIX
96
97/// See <https://pubs.opengroup.org/onlinepubs/9699919799/functions/getitimer.html>.
98///
99/// # Deprecation
100/// The `setitimer()` function was marked obsolescent in the Open Group Base
101/// Specifications Issue 7, and removed in Issue 8.
102#[deprecated]
103#[allow(deprecated)]
104#[unsafe(no_mangle)]
105pub unsafe extern "C" fn setitimer(
106 which: c_int,
107 value: *const itimerval,
108 ovalue: *mut itimerval,
109) -> c_int {
110 // TODO setitimer is unimplemented on Redox
111 Sys::setitimer(which, unsafe { &*value }, unsafe { ovalue.as_mut() })
112 .map(|()| 0)
113 .or_minus_one_errno()
114}
115
116/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/utimes.html>.
117///
118/// # Deprecation
119/// The `utimes()` function was marked legacy in the Open Group Base
120/// Specifications Issue 6, and then unmarked in Issue 7.
121#[deprecated]
122#[unsafe(no_mangle)]
123pub unsafe extern "C" fn utimes(path: *const c_char, times: *const timeval) -> c_int {
124 let path = unsafe { CStr::from_ptr(path) };
125 let times_spec = if times.is_null() {
126 None
127 } else {
128 Some([
129 timespec {
130 tv_sec: unsafe { (*times.offset(0)).tv_sec },
131 tv_nsec: c_long::from(unsafe { (*times.offset(0)).tv_usec }) * 1000,
132 },
133 timespec {
134 tv_sec: unsafe { (*times.offset(1)).tv_sec },
135 tv_nsec: c_long::from(unsafe { (*times.offset(1)).tv_usec }) * 1000,
136 },
137 ])
138 };
139 let times_ptr = match ×_spec {
140 Some(times_spec) => times_spec.as_ptr(),
141 // Nullptr is valid here, it means "use current time"
142 None => core::ptr::null(),
143 };
144 unsafe { Sys::utimens(path, times_ptr) }
145 .map(|()| 0)
146 .or_minus_one_errno()
147}