getsubopt-susv3.c 628 B

123456789101112131415161718192021222324252627282930313233
  1. /*
  2. * Copyright (C) 2006 Rich Felker <dalias@aerifal.cx>
  3. *
  4. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  5. */
  6. #include <stdlib.h>
  7. #include <string.h>
  8. libc_hidden_proto(strchr)
  9. libc_hidden_proto(strlen)
  10. libc_hidden_proto(strncmp)
  11. int getsubopt(char **opt, char *const *keys, char **val)
  12. {
  13. char *s = *opt;
  14. int i;
  15. *val = NULL;
  16. *opt = strchr(s, ',');
  17. if (*opt) *(*opt)++ = 0;
  18. else *opt = s + strlen(s);
  19. for (i=0; keys[i]; i++) {
  20. size_t l = strlen(keys[i]);
  21. if (strncmp(keys[i], s, l)) continue;
  22. if (s[l] == '=')
  23. *val = s + l;
  24. else if (s[l]) continue;
  25. return i;
  26. }
  27. return -1;
  28. }