about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlyssa Ross <hi@alyssa.is>2020-04-11 11:37:03 +0000
committerAlyssa Ross <hi@alyssa.is>2020-04-11 13:34:37 +0000
commit7f51a5ab1c6fbc316ee676c04e29abd1d9c8e615 (patch)
tree6c115539ce57a4b3164f6e5ef42494077f456ed6
parent85e5ecabb1d6f1776c1747884dd892ec43f8108a (diff)
downloadpushmail-7f51a5ab1c6fbc316ee676c04e29abd1d9c8e615.tar
pushmail-7f51a5ab1c6fbc316ee676c04e29abd1d9c8e615.tar.gz
pushmail-7f51a5ab1c6fbc316ee676c04e29abd1d9c8e615.tar.bz2
pushmail-7f51a5ab1c6fbc316ee676c04e29abd1d9c8e615.tar.lz
pushmail-7f51a5ab1c6fbc316ee676c04e29abd1d9c8e615.tar.xz
pushmail-7f51a5ab1c6fbc316ee676c04e29abd1d9c8e615.tar.zst
pushmail-7f51a5ab1c6fbc316ee676c04e29abd1d9c8e615.zip
Extract util module
-rw-r--r--src/main.rs42
-rw-r--r--src/util.rs36
2 files changed, 43 insertions, 35 deletions
diff --git a/src/main.rs b/src/main.rs
index e45a4cb..09c97e2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,57 +1,29 @@
+mod util;
+
 use clap::clap_app;
 use failure::{err_msg, format_err, Error};
 use isahc::prelude::*;
 use serde::Deserialize;
 use std::ffi::CString;
 use std::ffi::{OsStr, OsString};
-use std::io::{self, stderr, stdin, BufRead, BufReader, ErrorKind, Read, Write};
+use std::io::{stderr, stdin, BufRead, BufReader, Read, Write};
 use std::mem::MaybeUninit;
 use std::os::unix::prelude::*;
 use std::path::Path;
 use std::process::{exit, Command, ExitStatus, Stdio};
-
 use libc::{
-    gethostname, posix_spawn_file_actions_adddup2, posix_spawn_file_actions_init, posix_spawnp,
-    waitpid, _SC_HOST_NAME_MAX,
+    posix_spawn_file_actions_adddup2, posix_spawn_file_actions_init, posix_spawnp,
+    waitpid
 };
 
+use self::util::*;
+
 extern "C" {
     static mut environ: *mut *mut libc::c_char;
 }
 
 type Result<T> = std::result::Result<T, Error>;
 
-fn copy_stream(src: &mut dyn Read, dst: &mut dyn Write) -> io::Result<()> {
-    let mut buf = vec![0; 65536];
-    loop {
-        match src.read(&mut buf) {
-            Ok(0) => return Ok(()),
-            Ok(len) => dst.write_all(&buf[..len])?,
-            Err(e) if e.kind() == ErrorKind::Interrupted => continue,
-            Err(e) => return Err(e),
-        }
-    }
-}
-
-fn hostname() -> OsString {
-    let mut bytes = vec![0; _SC_HOST_NAME_MAX as usize + 2];
-
-    if unsafe { gethostname(bytes.as_mut_ptr(), bytes.len()) } == -1 {
-        panic!("gethostname failed: {}", std::io::Error::last_os_error());
-    }
-
-    if bytes[_SC_HOST_NAME_MAX as usize + 1] != 0 {
-        panic!("hostname longer than HOST_NAME_MAX");
-    }
-
-    let bytes: Vec<_> = bytes
-        .into_iter()
-        .take_while(|b| *b != 0)
-        .map(|b| b as u8)
-        .collect();
-    OsString::from_vec(bytes)
-}
-
 #[derive(Clone, Debug)]
 struct Config<'a> {
     from: &'a OsStr,
diff --git a/src/util.rs b/src/util.rs
new file mode 100644
index 0000000..8302ed6
--- /dev/null
+++ b/src/util.rs
@@ -0,0 +1,36 @@
+use std::io::prelude::*;
+use std::io::{ErrorKind, self};
+use std::ffi::OsString;
+use std::os::unix::prelude::*;
+use libc::{gethostname, _SC_HOST_NAME_MAX};
+
+pub fn copy_stream(src: &mut dyn Read, dst: &mut dyn Write) -> io::Result<()> {
+    let mut buf = vec![0; 65536];
+    loop {
+        match src.read(&mut buf) {
+            Ok(0) => return Ok(()),
+            Ok(len) => dst.write_all(&buf[..len])?,
+            Err(e) if e.kind() == ErrorKind::Interrupted => continue,
+            Err(e) => return Err(e),
+        }
+    }
+}
+
+pub fn hostname() -> OsString {
+    let mut bytes = vec![0; _SC_HOST_NAME_MAX as usize + 2];
+
+    if unsafe { gethostname(bytes.as_mut_ptr(), bytes.len()) } == -1 {
+        panic!("gethostname failed: {}", std::io::Error::last_os_error());
+    }
+
+    if bytes[_SC_HOST_NAME_MAX as usize + 1] != 0 {
+        panic!("hostname longer than HOST_NAME_MAX");
+    }
+
+    let bytes: Vec<_> = bytes
+        .into_iter()
+        .take_while(|b| *b != 0)
+        .map(|b| b as u8)
+        .collect();
+    OsString::from_vec(bytes)
+}