Skip to main content

relibc/header/errno/
mod.rs

1//! `errno.h` implementation.
2//!
3//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/errno.h.html>.
4
5use crate::platform::{
6    self,
7    types::{c_char, c_int},
8};
9
10//TODO: Consider removing, provided for compatibility with newlib
11#[unsafe(no_mangle)]
12pub extern "C" fn __errno() -> *mut c_int {
13    __errno_location()
14}
15
16/// Provide a pointer to relibc's internal `errno` variable.
17///
18/// This is the basis of the C-facing macro definition of `errno`, and should
19/// not be used directly.
20#[unsafe(no_mangle)]
21pub extern "C" fn __errno_location() -> *mut c_int {
22    platform::ERRNO.as_ptr()
23}
24
25/// Get the name used to invoke the program, similar to `argv[0]`.
26///
27/// This provides the basis for the C-facing macro definition of
28/// `program_invocation_name`, and should not be used directly.
29///
30/// The `program_invocation_name` variable is a GNU extension.
31#[unsafe(no_mangle)]
32pub unsafe extern "C" fn __program_invocation_name() -> *mut *mut c_char {
33    &raw mut platform::program_invocation_name
34}
35
36/// Get the directory-less name used to invoke the program.
37///
38/// This provides the basis for the C-facing macro definition of
39/// `program_invocation_short_name`, and should not be used directly.
40///
41/// The `program_invocation_short_name` variable is a GNU extension.
42#[unsafe(no_mangle)]
43pub unsafe extern "C" fn __program_invocation_short_name() -> *mut *mut c_char {
44    &raw mut platform::program_invocation_short_name
45}
46
47pub const EPERM: c_int = 1; /* Operation not permitted */
48pub const ENOENT: c_int = 2; /* No such file or directory */
49pub const ESRCH: c_int = 3; /* No such process */
50pub const EINTR: c_int = 4; /* Interrupted system call */
51pub const EIO: c_int = 5; /* I/O error */
52pub const ENXIO: c_int = 6; /* No such device or address */
53pub const E2BIG: c_int = 7; /* Argument list too long */
54pub const ENOEXEC: c_int = 8; /* Exec format error */
55pub const EBADF: c_int = 9; /* Bad file number */
56pub const ECHILD: c_int = 10; /* No child processes */
57pub const EAGAIN: c_int = 11; /* Try again */
58pub const ENOMEM: c_int = 12; /* Out of memory */
59pub const EACCES: c_int = 13; /* Permission denied */
60pub const EFAULT: c_int = 14; /* Bad address */
61pub const ENOTBLK: c_int = 15; /* Block device required */
62pub const EBUSY: c_int = 16; /* Device or resource busy */
63pub const EEXIST: c_int = 17; /* File exists */
64pub const EXDEV: c_int = 18; /* Cross-device link */
65pub const ENODEV: c_int = 19; /* No such device */
66pub const ENOTDIR: c_int = 20; /* Not a directory */
67pub const EISDIR: c_int = 21; /* Is a directory */
68pub const EINVAL: c_int = 22; /* Invalid argument */
69pub const ENFILE: c_int = 23; /* File table overflow */
70pub const EMFILE: c_int = 24; /* Too many open files */
71pub const ENOTTY: c_int = 25; /* Not a typewriter */
72pub const ETXTBSY: c_int = 26; /* Text file busy */
73pub const EFBIG: c_int = 27; /* File too large */
74pub const ENOSPC: c_int = 28; /* No space left on device */
75pub const ESPIPE: c_int = 29; /* Illegal seek */
76pub const EROFS: c_int = 30; /* Read-only file system */
77pub const EMLINK: c_int = 31; /* Too many links */
78pub const EPIPE: c_int = 32; /* Broken pipe */
79pub const EDOM: c_int = 33; /* Math argument out of domain of func */
80pub const ERANGE: c_int = 34; /* Math result not representable */
81pub const EDEADLK: c_int = 35; /* Resource deadlock would occur */
82pub const ENAMETOOLONG: c_int = 36; /* File name too long */
83pub const ENOLCK: c_int = 37; /* No record locks available */
84pub const ENOSYS: c_int = 38; /* Function not implemented */
85pub const ENOTEMPTY: c_int = 39; /* Directory not empty */
86pub const ELOOP: c_int = 40; /* Too many symbolic links encountered */
87pub const EWOULDBLOCK: c_int = 41; /* Operation would block */
88pub const ENOMSG: c_int = 42; /* No message of desired type */
89pub const EIDRM: c_int = 43; /* Identifier removed */
90pub const ECHRNG: c_int = 44; /* Channel number out of range */
91pub const EL2NSYNC: c_int = 45; /* Level 2 not synchronized */
92pub const EL3HLT: c_int = 46; /* Level 3 halted */
93pub const EL3RST: c_int = 47; /* Level 3 reset */
94pub const ELNRNG: c_int = 48; /* Link number out of range */
95pub const EUNATCH: c_int = 49; /* Protocol driver not attached */
96pub const ENOCSI: c_int = 50; /* No CSI structure available */
97pub const EL2HLT: c_int = 51; /* Level 2 halted */
98pub const EBADE: c_int = 52; /* Invalid exchange */
99pub const EBADR: c_int = 53; /* Invalid request descriptor */
100pub const EXFULL: c_int = 54; /* Exchange full */
101pub const ENOANO: c_int = 55; /* No anode */
102pub const EBADRQC: c_int = 56; /* Invalid request code */
103pub const EBADSLT: c_int = 57; /* Invalid slot */
104pub const EDEADLOCK: c_int = 58; /* Resource deadlock would occur */
105pub const EBFONT: c_int = 59; /* Bad font file format */
106pub const ENOSTR: c_int = 60; /* Device not a stream */
107pub const ENODATA: c_int = 61; /* No data available */
108pub const ETIME: c_int = 62; /* Timer expired */
109pub const ENOSR: c_int = 63; /* Out of streams resources */
110pub const ENONET: c_int = 64; /* Machine is not on the network */
111pub const ENOPKG: c_int = 65; /* Package not installed */
112pub const EREMOTE: c_int = 66; /* Object is remote */
113pub const ENOLINK: c_int = 67; /* Link has been severed */
114pub const EADV: c_int = 68; /* Advertise error */
115pub const ESRMNT: c_int = 69; /* Srmount error */
116pub const ECOMM: c_int = 70; /* Communication error on send */
117pub const EPROTO: c_int = 71; /* Protocol error */
118pub const EMULTIHOP: c_int = 72; /* Multihop attempted */
119pub const EDOTDOT: c_int = 73; /* RFS specific error */
120pub const EBADMSG: c_int = 74; /* Not a data message */
121pub const EOVERFLOW: c_int = 75; /* Value too large for defined data type */
122pub const ENOTUNIQ: c_int = 76; /* Name not unique on network */
123pub const EBADFD: c_int = 77; /* File descriptor in bad state */
124pub const EREMCHG: c_int = 78; /* Remote address changed */
125pub const ELIBACC: c_int = 79; /* Can not access a needed shared library */
126pub const ELIBBAD: c_int = 80; /* Accessing a corrupted shared library */
127pub const ELIBSCN: c_int = 81; /* .lib section in a.out corrupted */
128pub const ELIBMAX: c_int = 82; /* Attempting to link in too many shared libraries */
129pub const ELIBEXEC: c_int = 83; /* Cannot exec a shared library directly */
130pub const EILSEQ: c_int = 84; /* Illegal byte sequence */
131pub const ERESTART: c_int = 85; /* Interrupted system call should be restarted */
132pub const ESTRPIPE: c_int = 86; /* Streams pipe error */
133pub const EUSERS: c_int = 87; /* Too many users */
134pub const ENOTSOCK: c_int = 88; /* Socket operation on non-socket */
135pub const EDESTADDRREQ: c_int = 89; /* Destination address required */
136pub const EMSGSIZE: c_int = 90; /* Message too long */
137pub const EPROTOTYPE: c_int = 91; /* Protocol wrong type for socket */
138pub const ENOPROTOOPT: c_int = 92; /* Protocol not available */
139pub const EPROTONOSUPPORT: c_int = 93; /* Protocol not supported */
140pub const ESOCKTNOSUPPORT: c_int = 94; /* Socket type not supported */
141pub const EOPNOTSUPP: c_int = 95; /* Operation not supported on transport endpoint */
142pub const ENOTSUP: c_int = EOPNOTSUPP; /* Not supported */
143pub const EPFNOSUPPORT: c_int = 96; /* Protocol family not supported */
144pub const EAFNOSUPPORT: c_int = 97; /* Address family not supported by protocol */
145pub const EADDRINUSE: c_int = 98; /* Address already in use */
146pub const EADDRNOTAVAIL: c_int = 99; /* Cannot assign requested address */
147pub const ENETDOWN: c_int = 100; /* Network is down */
148pub const ENETUNREACH: c_int = 101; /* Network is unreachable */
149pub const ENETRESET: c_int = 102; /* Network dropped connection because of reset */
150pub const ECONNABORTED: c_int = 103; /* Software caused connection abort */
151pub const ECONNRESET: c_int = 104; /* Connection reset by peer */
152pub const ENOBUFS: c_int = 105; /* No buffer space available */
153pub const EISCONN: c_int = 106; /* Transport endpoint is already connected */
154pub const ENOTCONN: c_int = 107; /* Transport endpoint is not connected */
155pub const ESHUTDOWN: c_int = 108; /* Cannot send after transport endpoint shutdown */
156pub const ETOOMANYREFS: c_int = 109; /* Too many references: cannot splice */
157pub const ETIMEDOUT: c_int = 110; /* Connection timed out */
158pub const ECONNREFUSED: c_int = 111; /* Connection refused */
159pub const EHOSTDOWN: c_int = 112; /* Host is down */
160pub const EHOSTUNREACH: c_int = 113; /* No route to host */
161pub const EALREADY: c_int = 114; /* Operation already in progress */
162pub const EINPROGRESS: c_int = 115; /* Operation now in progress */
163pub const ESTALE: c_int = 116; /* Stale NFS file handle */
164pub const EUCLEAN: c_int = 117; /* Structure needs cleaning */
165pub const ENOTNAM: c_int = 118; /* Not a XENIX named type file */
166pub const ENAVAIL: c_int = 119; /* No XENIX semaphores available */
167pub const EISNAM: c_int = 120; /* Is a named type file */
168pub const EREMOTEIO: c_int = 121; /* Remote I/O error */
169pub const EDQUOT: c_int = 122; /* Quota exceeded */
170pub const ENOMEDIUM: c_int = 123; /* No medium found */
171pub const EMEDIUMTYPE: c_int = 124; /* Wrong medium type */
172pub const ECANCELED: c_int = 125; /* Operation Canceled */
173pub const ENOKEY: c_int = 126; /* Required key not available */
174pub const EKEYEXPIRED: c_int = 127; /* Key has expired */
175pub const EKEYREVOKED: c_int = 128; /* Key has been revoked */
176pub const EKEYREJECTED: c_int = 129; /* Key was rejected by service */
177pub const EOWNERDEAD: c_int = 130; /* Owner died */
178pub const ENOTRECOVERABLE: c_int = 131; /* State not recoverable */
179
180/// String representations for the respective `errno` values.
181pub(crate) const STR_ERROR: [&str; 132] = [
182    "Success",
183    "Operation not permitted",
184    "No such file or directory",
185    "No such process",
186    "Interrupted system call",
187    "I/O error",
188    "No such device or address",
189    "Argument list too long",
190    "Exec format error",
191    "Bad file number",
192    "No child processes",
193    "Try again",
194    "Out of memory",
195    "Permission denied",
196    "Bad address",
197    "Block device required",
198    "Device or resource busy",
199    "File exists",
200    "Cross-device link",
201    "No such device",
202    "Not a directory",
203    "Is a directory",
204    "Invalid argument",
205    "File table overflow",
206    "Too many open files",
207    "Not a typewriter",
208    "Text file busy",
209    "File too large",
210    "No space left on device",
211    "Illegal seek",
212    "Read-only file system",
213    "Too many links",
214    "Broken pipe",
215    "Math argument out of domain of func",
216    "Math result not representable",
217    "Resource deadlock would occur",
218    "File name too long",
219    "No record locks available",
220    "Function not implemented",
221    "Directory not empty",
222    "Too many symbolic links encountered",
223    "Operation would block",
224    "No message of desired type",
225    "Identifier removed",
226    "Channel number out of range",
227    "Level 2 not synchronized",
228    "Level 3 halted",
229    "Level 3 reset",
230    "Link number out of range",
231    "Protocol driver not attached",
232    "No CSI structure available",
233    "Level 2 halted",
234    "Invalid exchange",
235    "Invalid request descriptor",
236    "Exchange full",
237    "No anode",
238    "Invalid request code",
239    "Invalid slot",
240    "Resource deadlock would occur",
241    "Bad font file format",
242    "Device not a stream",
243    "No data available",
244    "Timer expired",
245    "Out of streams resources",
246    "Machine is not on the network",
247    "Package not installed",
248    "Object is remote",
249    "Link has been severed",
250    "Advertise error",
251    "Srmount error",
252    "Communication error on send",
253    "Protocol error",
254    "Multihop attempted",
255    "RFS specific error",
256    "Not a data message",
257    "Value too large for defined data type",
258    "Name not unique on network",
259    "File descriptor in bad state",
260    "Remote address changed",
261    "Can not access a needed shared library",
262    "Accessing a corrupted shared library",
263    ".lib section in a.out corrupted",
264    "Attempting to link in too many shared libraries",
265    "Cannot exec a shared library directly",
266    "Illegal byte sequence",
267    "Interrupted system call should be restarted",
268    "Streams pipe error",
269    "Too many users",
270    "Socket operation on non-socket",
271    "Destination address required",
272    "Message too long",
273    "Protocol wrong type for socket",
274    "Protocol not available",
275    "Protocol not supported",
276    "Socket type not supported",
277    "Operation not supported on transport endpoint",
278    "Protocol family not supported",
279    "Address family not supported by protocol",
280    "Address already in use",
281    "Cannot assign requested address",
282    "Network is down",
283    "Network is unreachable",
284    "Network dropped connection because of reset",
285    "Software caused connection abort",
286    "Connection reset by peer",
287    "No buffer space available",
288    "Transport endpoint is already connected",
289    "Transport endpoint is not connected",
290    "Cannot send after transport endpoint shutdown",
291    "Too many references: cannot splice",
292    "Connection timed out",
293    "Connection refused",
294    "Host is down",
295    "No route to host",
296    "Operation already in progress",
297    "Operation now in progress",
298    "Stale NFS file handle",
299    "Structure needs cleaning",
300    "Not a XENIX named type file",
301    "No XENIX semaphores available",
302    "Is a named type file",
303    "Remote I/O error",
304    "Quota exceeded",
305    "No medium found",
306    "Wrong medium type",
307    "Operation Canceled",
308    "Required key not available",
309    "Key has expired",
310    "Key has been revoked",
311    "Key was rejected by service",
312    "Owner died",
313    "State not recoverable",
314];
315
316/// Longest error message length
317pub(crate) const STRERROR_MAX: usize = {
318    // Number of digits of the max value of this platform's c_int
319    let digits = {
320        let mut len = 0;
321        let mut i = c_int::MAX;
322
323        while i > 0 {
324            i /= 10;
325            len += 1;
326        }
327
328        len
329    };
330
331    let mut longest: usize = "Unknown error: ".len() + digits + 1;
332
333    let mut i = 0;
334    while i < STR_ERROR.len() {
335        let len = STR_ERROR[i].len() + 1;
336        if len > longest {
337            longest = len;
338        }
339
340        i += 1;
341    }
342
343    longest
344};