C language isatty function and ttyname function and sendmsg function usage _c language

Source: Internet
Author: User
Tags sendmsg

Isatty () function
header file:

#include <unistd.h>

To define a function:

int isatty (int desc);

Function Description: If the parameter desc represented by the file descriptor for a terminal then return 1, otherwise return 0.

Return value: Returns 1 if the file is a terminal, or 0.

C language Ttyname () function: return a terminal name
header file:

#include <unistd.h>

To define a function:

char * ttyname (int desc);

Function Description: If the parameter desc represents a file descriptor that is a terminal, the terminal name is returned by a string pointer, or NULL is returned.

Return value: Returns a string pointer to the terminal name if successful, and returns null if an error occurs.

Example

#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 ("Not a tty\n");
  Close (FD);
}

Perform:

/dev/tty is a tty ttyname =/dev/tty

C language Sendmsg () function: data transfer via socket
header file:

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

To define a function:

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

Function Description: sendmsg () is used to pass data from the specified socket to the other host. The parameter s is a socket that has been established, and if the UDP protocol is used, it does not need to be wired. Parameter msg points to the content of the data structure of the connection, parameter flags are generally default to 0, detailed description please refer to send ().

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 lengt h
  int msg_flags;//flags on received message
};

Return value: Success returns the number of characters actually transmitted, failure returns 1, and the reason for the error is errno

Error code:
1, EBADF parameter s is not valid socket processing code.
2, the Efault parameter has a pointer to the memory space that cannot be accessed
3, Enotsock parameter S is a document description word, not socket.
4, Eintr was interrupted by the signal.
5, Eagain This operation will block the process, but the parameter s socket is not blocked.
6, ENOBUFS system buffer memory is not enough
7, Enomem core memory einval passed to system calls is incorrect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.