You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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