Mercurial > crates > nonstick
comparison src/memory.rs @ 64:bbe84835d6db v0.0.5
More organization; add lots of docs.
- moves `PamHandle` to its own module, since it will be used
by both modules and clients.
- adds a ton of documentation to the `PamModule` trait
and reorders methods to most-interesting-first.
- adds more flag values from pam_modules.h.
- other misc cleanup.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Thu, 22 May 2025 01:52:32 -0400 |
parents | 05cc2c27334f |
children |
comparison
equal
deleted
inserted
replaced
63:a7aa5ca0d00d | 64:bbe84835d6db |
---|---|
1 //! Utility functions for dealing with memory copying and stuff. | 1 //! Utility functions for dealing with memory copying and stuff. |
2 | 2 |
3 use crate::ErrorCode; | 3 use crate::constants::{ErrorCode, Result}; |
4 use libc::c_char; | 4 use libc::c_char; |
5 use std::ffi::{CStr, CString}; | 5 use std::ffi::{CStr, CString}; |
6 | 6 |
7 /// Safely converts a `&str` option to a `CString` option. | 7 /// Safely converts a `&str` option to a `CString` option. |
8 pub fn option_cstr(prompt: Option<&str>) -> crate::Result<Option<CString>> { | 8 pub fn option_cstr(prompt: Option<&str>) -> Result<Option<CString>> { |
9 prompt | 9 prompt |
10 .map(CString::new) | 10 .map(CString::new) |
11 .transpose() | 11 .transpose() |
12 .map_err(|_| ErrorCode::ConversationError) | 12 .map_err(|_| ErrorCode::ConversationError) |
13 } | 13 } |
20 } | 20 } |
21 } | 21 } |
22 | 22 |
23 /// Creates an owned copy of a string that is returned from a | 23 /// Creates an owned copy of a string that is returned from a |
24 /// <code>pam_get_<var>whatever</var></code> function. | 24 /// <code>pam_get_<var>whatever</var></code> function. |
25 pub fn copy_pam_string(result_ptr: *const c_char) -> crate::Result<String> { | 25 pub fn copy_pam_string(result_ptr: *const c_char) -> Result<String> { |
26 // We really shouldn't get a null pointer back here, but if we do, return nothing. | 26 // We really shouldn't get a null pointer back here, but if we do, return nothing. |
27 if result_ptr.is_null() { | 27 if result_ptr.is_null() { |
28 return Ok(String::new()); | 28 return Ok(String::new()); |
29 } | 29 } |
30 let bytes = unsafe { CStr::from_ptr(result_ptr) }; | 30 let bytes = unsafe { CStr::from_ptr(result_ptr) }; |