2 * Copyright 2003, Daniel Reinhold, danielre@users.sf.net. All rights reserved.
3 * Copyright 2007, François Revol, mmu_man@users.sf.net. All rights reserved.
4 * Distributed under the terms of the MIT License.
8 #include <SupportDefs.h>
16 #include <sys/param.h>
18 #include <errno_private.h>
22 * give the name of a tty fd. threadsafe.
23 * @param fd the tty to get the name from.
24 * @param buffer where to store the name to.
25 * @param bufferSize length of the buffer.
26 * @return 0 on success, -1 on error, sets errno.
29 ttyname_r(int fd
, char *buffer
, size_t bufferSize
)
33 // first, some sanity checks:
34 if (fstat(fd
, &fdStat
) < 0)
37 if (!S_ISCHR(fdStat
.st_mode
) || !isatty(fd
))
41 if (ioctl(fd
, B_GET_PATH_FOR_DEVICE
, buffer
, bufferSize
) < 0)
48 * give the name of a tty fd.
49 * @param fd the tty to get the name from.
50 * @return the name of the tty or NULL on error.
55 static char pathname
[MAXPATHLEN
];
57 int err
= ttyname_r(fd
, pathname
, sizeof(pathname
));