Skip to content

Instantly share code, notes, and snippets.

View philippkeller's full-sized avatar

Philipp Keller philippkeller

View GitHub Profile
#![allow(dead_code,
non_camel_case_types,
non_upper_case_globals,
non_snake_case)]
extern crate libc;
extern crate apue;
use apue::ToPtr;
#![allow(non_camel_case_types)]
extern crate libc;
use std::ffi::CString;
extern {
pub fn getchar() -> libc::c_int;
}
// bindgen generaged code starts...
diff --git a/src/unistd.rs b/src/unistd.rs
index 74d74c9..13defb8 100644
--- a/src/unistd.rs
+++ b/src/unistd.rs
@@ -5,8 +5,8 @@ use fcntl::{fcntl, OFlag, O_NONBLOCK, O_CLOEXEC, FD_CLOEXEC};
use fcntl::FcntlArg::{F_SETFD, F_SETFL};
use libc::{self, c_char, c_void, c_int, c_uint, size_t, pid_t, off_t, uid_t, gid_t, mode_t};
use std::mem;
-use std::ffi::{CString, CStr, OsString};
-use std::os::unix::ffi::OsStringExt;
@philippkeller
philippkeller / array-join.rs
Created September 13, 2016 20:45 — forked from anonymous/playground.rs
Rust code shared from the playground
use std::fmt;
pub trait Join {
fn join(&self) -> String;
}
impl<T: fmt::Display> Join for [T] {
fn join(&self) -> String {
self.iter()
.map(|a| format!("{}", a))
@philippkeller
philippkeller / merge-slice-itertools.rs
Last active September 14, 2016 13:36 — forked from anonymous/playground.rs
Rust code shared from the playground
/// merges an array with ordinal numbers into a string
extern crate itertools;
use itertools::Itertools;
fn join_int_slice(slice:&[u8]) -> String {
slice.iter().map(|&a| a as char).join("")
}
extern crate libc;
use libc::{passwd, getpwent, setpwent};
use std::ffi::CString;
unsafe fn getpwnam(name: &str) -> Option<passwd> {
setpwent();
while let Some(pw) = getpwent().as_ref() {
let pw_name = CString::from_raw(pw.pw_name).into_string().expect("found null");
if pw_name == name {
fn array_to_string(slice:&[i8]) -> String {
slice.iter().take_while(|&x| *x != 0).map(|&a| a as u8 as char).join("")
}
extern crate libc;
extern crate itertools;
use libc::{utsname, uname};
use std::ffi::CStr;
use itertools::Itertools;
fn array_to_string(slice:&[i8]) -> String {
slice.iter().take_while(|&x| *x != 0).map(|&a| a as u8 as char).join("")
}
@philippkeller
philippkeller / build.sh
Created October 1, 2016 20:15
build script to generate separate Cargo.toml files for linux and osx
#!/bin/bash
# builds Cargo.toml for linux and macos
# and only includes files in src/bin/*/*.rs which
# are not excluded by a #!cfg(target_os="...") attribute
cd "$(dirname "$0")"
for os in linux macos; do
echo "creating Cargo.toml for $os"
pub trait LibcResult<T> {
/// returns None if the result is empty (-1 if an integer, Null if a pointer)
/// and Some otherwise
///
/// # Example
/// if let Some(fd) = libc::creat(fd1, FILE_MODE).to_option() {
/// fd
/// } else {
/// panic!("{}", io::Error::last_os_error());
/// }