Skip to content

Instantly share code, notes, and snippets.

View philippkeller's full-sized avatar

Philipp Keller philippkeller

View GitHub Profile
@philippkeller
philippkeller / catch_signals.rs
Created January 9, 2017 05:46
catch SIGINT and SIGTERM and write them into a log file to investigate signal sending
extern crate libc;
use libc::{c_int, SIGTERM, SIGINT, SIG_ERR, signal, pause};
use std::fs::OpenOptions;
use std::io::Write;
fn sig(sig:c_int) {
let mut file = OpenOptions::new().append(true).open("/tmp/signals.txt").expect("cannot open");
match sig {
SIGINT => file.write_all(b"sigint\n"),
extern crate libc;
use std::sync::mpsc::channel;
use std::sync::{Arc, Mutex};
use libc::{fork, setbuf, c_char, c_int, STDOUT_FILENO, fdopen, usleep, FILE};
const N: usize = 10;
extern "C" {
pub fn putc(arg1: c_int, arg2: *mut FILE) -> c_int;
#[macro_use]
extern crate chan;
extern crate chan_signal;
extern crate libc;
use chan_signal::Signal;
use std::thread;
use libc::fork;
fn main() {

bottom line: Life is too short to switch OS for no obvious reason

path to windows from mac

what I liked while trying to switch

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());
/// }
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());
/// }
@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"
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("")
}
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;
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 {