Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ioctl call with nix 0.4.* #201

Closed
gwenn opened this issue Nov 8, 2015 · 4 comments
Closed

ioctl call with nix 0.4.* #201

gwenn opened this issue Nov 8, 2015 · 4 comments

Comments

@gwenn
Copy link

gwenn commented Nov 8, 2015

Hello,

What it is the equivalent in version 0.4.* to the following call in version 0.3.*:

ioctl::read_into(libc::STDOUT_FILENO, TIOCGWINSZ, &mut size)

(https://github.com/gwenn/rustyline/blob/master/src/lib.rs#L185)

Thanks

@gwenn
Copy link
Author

gwenn commented Nov 8, 2015

Maybe I should use libc directly ?

libc::ioctl(libc::STDOUT_FILENO, TIOCGWINSZ, &mut size)

http://doc.rust-lang.org/libc/x86_64-apple-darwin/libc/fn.ioctl.html

@posborne
Copy link
Member

posborne commented Nov 9, 2015

This works:

#![feature(libc)]

#[macro_use]
extern crate nix;
extern crate libc;

use std::mem;
use libc::c_ushort;


#[derive(Debug)]
#[repr(C)]
struct winsize {
    ws_row: c_ushort,
    ws_col: c_ushort,
    ws_xpixel: c_ushort,
    ws_ypixel: c_ushort
}

mod ioctl {
    use libc;

    #[cfg(any(target_os = "macos", target_os = "freebsd"))]
    const TIOCGWINSZ: libc::c_ulong = 0x40087468;
    #[cfg(any(target_os = "linux", target_os = "android"))]
    const TIOCGWINSZ: libc::c_ulong = 0x5413;
    ioctl!(bad read_winsize with TIOCGWINSZ);
}

fn main() {
    unsafe {
        let mut size: winsize = std::mem::zeroed();
        ioctl::read_winsize(0, mem::transmute(&mut size as *mut winsize)).unwrap();
        println!("Size: {:?}", size);
    }
}

@gwenn
Copy link
Author

gwenn commented Nov 9, 2015

Thanks.

@gwenn gwenn closed this as completed Nov 9, 2015
@posborne
Copy link
Member

posborne commented Nov 9, 2015

I think the original implementation did probably find the wrong level of abstraction. Here, it would be nice to use the semantics you get with read but that is hard coded to do the equivalent of an _IOR. As discussed in #190, another option might have been to support both the macro and function interface approach.

For now, I certainly don't want to introduce another set of breaking changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants