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

Use ttyname_r on BSDs #3

Open
Artoria2e5 opened this issue Apr 3, 2020 · 3 comments
Open

Use ttyname_r on BSDs #3

Artoria2e5 opened this issue Apr 3, 2020 · 3 comments

Comments

@Artoria2e5
Copy link

Artoria2e5 commented Apr 3, 2020

ttyname_r is a POSIX function for getting the name of any TTY, including a PTY, in an reentrant way. A POSIX (and nix::libc) compliant declaration is found in OpenBSD (bitrig too?), DragonFly, NetBSD, and FreeBSD -- so basically every value of target_os except Windows and the can be covered now.

Pseudocode:

  1. Check via isatty that fd is a tty. If not, error. (ttyname_r will complain later too.)
  2. Use sysconf to get TTY_NAME_MAX as sz. If not present or error, assume a default of 128.
  3. Allocate a buffer of the size sz.
  4. Call ttyname_r(fd, buf, sz).
  5. Tame the C string.

A quick of this thing is that some implementations return the old style /dev/ttyp name.

Don't use it on the mac -- gnulib says Apple fucked the declaration up.

@nelsonjchen
Copy link
Contributor

Hmm, it's not available in Nix. Maybe this should be added there. Would they be game?

https://docs.rs/nix/0.17.0/nix/?search=ttyname

@Artoria2e5
Copy link
Author

Artoria2e5 commented Apr 3, 2020

The libc crate it re-exports has ttyname_r: https://docs.rs/libc/0.2.66/libc/fn.ttyname_r.html (with the usual unsafe and errno territory warnings.) It uses a POSIX-compliant link name on macs too.

And yeah, it would be a good idea to wrap it safely...

@Artoria2e5
Copy link
Author

nix-rust/nix#1204

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

No branches or pull requests

2 participants