argz.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* Routines for dealing with '\0' separated arg vectors.
  2. Copyright (C) 1995-2020 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. #ifndef _ARGZ_H
  16. #define _ARGZ_H 1
  17. #include <features.h>
  18. #include <errno.h>
  19. #include <string.h> /* Need size_t, and strchr is called below. */
  20. __BEGIN_DECLS
  21. /* error_t may or may not be available from errno.h, depending on the
  22. operating system. */
  23. #ifndef __error_t_defined
  24. # define __error_t_defined 1
  25. typedef int error_t;
  26. #endif
  27. /* Make a '\0' separated arg vector from a unix argv vector, returning it in
  28. ARGZ, and the total length in LEN. If a memory allocation error occurs,
  29. ENOMEM is returned, otherwise 0. The result can be destroyed using free. */
  30. extern error_t __argz_create (char *const __argv[], char **__restrict __argz,
  31. size_t *__restrict __len) __THROW;
  32. extern error_t argz_create (char *const __argv[], char **__restrict __argz,
  33. size_t *__restrict __len) __THROW;
  34. /* Make a '\0' separated arg vector from a SEP separated list in
  35. STRING, returning it in ARGZ, and the total length in LEN. If a
  36. memory allocation error occurs, ENOMEM is returned, otherwise 0.
  37. The result can be destroyed using free. */
  38. extern error_t argz_create_sep (const char *__restrict __string,
  39. int __sep, char **__restrict __argz,
  40. size_t *__restrict __len) __THROW;
  41. /* Returns the number of strings in ARGZ. */
  42. extern size_t __argz_count (const char *__argz, size_t __len)
  43. __THROW __attribute_pure__;
  44. extern size_t argz_count (const char *__argz, size_t __len)
  45. __THROW __attribute_pure__;
  46. /* Puts pointers to each string in ARGZ into ARGV, which must be large enough
  47. to hold them all. */
  48. extern void __argz_extract (const char *__restrict __argz, size_t __len,
  49. char **__restrict __argv) __THROW;
  50. extern void argz_extract (const char *__restrict __argz, size_t __len,
  51. char **__restrict __argv) __THROW;
  52. /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
  53. except the last into the character SEP. */
  54. extern void __argz_stringify (char *__argz, size_t __len, int __sep) __THROW;
  55. extern void argz_stringify (char *__argz, size_t __len, int __sep) __THROW;
  56. /* Append BUF, of length BUF_LEN to the argz vector in ARGZ & ARGZ_LEN. */
  57. extern error_t argz_append (char **__restrict __argz,
  58. size_t *__restrict __argz_len,
  59. const char *__restrict __buf, size_t __buf_len)
  60. __THROW;
  61. /* Append STR to the argz vector in ARGZ & ARGZ_LEN. */
  62. extern error_t argz_add (char **__restrict __argz,
  63. size_t *__restrict __argz_len,
  64. const char *__restrict __str) __THROW;
  65. /* Append SEP separated list in STRING to the argz vector in ARGZ &
  66. ARGZ_LEN. */
  67. extern error_t argz_add_sep (char **__restrict __argz,
  68. size_t *__restrict __argz_len,
  69. const char *__restrict __string, int __delim)
  70. __THROW;
  71. /* Delete ENTRY from ARGZ & ARGZ_LEN, if it appears there. */
  72. extern void argz_delete (char **__restrict __argz,
  73. size_t *__restrict __argz_len,
  74. char *__restrict __entry) __THROW;
  75. /* Insert ENTRY into ARGZ & ARGZ_LEN before BEFORE, which should be an
  76. existing entry in ARGZ; if BEFORE is NULL, ENTRY is appended to the end.
  77. Since ARGZ's first entry is the same as ARGZ, argz_insert (ARGZ, ARGZ_LEN,
  78. ARGZ, ENTRY) will insert ENTRY at the beginning of ARGZ. If BEFORE is not
  79. in ARGZ, EINVAL is returned, else if memory can't be allocated for the new
  80. ARGZ, ENOMEM is returned, else 0. */
  81. extern error_t argz_insert (char **__restrict __argz,
  82. size_t *__restrict __argz_len,
  83. char *__restrict __before,
  84. const char *__restrict __entry) __THROW;
  85. /* Replace any occurrences of the string STR in ARGZ with WITH, reallocating
  86. ARGZ as necessary. If REPLACE_COUNT is non-zero, *REPLACE_COUNT will be
  87. incremented by number of replacements performed. */
  88. extern error_t argz_replace (char **__restrict __argz,
  89. size_t *__restrict __argz_len,
  90. const char *__restrict __str,
  91. const char *__restrict __with,
  92. unsigned int *__restrict __replace_count);
  93. /* Returns the next entry in ARGZ & ARGZ_LEN after ENTRY, or NULL if there
  94. are no more. If entry is NULL, then the first entry is returned. This
  95. behavior allows two convenient iteration styles:
  96. char *entry = 0;
  97. while ((entry = argz_next (argz, argz_len, entry)))
  98. ...;
  99. or
  100. char *entry;
  101. for (entry = argz; entry; entry = argz_next (argz, argz_len, entry))
  102. ...;
  103. */
  104. extern char *__argz_next (const char *__restrict __argz, size_t __argz_len,
  105. const char *__restrict __entry) __THROW;
  106. extern char *argz_next (const char *__restrict __argz, size_t __argz_len,
  107. const char *__restrict __entry) __THROW;
  108. #ifdef __USE_EXTERN_INLINES
  109. __extern_inline char *
  110. __NTH (__argz_next (const char *__argz, size_t __argz_len,
  111. const char *__entry))
  112. {
  113. if (__entry)
  114. {
  115. if (__entry < __argz + __argz_len)
  116. __entry = strchr (__entry, '\0') + 1;
  117. return __entry >= __argz + __argz_len ? (char *) NULL : (char *) __entry;
  118. }
  119. else
  120. return __argz_len > 0 ? (char *) __argz : 0;
  121. }
  122. __extern_inline char *
  123. __NTH (argz_next (const char *__argz, size_t __argz_len,
  124. const char *__entry))
  125. {
  126. return __argz_next (__argz, __argz_len, __entry);
  127. }
  128. #endif /* Use extern inlines. */
  129. __END_DECLS
  130. #endif /* argz.h */