25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

130 satır
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. #ifndef _UINTPTR_T
  26. typedef unsigned int uintptr_t;
  27. #endif
  28. #endif
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include "mygetopt.h"
  32. int myoptind = 1;
  33. char *myoptarg = NULL;
  34. /* XXX: this getopt_long implementation should not be trusted for other
  35. * applications without any serious peer reviewing. It “just works” with
  36. * zzuf but may fail miserably in other programs. */
  37. int mygetopt(int argc, char * const _argv[], const char *optstring,
  38. const struct myoption *longopts, int *longindex)
  39. {
  40. char **argv = (char **)(uintptr_t)_argv;
  41. char *flag;
  42. int i;
  43. if(myoptind >= argc)
  44. return -1;
  45. flag = argv[myoptind];
  46. if(flag[0] == '-' && flag[1] != '-')
  47. {
  48. char *tmp;
  49. int ret = flag[1];
  50. if(ret == '\0')
  51. return -1;
  52. tmp = strchr(optstring, ret);
  53. if(!tmp || ret == ':')
  54. return '?';
  55. myoptind++;
  56. if(tmp[1] == ':')
  57. {
  58. if(flag[2] != '\0')
  59. myoptarg = flag + 2;
  60. else
  61. myoptarg = argv[myoptind++];
  62. return ret;
  63. }
  64. if(flag[2] != '\0')
  65. {
  66. flag[1] = '-';
  67. myoptind--;
  68. argv[myoptind]++;
  69. }
  70. return ret;
  71. }
  72. if(flag[0] == '-' && flag[1] == '-')
  73. {
  74. if(flag[2] == '\0')
  75. return -1;
  76. for(i = 0; longopts[i].name; i++)
  77. {
  78. size_t l = strlen(longopts[i].name);
  79. if(strncmp(flag + 2, longopts[i].name, l))
  80. continue;
  81. switch(flag[2 + l])
  82. {
  83. case '=':
  84. if(!longopts[i].has_arg)
  85. goto bad_opt;
  86. if(longindex)
  87. *longindex = i;
  88. myoptind++;
  89. myoptarg = flag + 2 + l + 1;
  90. return longopts[i].val;
  91. case '\0':
  92. if(longindex)
  93. *longindex = i;
  94. myoptind++;
  95. if(longopts[i].has_arg)
  96. myoptarg = argv[myoptind++];
  97. return longopts[i].val;
  98. default:
  99. break;
  100. }
  101. }
  102. bad_opt:
  103. fprintf(stderr, "%s: unrecognized option `%s'\n", argv[0], flag);
  104. return '?';
  105. }
  106. return -1;
  107. }