Explain the usage of ttyname of function and isatty of function in C language
- 2020-04-02 03:20:46
- OfStack
C language ttyname() function: returns the name of a terminal
The header file:
#include <unistd.h>
Definition function:
char * ttyname(int desc);
Function description: if the file descriptor represented by parameter desc is a terminal, the name of the terminal is returned by a string pointer, otherwise NULL is returned.
Return value: returns a string pointer to the name of the terminal on success, or NULL if an error occurs.
sample
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
main()
{
int fd;
char * file = "/dev/tty";
fd = open (fiel, O_RDONLY);
printf("%s", file);
if(isatty(fd))
{
printf("is a tty. n");
printf("ttyname = %s n", ttyname(fd));
}
else
printf(" is not a ttyn");
close(fd);
}
Perform:
/dev/tty is a tty ttyname = /dev/tty
C isatty() function: determines if the file descriptor isa terminal
The header file:
#include <unistd.h>
Definition function:
int isatty(int desc);
Returns 1 if the file descriptor represented by parameter desc is a terminal, or 0 if not.
Return value: returns 1 if the file is a terminal, or 0 if it is not.