Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

128 строки
3.0 KiB

  1. /*
  2. * zzuf - general purpose fuzzer
  3. * Copyright (c) 2002, 2007 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id: mygetopt.c 271 2007-02-02 11:58:06Z sam $
  7. *
  8. * This program is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * mygetopt.c: getopt_long reimplementation
  16. */
  17. #include "config.h"
  18. #if defined HAVE_STDINT_H
  19. # include <stdint.h>
  20. #elif defined HAVE_INTTYPES_H
  21. # include <inttypes.h>
  22. #endif
  23. /* DOS / Kernel driver */
  24. #ifndef __intptr_t_defined
  25. typedef unsigned int uintptr_t;
  26. #endif
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include "mygetopt.h"
  30. int myoptind = 1;
  31. char *myoptarg = NULL;
  32. /* XXX: this getopt_long implementation should not be trusted for other
  33. * applications without any serious peer reviewing. It “just works” with
  34. * zzuf but may fail miserably in other programs. */
  35. int mygetopt(int argc, char * const _argv[], const char *optstring,
  36. const struct myoption *longopts, int *longindex)
  37. {
  38. char **argv = (char **)(uintptr_t)_argv;
  39. char *flag;
  40. int i;
  41. if(myoptind >= argc)
  42. return -1;
  43. flag = argv[myoptind];
  44. if(flag[0] == '-' && flag[1] != '-')
  45. {
  46. char *tmp;
  47. int ret = flag[1];
  48. if(ret == '\0')
  49. return -1;
  50. tmp = strchr(optstring, ret);
  51. if(!tmp || ret == ':')
  52. return '?';
  53. myoptind++;
  54. if(tmp[1] == ':')
  55. {
  56. if(flag[2] != '\0')
  57. myoptarg = tmp + 2;
  58. else
  59. myoptarg = argv[myoptind++];
  60. return ret;
  61. }
  62. if(flag[2] != '\0')
  63. {
  64. flag[1] = '-';
  65. myoptind--;
  66. argv[myoptind]++;
  67. }
  68. return ret;
  69. }
  70. if(flag[0] == '-' && flag[1] == '-')
  71. {
  72. if(flag[2] == '\0')
  73. return -1;
  74. for(i = 0; longopts[i].name; i++)
  75. {
  76. size_t l = strlen(longopts[i].name);
  77. if(strncmp(flag + 2, longopts[i].name, l))
  78. continue;
  79. switch(flag[2 + l])
  80. {
  81. case '=':
  82. if(!longopts[i].has_arg)
  83. goto bad_opt;
  84. if(longindex)
  85. *longindex = i;
  86. myoptind++;
  87. myoptarg = flag + 2 + l + 1;
  88. return longopts[i].val;
  89. case '\0':
  90. if(longindex)
  91. *longindex = i;
  92. myoptind++;
  93. if(longopts[i].has_arg)
  94. myoptarg = argv[myoptind++];
  95. return longopts[i].val;
  96. default:
  97. break;
  98. }
  99. }
  100. bad_opt:
  101. fprintf(stderr, "%s: unrecognized option `%s'\n", argv[0], flag);
  102. return '?';
  103. }
  104. return -1;
  105. }