Isatty () function
header file:
To define a function:
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:
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.