Skip to content

Implement get_winsize() for unix. #20613

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

Merged
merged 1 commit into from
Jan 25, 2015
Merged

Implement get_winsize() for unix. #20613

merged 1 commit into from
Jan 25, 2015

Conversation

ZoeyR
Copy link

@ZoeyR ZoeyR commented Jan 6, 2015

No description provided.

@rust-highfive
Copy link
Contributor

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikomatsakis (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see CONTRIBUTING.md for more information.

@amaranth
Copy link
Contributor

amaranth commented Jan 6, 2015

I think it'd be nicer if you had a const TIOCGWINSZ for the magic value. Also, is there a chance the ioctl could fail but write garbage in to the struct? That would currently get past your test but the call to ioctl returns -1 on error which should be a clearer indicator of a problem.

@ZoeyR
Copy link
Author

ZoeyR commented Jan 6, 2015

I missed the error return, that would be better. I'm not sure defining the const is necessary for this one use.

@ZoeyR ZoeyR changed the title Implement winsize() for unix. Implement get_winsize() for unix. Jan 7, 2015
@nikomatsakis
Copy link
Contributor

Is there any way to write a test for this?

@ZoeyR
Copy link
Author

ZoeyR commented Jan 7, 2015

I can't really think of a way to write a test for this since most tests will not take place in a tty. Not to mention if the test did take place in a tty it would have to always be of the same size for the test to work correctly.

@tbu-
Copy link
Contributor

tbu- commented Jan 8, 2015

@dgriffen Defining the constant gives later readers something to google for.

}

let mut size = winsize { ws_row: 0, ws_col: 0, ws_pixel: 0, ws_ypixel: 0 };
if funcs::bsd44::ioctl(0, 0x00005413, &mut size) == -1 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this literal into a const with a corresponding name that it has in C?

@alexcrichton
Copy link
Member

Thanks @dgriffen! Just one minor nit (that was also previously mentioned!) and otherwise r=me

@utkarshkukreti
Copy link
Contributor

Is this supposed to work on OSX? The value of TIOCGWINSZ on OSX is not the same as Linux.

}

let mut size = winsize { ws_row: 0, ws_col: 0, ws_pixel: 0, ws_ypixel: 0 };
if funcs::bsd44::ioctl(0, TIOCGWINSZ, &mut size) == -1 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/0/self.fd.fd()?

@alexcrichton
Copy link
Member

ping @dgriffen, any thoughts on @utkarshkukreti's comments?

@ZoeyR
Copy link
Author

ZoeyR commented Jan 20, 2015

I don't have a mac to test, so i can't confirm this. But it appears TIOCGWINSZ only has a different value on powerpc macs. I could include the other value with a compiler directive. But i would like someone to confirm the value first.

@alexcrichton
Copy link
Member

The value I have locally (OSX 10.10) for TIOCGWINSZ is 0x40087468, and I also just checked that a Ubuntu server has the value 0x00005413 (as listed)

@ZoeyR
Copy link
Author

ZoeyR commented Jan 20, 2015

Is you mac a powerpc or intel?

@alexcrichton
Copy link
Member

I'm using an intel mac

@ZoeyR
Copy link
Author

ZoeyR commented Jan 21, 2015

Updated the code with macos value. I cannot confirm that it works on mac, for aforementioned reasons. I have screenshots of a test on arch linux running gnome terminal.

test.rs code is [here](http://pastebin.com/63vJ0tAX)

@alexcrichton
Copy link
Member

Thanks! Could you squash the commits together as well?

@ZoeyR
Copy link
Author

ZoeyR commented Jan 21, 2015

Squashed everything together, should be ready to go.

@@ -19,6 +19,12 @@ pub struct TTY {
pub fd: FileDesc,
}

#[cfg(target_os = "macos")]
const TIOCGWINSZ: i32 = 0x40087468;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this (and the one below) should be of type c_ulong, not i32. Typo?

@ZoeyR
Copy link
Author

ZoeyR commented Jan 21, 2015

the ioctl function expects an i32, c_ulong is 64bit on 64bit machines, and has no way of being passes into ioctl.

Edit: Did a quick look, it appears that the version of ioctl I was calling expected an i32, found a version at c::iotcl that expects c_ulong, I am compiling and testing now.

@utkarshkukreti
Copy link
Contributor

Apparently liblibc defines ioctl to take c_ulong on Mac/BSDs and c_int on Linux/Android.

@ZoeyR
Copy link
Author

ZoeyR commented Jan 21, 2015

Also, it looks like openbsd used 0x40087468 as the value for TIOCGWINSZ, so I'll update that as well.

@alexcrichton
Copy link
Member

@bors: r+ 063b0f0

@alexcrichton
Copy link
Member

Thanks!

@bors
Copy link
Collaborator

bors commented Jan 22, 2015

⌛ Testing commit 063b0f0 with merge 7926a5d...

@bors
Copy link
Collaborator

bors commented Jan 22, 2015

💔 Test failed - auto-linux-64-x-android-t

@ZoeyR
Copy link
Author

ZoeyR commented Jan 22, 2015

I'll add android to the list for TIOCGWINSZ. Are there any other targets I need to add?

@ZoeyR
Copy link
Author

ZoeyR commented Jan 22, 2015

If I am unable to find a value for a specific os, should i leave code for that os as unimplemented?

@alexcrichton
Copy link
Member

I found this locally:

./ndk_standalone/sysroot/usr/include/asm/ioctls.h:#define TIOCGWINSZ 0x5413

Other platforms which are built include iOS and dragonfly. Perhaps this function could only be defined for supported platforms for now?

@ZoeyR
Copy link
Author

ZoeyR commented Jan 25, 2015

Made changes to the code that should allow it to compile on all platforms. For now ios and dragonfly remain unimplemented.

@alexcrichton
Copy link
Member

@bors: r+ ec88426

@bors
Copy link
Collaborator

bors commented Jan 25, 2015

⌛ Testing commit ec88426 with merge 0899807...

bors added a commit that referenced this pull request Jan 25, 2015
@bors
Copy link
Collaborator

bors commented Jan 25, 2015

@bors bors merged commit ec88426 into rust-lang:master Jan 25, 2015
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

Successfully merging this pull request may close these issues.

8 participants