argz-create.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /* Routines for dealing with '\0' separated arg vectors.
  2. Copyright (C) 1995-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <argz.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. /* Make a '\0' separated arg vector from a unix argv vector, returning it in
  19. ARGZ, and the total length in LEN. If a memory allocation error occurs,
  20. ENOMEM is returned, otherwise 0. */
  21. error_t
  22. __argz_create (char *const argv[], char **argz, size_t *len)
  23. {
  24. int argc;
  25. size_t tlen = 0;
  26. char *const *ap;
  27. char *p;
  28. for (argc = 0; argv[argc] != NULL; ++argc)
  29. tlen += strlen (argv[argc]) + 1;
  30. if (tlen == 0)
  31. *argz = NULL;
  32. else
  33. {
  34. *argz = malloc (tlen);
  35. if (*argz == NULL)
  36. return ENOMEM;
  37. for (p = *argz, ap = argv; *ap; ++ap, ++p)
  38. p = __stpcpy (p, *ap);
  39. }
  40. *len = tlen;
  41. return 0;
  42. }
  43. weak_alias (__argz_create, argz_create)