Skip to content

Commit

Permalink
Housecleaning:
Browse files Browse the repository at this point in the history
- move all bindings into libc.rs
- fix failing doc error
- removed unneeded dependencies
- tried to get it to compile on linux, no-main still fails, commented out f01-main.rs and f04-echo.rs for now until rust-lang/rust#18807 is fixed
- silence rust compiler warnings
  • Loading branch information
philippkeller committed Dec 21, 2016
1 parent a6cf6e5 commit 2ce26c7
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 140 deletions.
14 changes: 6 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ libc = "*"
errno = "*"
clap = "2.1"
regex = "*"
chan = "0.1.18"
chan-signal = "0.1.7"

[[bin]]
name="f03-list-files"
Expand Down Expand Up @@ -208,17 +206,17 @@ path = "src/bin/06-system-data-files/f02-getpwnam.rs"
name="f11-strftime"
path = "src/bin/06-system-data-files/f11-strftime.rs"

[[bin]]
name="f01-main"
path = "src/bin/07-process-env/f01-main.rs"
# [[bin]]
# name="f01-main"
# path = "src/bin/07-process-env/f01-main.rs"

[[bin]]
name="f03-atexit"
path = "src/bin/07-process-env/f03-atexit.rs"

[[bin]]
name="f04-echo"
path = "src/bin/07-process-env/f04-echo.rs"
# [[bin]]
# name="f04-echo"
# path = "src/bin/07-process-env/f04-echo.rs"

[[bin]]
name="f16-rlimits"
Expand Down
7 changes: 2 additions & 5 deletions src/bin/01-overview/f05-copy-stdin-stdout2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
extern crate libc;
extern crate apue;

use libc::{c_int, c_char, FILE, STDIN_FILENO, STDOUT_FILENO, fdopen, ferror};
use libc::{c_char, STDIN_FILENO, STDOUT_FILENO, fdopen, ferror};
use apue::LibcResult;
use apue::my_libc::{putc, getc};

extern "C" {
pub fn putc(arg1: c_int, arg2: *mut FILE) -> c_int;
pub fn getc(arg1: *mut FILE) -> c_int;
}

fn main() {
unsafe {
Expand Down
7 changes: 2 additions & 5 deletions src/bin/01-overview/f07-read-execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ extern crate libc;
#[macro_use(cstr, as_char)]
extern crate apue;

use libc::{STDIN_FILENO, c_char, c_int, printf, strlen, fgets, fdopen, fork, waitpid};
use libc::{STDIN_FILENO, c_char, printf, strlen, fgets, fdopen, fork, waitpid};
use apue::{array_to_string, LibcResult};

extern "C" {
pub fn execlp(file: *const c_char, arg0: *const c_char, ...) -> c_int;
}
use apue::my_libc::execlp;

const MAXLINE: usize = 100;

Expand Down
5 changes: 1 addition & 4 deletions src/bin/01-overview/f10-read-execute2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ extern crate apue;
use libc::{STDIN_FILENO, SIGINT, SIG_ERR, c_char, c_int, printf, strlen, fgets, fdopen, fork,
waitpid, signal};
use apue::{array_to_string, LibcResult};

extern "C" {
pub fn execlp(file: *const c_char, arg0: *const c_char, ...) -> c_int;
}
use apue::my_libc::execlp;

extern "C" fn sig_int(_: c_int) {
unsafe {
Expand Down
22 changes: 9 additions & 13 deletions src/bin/05-stdio/f11-check-buffered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@ extern crate apue;
#[allow(non_camel_case_types)]
mod buffered {
extern crate libc;

// can be called from libc::getchar once https://github.com/rust-lang/libc/pull/372 is released
extern "C" {
pub fn getchar() -> libc::c_int;
}
use self::libc::{fputs, getchar, fgetc, fopen, FILE};

// bindgen generaged code starts...
extern "C" {
Expand Down Expand Up @@ -89,7 +85,7 @@ mod buffered {
}
// ... bindgen generated code ends

unsafe fn pr_stdio(name: &str, fp: *mut libc::FILE) {
unsafe fn pr_stdio(name: &str, fp: *mut FILE) {
let fp = &mut *(fp as *mut MY_FILE);
let buffer_type = if (fp._flags & libc::_IONBF as i16) != 0 {
"unbuffered"
Expand All @@ -108,14 +104,14 @@ mod buffered {

pub fn runit() {
unsafe {
let stdin = __stdinp as *mut libc::FILE;
let stdout = __stdoutp as *mut libc::FILE;
let stderr = __stderrp as *mut libc::FILE;
let passwd = libc::fopen(cstr!("/etc/passwd"), cstr!("r"));
libc::fputs(cstr!("enter any character\n"), stderr);
let stdin = __stdinp as *mut FILE;
let stdout = __stdoutp as *mut FILE;
let stderr = __stderrp as *mut FILE;
let passwd = fopen(cstr!("/etc/passwd"), cstr!("r"));
fputs(cstr!("enter any character\n"), stderr);
getchar();
libc::fputs(cstr!("one line to stderr\n"), stderr);
libc::fgetc(passwd);
fputs(cstr!("one line to stderr\n"), stderr);
fgetc(passwd);
pr_stdio("stdin", stdin);
pr_stdio("stdout", stdout);
pr_stdio("stderr", stderr);
Expand Down
18 changes: 8 additions & 10 deletions src/bin/05-stdio/f12-tmpnam-tmpfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
/// one line of output
extern crate libc;
#[macro_use(print_err)]
#[macro_use(print_err, cstr)]
extern crate apue;

use std::ffi::{CStr, CString};
use apue::my_libc::tmpnam;
use libc::{tmpfile, fgets, fputs, rewind, L_tmpnam};

const MAXLINE: usize = 4096;

extern "C" {
pub fn tmpnam(ptr: *mut libc::c_char) -> *mut libc::c_char;
}

fn main() {
// method 1: get pointer to new buffer
let tmp = unsafe {
Expand All @@ -29,22 +27,22 @@ fn main() {

// method 2: create buffer ourselves, make tmpnam fill this buffer
let tmp = unsafe {
let name = CString::from_vec_unchecked(Vec::with_capacity(libc::L_tmpnam as usize))
let name = CString::from_vec_unchecked(Vec::with_capacity(L_tmpnam as usize))
.into_raw();
tmpnam(name);
CStr::from_ptr(name).to_owned().into_string().unwrap()
};
print_err!("tmp file={}", tmp);

unsafe {
let fp = libc::tmpfile();
let fp = tmpfile();
if fp.is_null() {
panic!("tmpfile error");
}
libc::fputs(CString::new("one line of output").unwrap().as_ptr(), fp);
libc::rewind(fp);
fputs(cstr!("one line of output"), fp);
rewind(fp);
let line = CString::from_vec_unchecked(Vec::with_capacity(MAXLINE)).into_raw();
if libc::fgets(line, MAXLINE as i32, fp).is_null() {
if fgets(line, MAXLINE as i32, fp).is_null() {
panic!("fgets error");
}
println!("{}", CStr::from_ptr(line).to_owned().into_string().unwrap());
Expand Down
25 changes: 2 additions & 23 deletions src/bin/06-system-data-files/e01-shadow-pw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,13 @@
/// - user needs to be root, checking for root with getuid
/// - first tried with iterating via getspent, then saw getspnam which is a lot easier of course
extern crate libc;
extern crate apue;

#[cfg(target_os = "linux")]
mod shadow {

use std::ffi::{CStr, CString};
use libc::{c_char, c_long, c_ulong};

#[repr(C)]
#[derive(Copy, Clone)]
#[derive(Debug)]
pub struct spwd {
pub sp_namp: *mut c_char,
pub sp_pwdp: *mut c_char,
pub sp_lstchg: c_long,
pub sp_min: c_long,
pub sp_max: c_long,
pub sp_warn: c_long,
pub sp_inact: c_long,
pub sp_expire: c_long,
pub sp_flag: c_ulong,
}

extern "C" {
pub fn setspent();
pub fn endspent();
pub fn getspent() -> *mut spwd;
pub fn getspnam(__name: *const ::std::os::raw::c_char) -> *mut spwd;
}
use apue::my_libc::{setspent, getspnam, endspent, getspent};

#[derive(Debug)]
pub struct PasswdOwned {
Expand Down
4 changes: 4 additions & 0 deletions src/bin/07-process-env/f01-main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ pub extern "C" fn main(_argc: i32, _argv: *const *const u8) {
}
}

#[allow(dead_code)]
fn spare() { panic!("{}", 0); } //adding this (doesn't have to be named "spare") makes the compilation work.
// you don't even need to add this function signature where you're using these functions.

// This is needed for Linux but not for Mac
#[lang = "eh_unwind_resume"]
#[no_mangle]
Expand Down
4 changes: 4 additions & 0 deletions src/bin/07-process-env/f04-echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ extern crate libc;

use libc::printf;

#[allow(dead_code)]
fn spare() { panic!("{}", 0); } //adding this (doesn't have to be named "spare") makes the compilation work.
// you don't even need to add this function signature where you're using these functions.

#[no_mangle] // ensure that this symbol is called `main` in the output
pub extern "C" fn main(_argc: i32, _argv: *const *const u8) {
unsafe {
Expand Down
2 changes: 1 addition & 1 deletion src/bin/07-process-env/f16-rlimits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/// mac only:
/// $ f16-rlimits
/// RLIMIT_AS (infinite) (infinite)
/// RLIMIT_CORE (infinite) (infinite)
/// RLIMIT_CORE 0 (infinite)
/// RLIMIT_CPU (infinite) (infinite)
/// RLIMIT_DATA (infinite) (infinite)
/// RLIMIT_FSIZE (infinite) (infinite)
Expand Down
9 changes: 2 additions & 7 deletions src/bin/08-process-cntl/f03-vfork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,11 @@ extern crate apue;

use libc::{printf, _exit, getpid, c_int};
use apue::{LibcResult, err_sys};


// vfork is not implemented in libc, and that's probably good so
// as vfork is somehow deprecated
extern "C" {
pub fn vfork() -> libc::pid_t;
}
use apue::my_libc::vfork;

static mut GLOBVAR: i64 = 6;

#[allow(unused_assignments)]
fn main() {
unsafe {
let mut var: i8 = 88;
Expand Down
23 changes: 4 additions & 19 deletions src/bin/08-process-cntl/f06-exit-status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,11 @@
extern crate libc;
extern crate apue;

use libc::{c_int, WIFEXITED, WEXITSTATUS, WIFSIGNALED, WTERMSIG, WCOREDUMP, WIFSTOPPED, WSTOPSIG,
SIGFPE};
use libc::{c_int, SIGFPE};
use libc::{exit, wait, fork, abort, raise};
use apue::{LibcResult, err_sys};
use apue::{LibcResult, err_sys, pr_exit};
use std::panic;

unsafe fn pr_exit(status: c_int) {
if WIFEXITED(status) {
println!("normal termination, exit status = {}", WEXITSTATUS(status));
} else if WIFSIGNALED(status) {
println!("abnormal termination, signal number = {} {}",
WTERMSIG(status),
if WCOREDUMP(status) {
" (core file generated)"
} else {
""
});
} else if WIFSTOPPED(status) {
println!("child stopped, signal number = {}", WSTOPSIG(status));
}
}

fn handle_panic(e: &panic::PanicInfo) {
match e.payload().downcast_ref::<String>() {
Expand Down Expand Up @@ -76,7 +60,8 @@ fn main() {
pid = fork().to_option().expect("fork error");
if pid == 0 {
// child
status /= 0; // divide by 0 generates SIGFPE
let z = 0;
status /= z; // divide by 0 generates SIGFPE
}

if wait(&mut status) != pid {
Expand Down
6 changes: 2 additions & 4 deletions src/bin/08-process-cntl/f12-race-condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
extern crate libc;
extern crate apue;

use libc::{c_int, c_char, FILE, STDOUT_FILENO, fork, setbuf, fdopen, usleep};
use libc::{c_char, FILE, STDOUT_FILENO, fork, setbuf, fdopen, usleep};
use apue::LibcResult;
use apue::my_libc::putc;

extern "C" {
pub fn putc(arg1: c_int, arg2: *mut FILE) -> c_int;
}

unsafe fn charatatime(out: *mut FILE, s: &str) {
for c in s.chars() {
Expand Down
40 changes: 0 additions & 40 deletions src/bin/08-process-cntl/f13-avoid-race.rs

This file was deleted.

33 changes: 32 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,23 @@ pub fn pr_exit(status: c_int) {
}

pub mod my_libc {
use libc::{dirent, DIR, c_int, c_char};
use libc::{dirent, DIR, c_int, c_char, c_long, c_ulong, pid_t, FILE};

#[repr(C)]
#[derive(Copy, Clone)]
#[derive(Debug)]
pub struct spwd {
pub sp_namp: *mut c_char,
pub sp_pwdp: *mut c_char,
pub sp_lstchg: c_long,
pub sp_min: c_long,
pub sp_max: c_long,
pub sp_warn: c_long,
pub sp_inact: c_long,
pub sp_expire: c_long,
pub sp_flag: c_ulong,
}

extern "C" {
#[cfg(target_os = "macos")]
#[link_name = "readdir$INODE64"]
Expand All @@ -151,6 +167,21 @@ pub mod my_libc {
#[cfg(not(target_os = "macos"))]
pub fn readdir(arg1: *mut DIR) -> *mut dirent;

pub fn tmpnam(ptr: *mut c_char) -> *mut c_char;

pub fn getc(arg1: *mut FILE) -> c_int;
pub fn putc(arg1: c_int, arg2: *mut FILE) -> c_int;
pub fn getchar() -> c_int;

pub fn setspent();
pub fn endspent();
pub fn getspent() -> *mut spwd;
pub fn getspnam(__name: *const c_char) -> *mut spwd;

// vfork is not implemented in libc, and that's probably good so
// as vfork is somehow deprecated
pub fn vfork() -> pid_t;

pub fn execl(__path: *const c_char, __arg0: *const c_char, ...) -> c_int;
pub fn execle(__path: *const c_char, __arg0: *const c_char, ...) -> c_int;
pub fn execlp(__file: *const c_char, __arg0: *const c_char, ...) -> c_int;
Expand Down

0 comments on commit 2ce26c7

Please sign in to comment.