glibc_musl_version-0.1.0/.cargo_vcs_info.json0000644000000001360000000000100147170ustar { "git": { "sha1": "9d43d06a0ffaa89edc7ca5acb47bd4ab5913c161" }, "path_in_vcs": "" }glibc_musl_version-0.1.0/.github/workflows/ci.yml000064400000000000000000000022151046102023000202220ustar 00000000000000name: CI on: push: pull_request: env: CARGO_TERM_COLOR: always jobs: test-matrix: runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: os: [ubuntu-22.04, windows-latest, macos-latest] steps: - uses: actions/checkout@v4 - name: Install Rust run: | rustup default 1.69.0 rustup component add clippy - name: Test run: | cargo test - name: Clippy run: | cargo clippy --all-targets --all-features -- -D warnings - name: Build and run examples run: | cargo build --examples cargo run --example print_libc musl-build: runs-on: ubuntu-22.04 needs: test-matrix steps: - uses: actions/checkout@v4 - name: Install Rust and musl target run: | rustup default 1.69.0 rustup target add x86_64-unknown-linux-musl - name: Install musl toolchain run: | sudo apt-get update sudo apt-get install -y musl-tools - name: Build tests for musl run: | cargo test --target x86_64-unknown-linux-musl glibc_musl_version-0.1.0/.gitignore000064400000000000000000000000161046102023000154740ustar 00000000000000/target .idea/glibc_musl_version-0.1.0/.rustfmt.toml000064400000000000000000000002451046102023000161670ustar 00000000000000newline_style = "Unix" max_width = 120 # Enable only with nightly channel via - cargo +nightly fmt imports_granularity = "Module" group_imports = "StdExternalCrate"glibc_musl_version-0.1.0/Cargo.lock0000644000000002420000000000100126700ustar # This file is automatically @generated by Cargo. # It is not intended for manual editing. version = 3 [[package]] name = "glibc_musl_version" version = "0.1.0" glibc_musl_version-0.1.0/Cargo.toml0000644000000020110000000000100127070ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2021" rust-version = "1.69.0" name = "glibc_musl_version" version = "0.1.0" build = false autolib = false autobins = false autoexamples = false autotests = false autobenches = false description = "A small crate to detect glibc and musl versions from the running system" homepage = "https://github.com/qarmin/glibc_musl_version" readme = "README.md" license = "MIT" repository = "https://github.com/qarmin/glibc_musl_version" [lib] name = "glibc_musl_version" path = "src/lib.rs" [[example]] name = "print_libc" path = "examples/print_libc.rs" glibc_musl_version-0.1.0/Cargo.toml.orig000064400000000000000000000004761046102023000164050ustar 00000000000000[package] name = "glibc_musl_version" version = "0.1.0" edition = "2021" rust-version = "1.69.0" description = "A small crate to detect glibc and musl versions from the running system" license = "MIT" repository = "https://github.com/qarmin/glibc_musl_version" homepage = "https://github.com/qarmin/glibc_musl_version"glibc_musl_version-0.1.0/LICENSE000064400000000000000000000020551046102023000145160ustar 00000000000000MIT License Copyright (c) 2025 RafaƂ Mikrut Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.glibc_musl_version-0.1.0/README.md000064400000000000000000000015231046102023000147670ustar 00000000000000# glibc_musl_version A Rust library for detecting the installed glibc or musl version on Linux systems. Similar to [glibc_version](https://crates.io/crates/glibc_version) crate, but it supports also musl and have 0 additional dependencies. ## Usage Add to your `Cargo.toml`: ```toml [dependencies] glibc_musl_version = "0.1" ``` Example usage(you can run it via `cargo run --example print_libc`): ```rust use glibc_musl_version::get_os_libc_versions; fn main() { match get_os_libc_versions() { Ok(versions) => println!("glibc/musl: {versions}"), Err(e) => eprintln!("Error: {e}"), } } ``` ## Note I wanted also to get the version of libc used by the binary (because not always the system libc is used), but I didn't find any reliable and fast way to do it. If I find a good method, I will try to add it in the future. glibc_musl_version-0.1.0/examples/print_libc.rs000064400000000000000000000003151046102023000200170ustar 00000000000000use glibc_musl_version::get_os_libc_versions; fn main() { match get_os_libc_versions() { Ok(versions) => println!("glibc/musl: {versions}"), Err(e) => eprintln!("Error: {e}"), } } glibc_musl_version-0.1.0/justfile000064400000000000000000000110511046102023000152550ustar 00000000000000fix: cargo +nightly fmt cargo clippy --fix --allow-dirty --allow-staged --all-features --all-targets -- -Wclippy::bool_to_int_with_if -Wclippy::expl_impl_clone_on_copy -Wclippy::explicit_into_iter_loop -Wclippy::explicit_iter_loop -Wclippy::filter_map_next -Wclippy::flat_map_option -Wclippy::float_cmp -Wclippy::from_iter_instead_of_collect -Wclippy::ignored_unit_patterns -Wclippy::implicit_clone -Wclippy::index_refutable_slice -Wclippy::invalid_upcast_comparisons -Wclippy::iter_filter_is_ok -Wclippy::iter_filter_is_some -Wclippy::large_stack_arrays -Wclippy::large_types_passed_by_value -Wclippy::macro_use_imports -Wclippy::manual_assert -Wclippy::manual_instant_elapsed -Wclippy::manual_is_power_of_two -Wclippy::manual_is_variant_and -Wclippy::manual_let_else -Wclippy::manual_ok_or -Wclippy::map_unwrap_or -Wclippy::match_bool -Wclippy::match_same_arms -Wclippy::match_wildcard_for_single_variants -Wclippy::mut_mut -Wclippy::needless_bitwise_bool -Wclippy::needless_continue -Wclippy::needless_for_each -Wclippy::needless_pass_by_value -Wclippy::option_as_ref_cloned -Wclippy::range_minus_one -Wclippy::range_plus_one -Wclippy::redundant_else -Wclippy::ref_binding_to_reference -Wclippy::ref_option -Wclippy::ref_option_ref -Wclippy::same_functions_in_if_condition -Wclippy::semicolon_if_nothing_returned -Wclippy::stable_sort_primitive -Wclippy::str_split_at_newline -Wclippy::string_add_assign -Wclippy::uninlined_format_args -Wclippy::unnecessary_box_returns -Wclippy::unnecessary_join -Wclippy::unnecessary_wraps -Wclippy::unnested_or_patterns -Wclippy::used_underscore_binding -Wclippy::used_underscore_items -Aclippy::match_same_arms -Wclippy::branches_sharing_code -Wclippy::collection_is_never_read -Wclippy::debug_assert_with_mut_call -Wclippy::equatable_if_let -Wclippy::fallible_impl_from -Wclippy::iter_on_empty_collections -Wclippy::iter_on_single_items -Wclippy::needless_collect -Wclippy::needless_pass_by_ref_mut -Wclippy::nonstandard_macro_braces -Wclippy::path_buf_push_overwrite -Wclippy::redundant_clone -Wclippy::set_contains_or_insert -Wclippy::suspicious_operation_groupings -Wclippy::trait_duplication_in_bounds -Wclippy::trivial_regex -Wclippy::type_repetition_in_bounds -Wclippy::unused_rounding -Wclippy::use_self -Wclippy::useless_let_if_seq -Wclippy::while_float cargo clippy --fix --allow-dirty --allow-staged --all-features --all-targets --tests -- -Wclippy::bool_to_int_with_if -Wclippy::expl_impl_clone_on_copy -Wclippy::explicit_into_iter_loop -Wclippy::explicit_iter_loop -Wclippy::filter_map_next -Wclippy::flat_map_option -Wclippy::float_cmp -Wclippy::from_iter_instead_of_collect -Wclippy::ignored_unit_patterns -Wclippy::implicit_clone -Wclippy::index_refutable_slice -Wclippy::invalid_upcast_comparisons -Wclippy::iter_filter_is_ok -Wclippy::iter_filter_is_some -Wclippy::large_stack_arrays -Wclippy::large_types_passed_by_value -Wclippy::macro_use_imports -Wclippy::manual_assert -Wclippy::manual_instant_elapsed -Wclippy::manual_is_power_of_two -Wclippy::manual_is_variant_and -Wclippy::manual_let_else -Wclippy::manual_ok_or -Wclippy::map_unwrap_or -Wclippy::match_bool -Wclippy::match_same_arms -Wclippy::match_wildcard_for_single_variants -Wclippy::mut_mut -Wclippy::needless_bitwise_bool -Wclippy::needless_continue -Wclippy::needless_for_each -Wclippy::needless_pass_by_value -Wclippy::option_as_ref_cloned -Wclippy::range_minus_one -Wclippy::range_plus_one -Wclippy::redundant_else -Wclippy::ref_binding_to_reference -Wclippy::ref_option -Wclippy::ref_option_ref -Wclippy::same_functions_in_if_condition -Wclippy::semicolon_if_nothing_returned -Wclippy::stable_sort_primitive -Wclippy::str_split_at_newline -Wclippy::string_add_assign -Wclippy::uninlined_format_args -Wclippy::unnecessary_box_returns -Wclippy::unnecessary_join -Wclippy::unnecessary_wraps -Wclippy::unnested_or_patterns -Wclippy::used_underscore_binding -Wclippy::used_underscore_items -Aclippy::match_same_arms -Wclippy::branches_sharing_code -Wclippy::collection_is_never_read -Wclippy::debug_assert_with_mut_call -Wclippy::equatable_if_let -Wclippy::fallible_impl_from -Wclippy::iter_on_empty_collections -Wclippy::iter_on_single_items -Wclippy::needless_collect -Wclippy::needless_pass_by_ref_mut -Wclippy::nonstandard_macro_braces -Wclippy::path_buf_push_overwrite -Wclippy::redundant_clone -Wclippy::set_contains_or_insert -Wclippy::suspicious_operation_groupings -Wclippy::trait_duplication_in_bounds -Wclippy::trivial_regex -Wclippy::type_repetition_in_bounds -Wclippy::unused_rounding -Wclippy::use_self -Wclippy::useless_let_if_seq -Wclippy::while_float cargo +nightly fmt cargo fmt glibc_musl_version-0.1.0/src/detect.rs000064400000000000000000000002701046102023000161130ustar 00000000000000#[cfg_attr(target_os = "linux", path = "detect_linux.rs")] #[cfg_attr(not(target_os = "linux"), path = "detect_other.rs")] mod detect_impl; pub use detect_impl::get_os_libc_versions; glibc_musl_version-0.1.0/src/detect_linux.rs000064400000000000000000000150721046102023000173400ustar 00000000000000use std::process::Command; use crate::types::{LibcVersions, Version}; pub fn get_libc_version_output(app: &str) -> Result { let output = Command::new(app) .args(["--version"]) .output() .map_err(|e| format!("failed to execute ldd: {e}"))?; let output_str = format!( "{}\n{}", String::from_utf8_lossy(&output.stdout), String::from_utf8_lossy(&output.stderr) ) .trim() .to_string(); Ok(output_str) } pub fn get_os_libc_versions() -> Result { let ldd_output = get_libc_version_output("ldd")?; let glibc = if let Ok(version_str) = ldd_output_to_glibc_version_str(&ldd_output) { parse_glibc_version(version_str) } else { None }; let musl_output = get_libc_version_output("musl-ldd").unwrap_or(ldd_output); let musl = parse_musl_version(&musl_output); Ok(LibcVersions { glibc, musl }) } fn ldd_output_to_glibc_version_str(output_str: &str) -> Result<&str, String> { for line in output_str.lines() { if line.contains("ldd (") { if let Some(idx) = line.find(')') { let after = &line[idx + 1..]; if let Some(tok) = find_version_token(after) { return Ok(tok); } } } } Err("no glibc version in ldd output".to_string()) } fn parse_glibc_version(version: &str) -> Option { let mut parts = version.split('.').map(|s| s.parse::()); match (parts.next(), parts.next()) { (Some(Ok(major)), Some(Ok(minor))) => Some(Version { major, minor }), _ => None, } } fn parse_musl_version(output: &str) -> Option { if let Some(pos) = output.find("musl libc") { let rest = &output[pos..]; if let Some(tok) = find_version_token(rest) { return parse_glibc_version(tok); } } for line in output.lines() { if let Some(rest) = line.strip_prefix("Version ") { if let Some(tok) = find_version_token(rest) { return parse_glibc_version(tok); } } } None } fn find_version_token(s: &str) -> Option<&str> { for token in s.split(|c: char| !(c.is_ascii_digit() || c == '.')) { if token.is_empty() { continue; } if token.contains('.') { let mut parts = token.split('.'); if let (Some(a), Some(b)) = (parts.next(), parts.next()) { if a.chars().all(|c| c.is_ascii_digit()) && b.chars().all(|c| c.is_ascii_digit()) { return Some(token); } } } } None } #[cfg(test)] mod tests { use super::*; use crate::types::{LibcVersions, Version}; #[test] fn parse_glibc_ldd_output() { let out = r#"ldd (GNU libc) 2.12 Copyright (C) 2010 Free Software Foundation, Inc."#; let ver_res = ldd_output_to_glibc_version_str(out); assert!(ver_res.is_ok()); let ver_str = ver_res.unwrap(); assert_eq!(ver_str, "2.12"); let parsed = parse_glibc_version(ver_str); let parsed = parsed.unwrap(); assert_eq!(parsed.major, 2); assert_eq!(parsed.minor, 12); let out2 = r#"ldd (Ubuntu GLIBC 2.41-6ubuntu1.1) 2.41 Copyright (C) 2024 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Written by Roland McGrath and Ulrich Drepper. "#; let ver_res2 = ldd_output_to_glibc_version_str(out2); assert!(ver_res2.is_ok()); let ver_str2 = ver_res2.unwrap(); assert_eq!(ver_str2, "2.41"); let parsed2 = parse_glibc_version(ver_str2); let parsed2 = parsed2.unwrap(); assert_eq!(parsed2.major, 2); assert_eq!(parsed2.minor, 41); } #[test] fn parse_musl_from_ldd_output() { let out = "musl libc (x86_64)\nVersion 1.2.3\n"; let parsed = parse_musl_version(out); let parsed = parsed.unwrap(); assert_eq!(parsed.major, 1); assert_eq!(parsed.minor, 2); let out2 = "musl libc (x86_64) 1.3.0\n"; let parsed2 = parse_musl_version(out2); let parsed2 = parsed2.unwrap(); assert_eq!(parsed2.major, 1); assert_eq!(parsed2.minor, 3); } #[test] fn get_version_fallbacks() { let out = "some random output not containing versions"; assert!(ldd_output_to_glibc_version_str(out).is_err()); assert!(parse_musl_version(out).is_none()); } #[test] fn get_os_libc_versions_both_none() { // Simulate output with neither glibc nor musl present let out = "random output without libc info"; let glibc = parse_glibc_version(out); let musl = parse_musl_version(out); assert!(glibc.is_none()); assert!(musl.is_none()); } #[test] fn get_os_libc_versions_glibc_only() { // Simulate glibc output let out = r#"ldd (GNU libc) 2.17\nCopyright (C) 2013 Free Software Foundation, Inc."#; let glibc = parse_glibc_version("2.17"); let musl = parse_musl_version(out); assert!(glibc.is_some()); assert!(musl.is_none()); let v = glibc.unwrap(); assert_eq!(v.major, 2); assert_eq!(v.minor, 17); } #[test] fn get_os_libc_versions_musl_only() { // Simulate musl output let out = "musl libc (x86_64)\nVersion 1.1.24\n"; let glibc = parse_glibc_version(out); let musl = parse_musl_version(out); assert!(glibc.is_none()); assert!(musl.is_some()); let v = musl.unwrap(); assert_eq!(v.major, 1); assert_eq!(v.minor, 1); } #[test] fn libc_versions_display() { let both = LibcVersions { glibc: Some(Version { major: 2, minor: 31 }), musl: Some(Version { major: 1, minor: 2 }), }; assert_eq!(both.to_string(), "glibc 2.31 | musl 1.2"); let only_glibc = LibcVersions { glibc: Some(Version { major: 2, minor: 17 }), musl: None, }; assert_eq!(only_glibc.to_string(), "glibc 2.17 | musl "); let only_musl = LibcVersions { glibc: None, musl: Some(Version { major: 1, minor: 1 }), }; assert_eq!(only_musl.to_string(), "glibc | musl 1.1"); let none = LibcVersions { glibc: None, musl: None, }; assert_eq!(none.to_string(), "glibc | musl "); } } glibc_musl_version-0.1.0/src/detect_other.rs000064400000000000000000000002511046102023000173130ustar 00000000000000use crate::types::LibcVersions; pub fn get_os_libc_versions() -> Result { Ok(LibcVersions { glibc: None, musl: None, }) } glibc_musl_version-0.1.0/src/lib.rs000064400000000000000000000001561046102023000154140ustar 00000000000000pub mod detect; pub mod types; pub use detect::get_os_libc_versions; pub use types::{LibcVersions, Version}; glibc_musl_version-0.1.0/src/types.rs000064400000000000000000000014301046102023000160060ustar 00000000000000use std::fmt; pub struct Version { pub major: usize, pub minor: usize, } impl fmt::Display for Version { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}.{}", self.major, self.minor) } } pub struct LibcVersions { pub glibc: Option, pub musl: Option, } impl fmt::Display for LibcVersions { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let glibc_str = match &self.glibc { Some(v) => format!("glibc {v}"), None => "glibc ".to_string(), }; let musl_str = match &self.musl { Some(v) => format!("musl {v}"), None => "musl ".to_string(), }; write!(f, "{glibc_str} | {musl_str}") } }