Linux C functions-Terminal Control

Source: Internet
Author: User
Common Linux C functions-terminal control-general Linux technology-Linux programming and kernel information. For more information, see the following section. Getopt (analyze command line parameters)
Related functions

Header file # include

Define the function int getopt (int argc, char * const argv [], const char * optstring );

Function Description getopt () is used to analyze command line parameters. The argc and argv parameters are the number and content of parameters passed by main. The optstring parameter indicates the option string to be processed. This function returns the option letter in argv, which corresponds to the letter in the optstring parameter. If the letter in the option string is followed by the colon ":", it indicates that there are related parameters. The global variable optarg points to this additional parameter. If getopt () fails to find the correct parameter, an error message is printed and the global variable optopt is set to "?". If you do not want getopt () to print the error message, set the global variable opterr to 0.

Return Value: If a matching parameter is found, a letter is returned. If the parameter is not included in the option Letter of The optstring parameter, "?" is returned. Character.-1 is returned when the analysis ends.

Example # include
# Include
Int main (int argc, char ** argv)
{
Int ch;
Opterr = 0;
While (ch = getopt (argc, argv, "a: bcde "))! =-1)
Switch (ch)
{
Case 'A ':
Printf ("option a: '% s' \ n", optarg );
Break;
Case 'B ':
Printf ("option B: B \ n ");
Break;
Default:
Printf ("other option: % c \ n", ch );
}
Printf ("optopt + % c \ n", optopt );
}

Run $./getopt? B
Option B: B
$./Getopt? C
Other option: c
$./Getopt? A
Other option :?
$./Getopt? A12345
Option a: '20140901'

 




Isatty (determine whether the file description is a terminal)
Related Function ttyname

Header file # include

Defines the int isatty (int desc) function );

Function Description: If the desc parameter represents a terminal, 1 is returned; otherwise, 0 is returned.

Return Value: if the file is a terminal, 1 is returned; otherwise, 0 is returned.

For more information, see ttyname ().

 




Select (I/O multi-work mechanism)
Header file # include
# Include
# Include

Define the int select function (int n, fd_set * readfds, fd_set * writefds, fd_set * limit TFDs, struct timeval * timeout );

Function Description: select () is used to wait for the state change of the description word of the file. Parameter n represents the largest file descriptive word plus 1. The readfds, writefds, and limit TFDs parameters are called descriptive phrases and are used to return the reading, writing, or exception of the descriptive word. The macro below provides a way to deal with these three descriptive phrases:
FD_CLR (inr fd, fd_set * set); used to clear the related fd bits in the description phrase set.
FD_ISSET (int fd, fd_set * set); used to test whether the bits of the related fd in the phrase set are true.
FD_SET (int fd, fd_set * set); used to set the fd bit in the description phrase set.
FD_ZERO (fd_set * set); used to clear all bits of the phrase set.

The timeout parameter is a structure timeval used to set the select () wait time. Its structure is defined as follows:
Struct timeval
{
Time_t TV _sec;
Time_t TV _usec;
};

Return value if the timeout parameter is set to NULL, it indicates that select () has no timeout.

If the error code is successfully executed, the number of changes in the state of the description word in the file is returned. If 0 is returned, it indicates that the time before the State of the description word changes has exceeded the timeout time. If an error occurs,-1 is returned, the error cause is stored in errno. The value of readfds, writefds, limit TFDs, and timeout parameters becomes unpredictable.
The description of the EBADF file is invalid or the file is closed.
EINTR this call is interrupted by the signal
The value of n is negative.
Insufficient ENOMEM core memory

Sample Common Program snippets: fs_set readset;
FD_ZERO (& readset );
FD_SET (fd, & readset );
Select (fd + 1, & readset, NULL );
If (FD_ISSET (fd, readset ){......}

 




Ttyname (return the name of a terminal)
Isatty

Header file # include

Define the function char * ttyname (int desc );

Function Description: If the desc parameter represents a terminal, the terminal name is returned by a string pointer. Otherwise, NULL is returned.

If the return value is successful, a string pointer to the terminal name is returned. If an error occurs, NULL is returned.

Example # include
# Include
# Include
# Include
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 tty \ n ");
Close (fd );
}

Run/dev/tty is a tty
Ttyname =/dev/tty
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.