The use of getopt () function and select () function in C language _c language

Source: Internet
Author: User

C language Getopt () function: parsing command line arguments
header File

#include <unistd.h>

To define a function:

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

Function Description: getopt () is used to parse command line arguments.
1. Parameter argc and argv are the number and contents of the parameters passed by main ().
2. The parameter optstring represents the option string to be processed.

This function returns the next option letter in argv, which corresponds to the letter in the parameter optstring.

If the letter in the option string followed by a colon ":" indicates that there are also related parameters, the global variable Optarg points to this extra parameter.
If Getopt () fails to find a conforming parameter, the error message is printed and the global variable optopt is set to? Character, if you do not want getopt () to print an error message, just set the global variable Opterr to 0.

Return value: Returns this parameter letter if a conforming parameter is found, and returns "?" if the argument does not contain an option letter in the parameter optstring. character, and the end of analysis returns-1.

Example

#include <stdio.h>
#include <unistd.h>
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");
   Default:
     printf ("Other option:%c\n", ch);
  }
  printf ("Optopt +%c\n", optopt);
}

Perform:

$. /getopt-b
option B:b
$./getopt-c
option:c
$./getopt-a other
option:?
$. /getopt-a12345
Option A: ' 12345 '

C language Select () function: I/O multi-work mechanism
to define a function:

int select (int n, fd_set * Readfds, Fd_set * Writefds, Fd_set * Exceptfds, struct timeval * timeout);

Function Description: Select () is used to wait for changes in the state of the file description Word. The parameter n represents the largest file descriptor plus 1, and the parameters Readfds, Writefds, and Exceptfds are called descriptive phrases that are used to return the read, write, or exception of the descriptive word. The macro below provides a way to handle these three types of phrases:

    • FD_CLR (INR fd, fd_set* set); Used to clear a bit of the associated FD in the phrase set
    • Fd_isset (int fd, fd_set *set); Used to test whether a bit of the associated FD in the phrase set is true
    • Fd_set (int fd, fd_set*set); Used to set the bit of the associated FD in the phrase set
    • Fd_zero (Fd_set *set); Used to clear all bits of the description phrase set

Parameter timeout is a struct timeval used to set the wait time for select (), which is defined as follows:

struct Timeval
{
  time_t tv_sec;
  time_t tv_usec;
};

Return value: If the parameter timeout is set to NULL, the Select () has no timeout.

Error code: Successful execution returns the number of changes in the state of the file descriptor, if the return 0 represents more than timeout time before the state of the descriptor changes, and returns 1 when there is an error, the reason for the error is in errno, at this point the parameter Readfds, Writefds, Exceptfds and timeout values become unpredictable.

    • EBADF file descriptor is invalid or the file is closed
    • Eintr This call is interrupted by a signal
    • The EINVAL parameter n is a negative value.
    • Enomem Core memory is low

Example:
Common pieces of the program:

Fs_set Readset;
Fd_zero (&readset);
Fd_set (FD, &readset);
Select (fd+1, &readset, NULL, NULL, NULL);
if (Fd_isset (FD, Readset) {...}

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.