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

📄 gnu_getopt.c

📁 linux下用PCMCIA无线网卡虚拟无线AP的程序源码
💻 C
📖 第 1 页 / 共 2 页
字号:
			     "%s: option `%s' is ambiguous\n"),		     argv[0], argv[optind]);#else	    fprintf (stderr, "%s: option `%s' is ambiguous\n",		     argv[0], argv[optind]);#endif	  nextchar += strlen (nextchar);	  optind++;	  return '?';	}      if (pfound != NULL)	{	  option_index = indfound;	  optind++;	  if (*nameend)	    {	      /* Don't test has_arg with >, because some C compilers don't		 allow it to be used on enums.  */	      if (pfound->has_arg)		optarg = nameend + 1;	      else		{		  if (opterr)		    {		      if (argv[optind - 1][1] == '-')			/* --option */#if NLS			fprintf (stderr,				 catgets(_libc_cat, GetoptSet, GetoptNoArgumentsAllowed1,				 "%s: option `--%s' doesn't allow an argument\n"),				 argv[0], pfound->name);#else			fprintf (stderr,				 "%s: option `--%s' doesn't allow an argument\n",				 argv[0], pfound->name);#endif		      else			/* +option or -option */#if NLS			fprintf (stderr,			     catgets(_libc_cat, GetoptSet, GetoptNoArgumentsAllowed2,			     "%s: option `%c%s' doesn't allow an argument\n"),			     argv[0], argv[optind - 1][0], pfound->name);#else			fprintf (stderr,			     "%s: option `%c%s' doesn't allow an argument\n",			     argv[0], argv[optind - 1][0], pfound->name);#endif		    }		  nextchar += strlen (nextchar);		  return '?';		}	    }	  else if (pfound->has_arg == 1)	    {	      if (optind < argc)		optarg = argv[optind++];	      else		{		  if (opterr)#if NLS		    fprintf (stderr,			     catgets(_libc_cat, GetoptSet, GetoptRequiresArgument1,		             "%s: option `%s' requires an argument\n"),			     argv[0], argv[optind - 1]);#else		    fprintf (stderr, "%s: option `%s' requires an argument\n",			     argv[0], argv[optind - 1]);#endif		  nextchar += strlen (nextchar);		  return optstring[0] == ':' ? ':' : '?';		}	    }	  nextchar += strlen (nextchar);	  if (longind != NULL)	    *longind = option_index;	  if (pfound->flag)	    {	      *(pfound->flag) = pfound->val;	      return 0;	    }	  return pfound->val;	}      /* Can't find it as a long option.  If this is not getopt_long_only,	 or the option starts with '--' or is not a valid short	 option, then it's an error.	 Otherwise interpret it as a short option.  */      if (!long_only || argv[optind][1] == '-'	  || my_index (optstring, *nextchar) == NULL)	{	  if (opterr)	    {	      if (argv[optind][1] == '-')		/* --option */#if NLS		fprintf (stderr,			 catgets(_libc_cat, GetoptSet, GetoptUnrecognized1,			 "%s: unrecognized option `--%s'\n"),			 argv[0], nextchar);#else		fprintf (stderr, "%s: unrecognized option `--%s'\n",			 argv[0], nextchar);#endif	      else		/* +option or -option */#if NLS		fprintf (stderr,			 catgets(_libc_cat, GetoptSet, GetoptUnrecognized2,			 "%s: unrecognized option `%c%s'\n"),			 argv[0], argv[optind][0], nextchar);#else		fprintf (stderr, "%s: unrecognized option `%c%s'\n",			 argv[0], argv[optind][0], nextchar);#endif	    }	  nextchar = (char *) "";	  optind++;	  return '?';	}    }  /* Look at and handle the next short option-character.  */  {    char c = *nextchar++;    char *temp = my_index (optstring, c);    /* Increment `optind' when we start to process its last character.  */    if (*nextchar == '\0')      ++optind;    if (temp == NULL || c == ':')      {	if (opterr)	  {	    /* 1003.2 specifies the format of this message.  */#if NLS	    fprintf (stderr,		catgets(_libc_cat, GetoptSet, GetoptIllegal,		"%s: illegal option -- %c\n"),		argv[0], c);#else	    fprintf (stderr, "%s: illegal option -- %c\n", argv[0], c);#endif	  }	optopt = 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 = NULL;	    nextchar = NULL;	  }	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)		  {		    /* 1003.2 specifies the format of this message.  */#if NLS		    fprintf (stderr,			catgets(_libc_cat, GetoptSet,			GetoptRequiresArgument2,			"%s: option requires an argument -- %c\n"),			argv[0], c);#else		    fprintf (stderr, "%s: option requires an argument -- %c\n",			     argv[0], c);#endif		  }		optopt = c;		if (optstring[0] == ':')		  c = ':';		else		  c = '?';	      }	    else	      /* We already incremented `optind' once;		 increment it again when taking next ARGV-elt as argument.  */	      optarg = argv[optind++];	    nextchar = NULL;	  }      }    return c;  }}intgetopt (argc, argv, optstring)     int argc;     char *const *argv;     const char *optstring;{  return _getopt_internal (argc, argv, optstring,			   (const struct option *) 0,			   (int *) 0,			   0);}#endif /* L__gnu_getopt_internal */#ifdef L_gnu_getopt_longintgetopt_long (argc, argv, options, long_options, opt_index)     int argc;     char *const *argv;     const char *options;     const struct option *long_options;     int *opt_index;{  return _getopt_internal (argc, argv, options, long_options, opt_index, 0);}#endif /*  L_gnu_getopt_long */#ifdef L_gnu_getopt_long_only/* Like getopt_long, but '-' as well as '--' can indicate a long option.   If an option that starts with '-' (not '--') doesn't match a long option,   but does match a short option, it is parsed as a short option   instead.  */intgetopt_long_only (argc, argv, options, long_options, opt_index)     int argc;     char *const *argv;     const char *options;     const struct option *long_options;     int *opt_index;{  return _getopt_internal (argc, argv, options, long_options, opt_index, 1);}#endif /* L_gnu_getopt_long_only */#ifdef TEST/* Compile with -DTEST to make an executable for use in testing   the above definition of `getopt'.  */intmain (argc, argv)     int argc;     char **argv;{  int c;  int digit_optind = 0;  while (1)    {      int this_option_optind = optind ? optind : 1;      c = getopt (argc, argv, "abc:d:0123456789");      if (c == 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");    }  exit (0);}#endif /* TEST */  /* getopt_long testing */#ifdef TEST_LONG/* Compile with -DTEST_LONG to make an executable for use in testing   the above definition of `getopt'.  */intmain (argc, argv)     int argc;     char **argv;{  int c;  int digit_optind = 0;  while (1)    {      int this_option_optind = optind ? optind : 1;      int option_index = 0;      static struct option long_options[] =      {	{"add", 1, 0, 0},	{"append", 0, 0, 0},	{"delete", 1, 0, 0},	{"verbose", 0, 0, 0},	{"create", 0, 0, 0},	{"file", 1, 0, 0},	{0, 0, 0, 0}      };      c = getopt_long (argc, argv, "abc:d:0123456789",		       long_options, &option_index);      if (c == EOF)	break;      switch (c)	{	case 0:	  printf ("option %s", long_options[option_index].name);	  if (optarg)	    printf (" with arg %s", optarg);	  printf ("\n");	  break;	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 'd':	  printf ("option d 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");    }  exit (0);}#endif /* TEST_LONG */

⌨️ 快捷键说明

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