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