@@ -100,7 +100,7 @@ pub fn grantpt(fd: &PtyMaster) -> Result<()> {
100
100
/// unlockpt(&master_fd)?;
101
101
///
102
102
/// // Get the name of the slave
103
- /// let slave_name = ptsname(&master_fd)?;
103
+ /// let slave_name = unsafe { ptsname(&master_fd) } ?;
104
104
///
105
105
/// // Try to open the slave
106
106
/// # #[allow(unused_variables)]
@@ -125,20 +125,26 @@ pub fn posix_openpt(flags: fcntl::OFlag) -> Result<PtyMaster> {
125
125
/// [ptsname(3)](http://man7.org/linux/man-pages/man3/ptsname.3.html))
126
126
///
127
127
/// `ptsname()` returns the name of the slave pseudoterminal device corresponding to the master
128
- /// referred to by `fd`. Note that this function is *not* threadsafe. For that see `ptsname_r()`.
128
+ /// referred to by `fd`.
129
129
///
130
130
/// This value is useful for opening the slave pty once the master has already been opened with
131
131
/// `posix_openpt()`.
132
+ ///
133
+ /// # Safety
134
+ ///
135
+ /// `ptsname()` mutates global variables and is *not* threadsafe.
136
+ /// Mutating global variables is always considered `unsafe` by Rust and this
137
+ /// function is marked as `unsafe` to reflect that.
138
+ ///
139
+ /// For a threadsafe and non-`unsafe` alternative on Linux, see `ptsname_r()`.
132
140
#[ inline]
133
- pub fn ptsname ( fd : & PtyMaster ) -> Result < String > {
134
- let name_ptr = unsafe { libc:: ptsname ( fd. as_raw_fd ( ) ) } ;
141
+ pub unsafe fn ptsname ( fd : & PtyMaster ) -> Result < String > {
142
+ let name_ptr = libc:: ptsname ( fd. as_raw_fd ( ) ) ;
135
143
if name_ptr. is_null ( ) {
136
144
return Err ( Error :: last ( ) . into ( ) ) ;
137
145
}
138
146
139
- let name = unsafe {
140
- CStr :: from_ptr ( name_ptr)
141
- } ;
147
+ let name = CStr :: from_ptr ( name_ptr) ;
142
148
Ok ( name. to_string_lossy ( ) . into_owned ( ) )
143
149
}
144
150
@@ -187,7 +193,7 @@ pub fn unlockpt(fd: &PtyMaster) -> Result<()> {
187
193
188
194
/// Create a new pseudoterminal, returning the slave and master file descriptors
189
195
/// in `OpenptyResult`
190
- /// (see [openpty](http://man7.org/linux/man-pages/man3/openpty.3.html)).
196
+ /// (see [openpty](http://man7.org/linux/man-pages/man3/openpty.3.html)).
191
197
///
192
198
/// If `winsize` is not `None`, the window size of the slave will be set to
193
199
/// the values in `winsize`. If `termios` is not `None`, the pseudoterminal's
0 commit comments