1/* Routines for dealing with '\0' separated arg vectors.
2 Copyright (C) 1995-2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19#ifndef _ARGZ_H
20#define _ARGZ_H 1
21
22#include <features.h>
23
24#define __need_error_t
25#include <errno.h>
26#include <string.h> /* Need size_t, and strchr is called below. */
27
28#ifndef __error_t_defined
29typedef int error_t;
30#endif
31
32
33__BEGIN_DECLS
34
35/* Make a '\0' separated arg vector from a unix argv vector, returning it in
36 ARGZ, and the total length in LEN. If a memory allocation error occurs,
37 ENOMEM is returned, otherwise 0. The result can be destroyed using free. */
38extern error_t __argz_create (char *const __argv[], char **__restrict __argz,
39 size_t *__restrict __len) __THROW;
40extern error_t argz_create (char *const __argv[], char **__restrict __argz,
41 size_t *__restrict __len) __THROW;
42
43/* Make a '\0' separated arg vector from a SEP separated list in
44 STRING, returning it in ARGZ, and the total length in LEN. If a
45 memory allocation error occurs, ENOMEM is returned, otherwise 0.
46 The result can be destroyed using free. */
47extern error_t __argz_create_sep (const char *__restrict __string,
48 int __sep, char **__restrict __argz,
49 size_t *__restrict __len) __THROW;
50extern error_t argz_create_sep (const char *__restrict __string,
51 int __sep, char **__restrict __argz,
52 size_t *__restrict __len) __THROW;
53
54/* Returns the number of strings in ARGZ. */
55extern size_t __argz_count (const char *__argz, size_t __len)
56 __THROW __attribute_pure__;
57extern size_t argz_count (const char *__argz, size_t __len)
58 __THROW __attribute_pure__;
59
60/* Puts pointers to each string in ARGZ into ARGV, which must be large enough
61 to hold them all. */
62extern void __argz_extract (const char *__restrict __argz, size_t __len,
63 char **__restrict __argv) __THROW;
64extern void argz_extract (const char *__restrict __argz, size_t __len,
65 char **__restrict __argv) __THROW;
66
67/* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
68 except the last into the character SEP. */
69extern void __argz_stringify (char *__argz, size_t __len, int __sep) __THROW;
70extern void argz_stringify (char *__argz, size_t __len, int __sep) __THROW;
71
72/* Append BUF, of length BUF_LEN to the argz vector in ARGZ & ARGZ_LEN. */
73extern error_t __argz_append (char **__restrict __argz,
74 size_t *__restrict __argz_len,
75 const char *__restrict __buf, size_t __buf_len)
76 __THROW;
77extern error_t argz_append (char **__restrict __argz,
78 size_t *__restrict __argz_len,
79 const char *__restrict __buf, size_t __buf_len)
80 __THROW;
81
82/* Append STR to the argz vector in ARGZ & ARGZ_LEN. */
83extern error_t __argz_add (char **__restrict __argz,
84 size_t *__restrict __argz_len,
85 const char *__restrict __str) __THROW;
86extern error_t argz_add (char **__restrict __argz,
87 size_t *__restrict __argz_len,
88 const char *__restrict __str) __THROW;
89
90/* Append SEP separated list in STRING to the argz vector in ARGZ &
91 ARGZ_LEN. */
92extern error_t __argz_add_sep (char **__restrict __argz,
93 size_t *__restrict __argz_len,
94 const char *__restrict __string, int __delim)
95 __THROW;
96extern error_t argz_add_sep (char **__restrict __argz,
97 size_t *__restrict __argz_len,
98 const char *__restrict __string, int __delim)
99 __THROW;
100
101/* Delete ENTRY from ARGZ & ARGZ_LEN, if it appears there. */
102extern void __argz_delete (char **__restrict __argz,
103 size_t *__restrict __argz_len,
104 char *__restrict __entry) __THROW;
105extern void argz_delete (char **__restrict __argz,
106 size_t *__restrict __argz_len,
107 char *__restrict __entry) __THROW;
108
109/* Insert ENTRY into ARGZ & ARGZ_LEN before BEFORE, which should be an
110 existing entry in ARGZ; if BEFORE is NULL, ENTRY is appended to the end.
111 Since ARGZ's first entry is the same as ARGZ, argz_insert (ARGZ, ARGZ_LEN,
112 ARGZ, ENTRY) will insert ENTRY at the beginning of ARGZ. If BEFORE is not
113 in ARGZ, EINVAL is returned, else if memory can't be allocated for the new
114 ARGZ, ENOMEM is returned, else 0. */
115extern error_t __argz_insert (char **__restrict __argz,
116 size_t *__restrict __argz_len,
117 char *__restrict __before,
118 const char *__restrict __entry) __THROW;
119extern error_t argz_insert (char **__restrict __argz,
120 size_t *__restrict __argz_len,
121 char *__restrict __before,
122 const char *__restrict __entry) __THROW;
123
124/* Replace any occurrences of the string STR in ARGZ with WITH, reallocating
125 ARGZ as necessary. If REPLACE_COUNT is non-zero, *REPLACE_COUNT will be
126 incremented by number of replacements performed. */
127extern error_t __argz_replace (char **__restrict __argz,
128 size_t *__restrict __argz_len,
129 const char *__restrict __str,
130 const char *__restrict __with,
131 unsigned int *__restrict __replace_count);
132extern error_t argz_replace (char **__restrict __argz,
133 size_t *__restrict __argz_len,
134 const char *__restrict __str,
135 const char *__restrict __with,
136 unsigned int *__restrict __replace_count);
137
138/* Returns the next entry in ARGZ & ARGZ_LEN after ENTRY, or NULL if there
139 are no more. If entry is NULL, then the first entry is returned. This
140 behavior allows two convenient iteration styles:
141
142 char *entry = 0;
143 while ((entry = argz_next (argz, argz_len, entry)))
144 ...;
145
146 or
147
148 char *entry;
149 for (entry = argz; entry; entry = argz_next (argz, argz_len, entry))
150 ...;
151*/
152extern char *__argz_next (const char *__restrict __argz, size_t __argz_len,
153 const char *__restrict __entry) __THROW;
154extern char *argz_next (const char *__restrict __argz, size_t __argz_len,
155 const char *__restrict __entry) __THROW;
156
157#ifdef __USE_EXTERN_INLINES
158__extern_inline char *
159__NTH (__argz_next (const char *__argz, size_t __argz_len,
160 const char *__entry))
161{
162 if (__entry)
163 {
164 if (__entry < __argz + __argz_len)
165 __entry = strchr (__entry, '\0') + 1;
166
167 return __entry >= __argz + __argz_len ? (char *) NULL : (char *) __entry;
168 }
169 else
170 return __argz_len > 0 ? (char *) __argz : 0;
171}
172__extern_inline char *
173__NTH (argz_next (const char *__argz, size_t __argz_len,
174 const char *__entry))
175{
176 return __argz_next (__argz, __argz_len, __entry);
177}
178#endif /* Use extern inlines. */
179
180__END_DECLS
181
182#endif /* argz.h */
183