argz-extract.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* -*-comment-start: "//";comment-end:""-*-
  2. * GNU Mes --- Maxwell Equations of Software
  3. * Copyright © 1995-2018 Free Software Foundation, Inc.
  4. * Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
  5. *
  6. * This file is part of GNU Mes.
  7. *
  8. * GNU Mes is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * GNU Mes is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /** Commentary:
  22. Taken from GNU C Library
  23. Routines for dealing with '\0' separated arg vectors.
  24. Written by Miles Bader <miles@gnu.org>
  25. */
  26. #include <argz.h>
  27. /* Puts pointers to each string in ARGZ, plus a terminating 0 element, into
  28. ARGV, which must be large enough to hold them all. */
  29. void
  30. __argz_extract (char const *argz, size_t len, char **argv)
  31. {
  32. __argz_extract_count (argz, len, argv);
  33. }
  34. size_t
  35. __argz_extract_count (char const *argz, size_t len, char **argv)
  36. {
  37. size_t count = 0;
  38. while (len > 0)
  39. {
  40. size_t part_len = strlen (argz);
  41. *argv++ = (char *) argz;
  42. argz += part_len + 1;
  43. len -= part_len + 1;
  44. count ++;
  45. }
  46. *argv = 0;
  47. return count;
  48. }