getsubopt-susv3.c 549 B

123456789101112131415161718192021222324252627282930
  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. int getsubopt(char **opt, char *const *keys, char **val)
  9. {
  10. char *s = *opt;
  11. int i;
  12. *val = NULL;
  13. *opt = strchr(s, ',');
  14. if (*opt) *(*opt)++ = 0;
  15. else *opt = s + strlen(s);
  16. for (i=0; keys[i]; i++) {
  17. size_t l = strlen(keys[i]);
  18. if (strncmp(keys[i], s, l)) continue;
  19. if (s[l] == '=')
  20. *val = s + l;
  21. else if (s[l]) continue;
  22. return i;
  23. }
  24. return -1;
  25. }