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

confused by libc::stat #403

Closed
portaloffreedom opened this issue Sep 20, 2016 · 3 comments
Closed

confused by libc::stat #403

portaloffreedom opened this issue Sep 20, 2016 · 3 comments

Comments

@portaloffreedom
Copy link

I'm trying to call the stat function for a personal project. As far as I understood I need to call the libc::stat function on the libc::stat struct. I can understand how it works, but since struct and function are in the same module, the rust compiler is quite confused.

I've searched online and found nothing that tells me how to use libc::<stat as struct> or <stat as fn>(path, &mut stat_struct) or similar combinations.

How am I supposed to fix this? Is this a bug in the library? Or is it me that is missing something about the language?

@ghost
Copy link

ghost commented Sep 20, 2016

Rust has separate namespaces for types (where stat struct would be) and values
(where stat function would be). Thus, this shouldn't cause any problems.

Those two can be use as follows (to display inode number of root directory, in
this example):

extern crate libc;

use std::mem;
use std::ffi::*;

fn main() {
    unsafe {
        let mut s: libc::stat = mem::zeroed();
        let path = CString::new("/").unwrap();
        let ret = libc::stat(path.as_ptr(), &mut s);
        assert_eq!(ret, 0);
        println!("{:?}", s.st_ino);
    }
}

If this actually causes problems, it will be easier to help if you actually
paste the code and/or error messages.

@portaloffreedom
Copy link
Author

I managed, I had a stupid syntax error, totally my fault. Still, wouldn't it be nice to add this example in the documentation? I think it could help other people

@alexcrichton
Copy link
Member

Thanks for the report! Rust actually has two namespaces (a type and a value namespace) for problems like this, so depending on where libc::stat is referenced it'll be unambiguous.

Currently this library has little documentation as it's just a straight binding to libc, but perhaps a clarification could be made in the book somewhere?

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