C language isatty function and ttyname function and sendmsg function usage

  • 2020-04-02 03:20:44
  • OfStack

Isatty () function
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.

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 language sendmsg() function: the transfer of data through a socket
The header file:


#include <sys/types.h>  #include <sys/socket.h>

Definition function:


int sendmsg(int s, const strcut msghdr *msg, unsigned int flags);

Sendmsg () is used to send data from the specified socket to the other host.

The structure MSGHDR is defined as follows:


struct msghdr
{
  void *msg_name; //Address to send to /receive from .
  socklen_t msg_namelen; //Length of addres data
  strcut iovec * msg_iov; //Vector of data to send/receive into
  size_t msg_iovlen; //Number of elements in the vector
  void * msg_control; //Ancillary dat
  size_t msg_controllen; //Ancillary data buffer length
  int msg_flags; //Flags on received message
};

Return value: returns the number of characters actually sent on success, returns -1 on failure, and the error is in errno

Error code:
1. Illegal socket processing code for EBADF parameter s.
2. The EFAULT parameter has a pointer to an inaccessible memory space
3. ENOTSOCK parameter s is a file descriptor, not a socket.
4. EINTR is interrupted by the signal.
5. This operation will block the process, but the socket of parameter s is not blocked.
6. The buffer memory of ENOBUFS system is insufficient
7, ENOMEM core memory is out of EINVAL to the system call parameters are not correct.


Related articles: