uucore/mods/posix.rs
1// This file is part of the uutils coreutils package.
2//
3// For the full copyright and license information, please view the LICENSE
4// file that was distributed with this source code.
5// spell-checker:ignore (vars)
6//! Iterate over lines, including the line ending character(s).
7//!
8//! This module provides the [`posix_version`] function, that returns
9//! Some(usize) if the `_POSIX2_VERSION` environment variable is defined
10//! and has value that can be parsed.
11//! Otherwise returns None, so the calling utility would assume default behavior.
12//!
13//! NOTE: GNU (as of v9.4) recognizes three distinct values for POSIX version
14//!
15//! Utilities that rely on this module:
16//! `sort` (TBD)
17//! `tail` (TBD)
18//! `touch` (TBD)
19//! `uniq`
20//!
21use std::env;
22
23/// '199209' for POSIX 1003.2-1992, which would define Obsolete mode
24pub const OBSOLETE: usize = 199_209;
25
26/// '200112' for POSIX 1003.1-2001, which is the minimum version for Traditional mode
27pub const TRADITIONAL: usize = 200_112;
28
29/// '200809' for POSIX 1003.1-2008, which is the minimum version for Modern mode
30pub const MODERN: usize = 200_809;
31
32/// Returns the value of the `_POSIX2_VERSION` environment variable if it is defined
33pub fn posix_version() -> Option<usize> {
34 env::var("_POSIX2_VERSION")
35 .ok()
36 .and_then(|v| v.parse::<usize>().ok())
37}
38
39#[cfg(test)]
40mod tests {
41 use crate::posix::*;
42
43 #[test]
44 fn test_posix_version() {
45 // default
46 assert_eq!(posix_version(), None);
47 // set specific version
48 unsafe { env::set_var("_POSIX2_VERSION", OBSOLETE.to_string()) };
49 assert_eq!(posix_version(), Some(OBSOLETE));
50 unsafe { env::set_var("_POSIX2_VERSION", TRADITIONAL.to_string()) };
51 assert_eq!(posix_version(), Some(TRADITIONAL));
52 unsafe { env::set_var("_POSIX2_VERSION", MODERN.to_string()) };
53 assert_eq!(posix_version(), Some(MODERN));
54 }
55}