Two sections of Linux code, project decommissioned, some use, throwing a pity

Source: Internet
Author: User
Two sections of Linux code, project decommissioned, and some use, throwing away a pity-general Linux technology-Linux programming and kernel information. The following is a detailed description. 1. For normal Linux input, you must press enter to receive the input. You can use this function to perform each response. However, when a BUG is printf, A '\ n' must be added for output. The cause is unknown.

Void get_callee_number (char * buf)
{
Fd_set keyset;
Struct timeval TV;
Int keyfd = fileno (stdin), ret;
Char c;
Struct termios save, ne;
Int I = 0;

Ioctl (0, TCGETS, & save );
Ioctl (0, TCGETS, & ne );
Ne. c_lflag & = ~ (ECHO | ICANON );
Ioctl (0, TCSETS, & ne );

While (1)
{
TV. TV _sec = 2;
TV. TV _usec = 0;
FD_SET (keyfd, & keyset );
Ret = select (keyfd + 1, & keyset, NULL, NULL, & TV );
If (ret = 0 & strlen (buf )! = 0)
Break;
If (FD_ISSET (keyfd, & keyset ))
{
Read (keyfd, & c, 1 );
Buf [I ++] = c;
// Printf ("the key is % c \ n", c );
}
If (c! = 0) printf ("% c \ n", c );

}
Ioctl (0, TCSETS, & save );
}

2. Obtain the local Internet IP address (the first Nic)

Void get_local_ip (int family, char * address, int size)
{
Int fd, intrface;
Struct ifreq buf [16];
// Struct arpreq arp;
Struct ifconf ifc;

Memset (address, 0, size );

If (fd = socket (family, SOCK_DGRAM, 0)> = 0)
{
Ifc. ifc_len = sizeof buf;
Ifc. ifc_buf = (caddr_t) buf;
If (! Ioctl (fd, SIOCGIFCONF, (char *) & ifc ))
{
Intrface = ifc. ifc_len/sizeof (struct ifreq)-1;
// Obtain the IP address of the current Nic
If (! (Ioctl (fd, SIOCGIFADDR, (char *) & buf [intrface])
{
// Strcpy (address, inet_ntoa (struct sockaddr_in *) (& buf [intrface]. ifr_addr)-> sin_addr ));
Snprintf (address, size, (char *) inet_ntoa (struct sockaddr_in *) (& buf [intrface]. ifr_addr)-> sin_addr ));
}
Else
{
Char str [256];
Sprintf (str, "cpm: ioctl device % s", buf [intrface]. ifr_name );
Perror (str );
}
} Else
Perror ("cpm: ioctl ");
} Else
Perror ("cpm: socket ");
Close (fd );
}
Related Article

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.