1/* Routines for dealing with '\0' separated arg vectors.
2 Copyright (C) 1995-2018 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#include <errno.h>
24#include <string.h> /* Need size_t, and strchr is called below. */
25
26__BEGIN_DECLS
27
28/* error_t may or may not be available from errno.h, depending on the
29 operating system. */
30#ifndef __error_t_defined
31# define __error_t_defined 1
32typedef int error_t;
33#endif
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;
50
51/* Returns the number of strings in ARGZ. */
52extern size_t __argz_count (const char *__argz, size_t __len)
53 __THROW __attribute_pure__;
54extern size_t argz_count (const char *__argz, size_t __len)
55 __THROW __attribute_pure__;
56
57/* Puts pointers to each string in ARGZ into ARGV, which must be large enough
58 to hold them all. */
59extern void __argz_extract (const char *__restrict __argz, size_t __len,
60 char **__restrict __argv) __THROW;
61extern void argz_extract (const char *__restrict __argz, size_t __len,
62 char **__restrict __argv) __THROW;
63
64/* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
65 except the last into the character SEP. */
66extern void __argz_stringify (char *__argz, size_t __len, int __sep) __THROW;
67extern void argz_stringify (char *__argz, size_t __len, int __sep) __THROW;
68
69/* Append BUF, of length BUF_LEN to the argz vector in ARGZ & ARGZ_LEN. */
70extern error_t argz_append (char **__restrict __argz,
71 size_t *__restrict __argz_len,
72 const char *__restrict __buf, size_t __buf_len)
73 __THROW;
74
75/* Append STR to the argz vector in ARGZ & ARGZ_LEN. */
76extern error_t argz_add (char **__restrict __argz,
77 size_t *__restrict __argz_len,
78 const char *__restrict __str) __THROW;
79
80/* Append SEP separated list in STRING to the argz vector in ARGZ &
81 ARGZ_LEN. */
82extern error_t argz_add_sep (char **__restrict __argz,
83 size_t *__restrict __argz_len,
84 const char *__restrict __string, int __delim)
85 __THROW;
86
87/* Delete ENTRY from ARGZ & ARGZ_LEN, if it appears there. */
88extern void argz_delete (char **__restrict __argz,
89 size_t *__restrict __argz_len,
90 char *__restrict __entry) __THROW;
91
92/* Insert ENTRY into ARGZ & ARGZ_LEN before BEFORE, which should be an
93 existing entry in ARGZ; if BEFORE is NULL, ENTRY is appended to the end.
94 Since ARGZ's first entry is the same as ARGZ, argz_insert (ARGZ, ARGZ_LEN,
95 ARGZ, ENTRY) will insert ENTRY at the beginning of ARGZ. If BEFORE is not
96 in ARGZ, EINVAL is returned, else if memory can't be allocated for the new
97 ARGZ, ENOMEM is returned, else 0. */
98extern error_t argz_insert (char **__restrict __argz,
99 size_t *__restrict __argz_len,
100 char *__restrict __before,
101 const char *__restrict __entry) __THROW;
102
103/* Replace any occurrences of the string STR in ARGZ with WITH, reallocating
104 ARGZ as necessary. If REPLACE_COUNT is non-zero, *REPLACE_COUNT will be
105 incremented by number of replacements performed. */
106extern error_t argz_replace (char **__restrict __argz,
107 size_t *__restrict __argz_len,
108 const char *__restrict __str,
109 const char *__restrict __with,
110 unsigned int *__restrict __replace_count);
111
112/* Returns the next entry in ARGZ & ARGZ_LEN after ENTRY, or NULL if there
113 are no more. If entry is NULL, then the first entry is returned. This
114 behavior allows two convenient iteration styles:
115
116 char *entry = 0;
117 while ((entry = argz_next (argz, argz_len, entry)))
118 ...;
119
120 or
121
122 char *entry;
123 for (entry = argz; entry; entry = argz_next (argz, argz_len, entry))
124 ...;
125*/
126extern char *__argz_next (const char *__restrict __argz, size_t __argz_len,
127 const char *__restrict __entry) __THROW;
128extern char *argz_next (const char *__restrict __argz, size_t __argz_len,
129 const char *__restrict __entry) __THROW;
130
131#ifdef __USE_EXTERN_INLINES
132__extern_inline char *
133__NTH (__argz_next (const char *__argz, size_t __argz_len,
134 const char *__entry))
135{
136 if (__entry)
137 {
138 if (__entry < __argz + __argz_len)
139 __entry = strchr (__entry, '\0') + 1;
140
141 return __entry >= __argz + __argz_len ? (char *) NULL : (char *) __entry;
142 }
143 else
144 return __argz_len > 0 ? (char *) __argz : 0;
145}
146__extern_inline char *
147__NTH (argz_next (const char *__argz, size_t __argz_len,
148 const char *__entry))
149{
150 return __argz_next (__argz, __argz_len, __entry);
151}
152#endif /* Use extern inlines. */
153
154__END_DECLS
155
156#endif /* argz.h */
157