⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getopt.c

📁 C写的用软件无线电实现的GPS模拟程序
💻 C
📖 第 1 页 / 共 2 页
字号:
	{	  /* If we have just processed some options following some non-options,	     exchange them so that the options come first.  */	  if (first_nonopt != last_nonopt && last_nonopt != optind)	    exchange (argv);	  else if (last_nonopt != optind)	    first_nonopt = optind;	  /* Now skip any additional non-options	     and extend the range of non-options previously skipped.  */	  while (optind < argc		 && (argv[optind][0] != '-'		     || argv[optind][1] == 0)		 && (_getopt_long_options == 0		     || argv[optind][0] != '+'		     || argv[optind][1] == 0))	    optind++;	  last_nonopt = optind;	}      /* Special ARGV-element `--' means premature end of options.	 Skip it like a null option,	 then exchange with previous non-options as if it were an option,	 then skip everything else like a non-option.  */      if (optind != argc && !strcmp (argv[optind], "--"))	{	  optind++;	  if (first_nonopt != last_nonopt && last_nonopt != optind)	    exchange (argv);	  else if (first_nonopt == last_nonopt)	    first_nonopt = optind;	  last_nonopt = argc;	  optind = argc;	}      /* If we have done all the ARGV-elements, stop the scan	 and back over any non-options that we skipped and permuted.  */      if (optind == argc)	{	  /* Set the next-arg-index to point at the non-options	     that we previously skipped, so the caller will digest them.  */	  if (first_nonopt != last_nonopt)	    optind = first_nonopt;	  return EOF;	}	       /* If we have come to a non-option and did not permute it,	 either stop the scan or describe it to the caller and pass it by.  */      if ((argv[optind][0] != '-' || argv[optind][1] == 0)	  && (_getopt_long_options == 0	      || argv[optind][0] != '+' || argv[optind][1] == 0))	{	  if (ordering == REQUIRE_ORDER)	    return EOF;	  optarg = argv[optind++];	  return 1;	}      /* We have found another option-ARGV-element.	 Start decoding its characters.  */      nextchar = argv[optind] + 1;    }  if (_getopt_long_options != 0      && (argv[optind][0] == '+'	  || (_getopt_long_only && argv[optind][0] == '-'))      )    {      CONST struct option *p;      char *s = nextchar;      int exact = 0;      int ambig = 0;      CONST struct option *pfound = 0;      int indfound;      while (*s && *s != '=') s++;      /* Test all options for either exact match or abbreviated matches.  */      for (p = _getopt_long_options, option_index = 0; p->name; 	   p++, option_index++)	if (!strncmp (p->name, nextchar, (size_t)(s - nextchar)))	  {	    if (s - nextchar == strlen( p->name))	      {		/* Exact match found.  */		pfound = p;		indfound = option_index;		exact = 1;		break;	      }	    else if (pfound == 0)	      {		/* First nonexact match found.  */		pfound = p;		indfound = option_index;	      }	    else	      /* Second nonexact match found.  */	      ambig = 1;	  }      if (ambig && !exact)	{	  fprintf (stderr, "%s: option `%s' is ambiguous\n",		   ProgramName, argv[optind]);	  nextchar += strlen (nextchar);			   	  return '?';	}      if (pfound != 0)	{	  option_index = indfound;	  optind++;	  if (*s)	    {	      if (pfound->has_arg > 0)		optarg = s + 1;	      else		{		  fprintf (stderr,			   "%s: option `%c%s' doesn't allow an argument\n",			   ProgramName, argv[optind - 1][0], pfound->name);		  nextchar += strlen (nextchar);			   		  return '?';		}	    }	  else if (pfound->has_arg == 1)	    {	      if (optind < argc)		optarg = argv[optind++];	      else		{		  fprintf (stderr, "%s: option `%s' requires an argument\n",			   ProgramName, argv[optind - 1]);		  nextchar += strlen (nextchar);		   		  return '?';		}	    }	  nextchar += strlen (nextchar);	  if (pfound->flag)	    *(pfound->flag) = pfound->val;	  return 0;	}      /* Can't find it as a long option.  If this is getopt_long_only,	 and the option starts with '-' and is a valid short	 option, then interpret it as a short option.  Otherwise it's	 an error.  */      if (_getopt_long_only == 0 || argv[optind][0] == '+' ||	  index (optstring, *nextchar) == 0)	{	  if (opterr != 0)	    fprintf (stderr, "%s: unrecognized option `%c%s'\n",		     ProgramName, argv[optind][0], nextchar);	  nextchar += strlen (nextchar);		   	  return '?';	}    }   /* Look at and handle the next option-character.  */  {    char c = *nextchar++;    char *temp = index (optstring, c);    /* Increment `optind' when we start to process its last character.  */    if (*nextchar == 0)      optind++;    if (temp == 0 || c == ':')      {	if (opterr != 0)	  {	    if (c < 040 || c >= 0177)	      fprintf (stderr, "%s: unrecognized option, character code 0%o\n",		       ProgramName, c);	    else	      fprintf (stderr, "%s: unrecognized option `-%c'\n",		       ProgramName, c);	  }	return '?';      }    if (temp[1] == ':')      {	if (temp[2] == ':')	  {	    /* This is an option that accepts an argument optionally.  */	    if (*nextchar != 0)	      {	        optarg = nextchar;		optind++;	      }	    else	      optarg = 0;	    nextchar = 0;	  }	else	  {	    /* This is an option that requires an argument.  */	    if (*nextchar != 0)	      {		optarg = nextchar;		/* If we end this ARGV-element by taking the rest as an arg,		   we must advance to the next element now.  */		optind++;	      }	    else if (optind == argc)	      {		if (opterr != 0)		  fprintf (stderr, "%s: option `-%c' requires an argument\n",			   ProgramName, c);		c = '?';	      }	    else	      /* We already incremented `optind' once;		 increment it again when taking next ARGV-elt as argument.  */	      optarg = argv[optind++];	    nextchar = 0;	  }      }    return c;  }}#ifdef TEST/* Compile with -DTEST to make an executable for use in testing   the above definition of `getopt'.  */UBYTE ProgramName[] = "getopt";intmain ( int argc, char **argv){  char c;  int digit_optind = 0;  while (1)    {      int this_option_optind = optind;      if ((c = GetOpt (argc, argv, "abc:d:0123456789")) == EOF)	break;      switch (c)	{	case '0':	case '1':	case '2':	case '3':	case '4':	case '5':	case '6':	case '7':	case '8':	case '9':	  if (digit_optind != 0 && digit_optind != this_option_optind)	    printf ("digits occur in two different argv-elements.\n");	  digit_optind = this_option_optind;	  printf ("option %c\n", c);	  break;	case 'a':	  printf ("option a\n");	  break;	case 'b':	  printf ("option b\n");	  break;	case 'c':	  printf ("option c with value `%s'\n", optarg);	  break;	case '?':	  break;	default:	  printf ("?? getopt returned character code 0%o ??\n", c);	}    }  if (optind < argc)    {      printf ("non-option ARGV-elements: ");      while (optind < argc)	printf ("%s ", argv[optind++]);      printf ("\n");    }  return 0;}#endif /* TEST *//* ============================ End of File ========================= */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -