Updated.
[glibc/history.git] / string / argz.h
blobb6906d976e5c725f01c3e3db1a1698c822333ab6
1 /* Routines for dealing with '\0' separated arg vectors.
2 Copyright (C) 1995, 1996 Free Software Foundation, Inc.
3 Written by Miles Bader <miles@gnu.ai.mit.edu>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2, or (at
8 your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #ifndef __ARGZ_H__
21 #define __ARGZ_H__ 1
22 #include <features.h>
24 #include <errno.h> /* Define error_t. */
25 #include <string.h> /* Need size_t, and strchr is called below. */
28 __BEGIN_DECLS
30 #ifdef __USE_GNU
32 /* Make a '\0' separated arg vector from a unix argv vector, returning it in
33 ARGZ, and the total length in LEN. If a memory allocation error occurs,
34 ENOMEM is returned, otherwise 0. The result can be destroyed using free. */
35 extern error_t __argz_create __P ((char *__const __argv[], char **__argz,
36 size_t *__len));
37 extern error_t argz_create __P ((char *__const __argv[], char **__argz,
38 size_t *__len));
40 /* Make a '\0' separated arg vector from a SEP separated list in
41 STRING, returning it in ARGZ, and the total length in LEN. If a
42 memory allocation error occurs, ENOMEM is returned, otherwise 0.
43 The result can be destroyed using free. */
44 extern error_t __argz_create_sep __P ((__const char *__string, int __sep,
45 char **__argz, size_t *__len));
46 extern error_t argz_create_sep __P ((__const char *__string, int __sep,
47 char **__argz, size_t *__len));
49 /* Returns the number of strings in ARGZ. */
50 extern size_t __argz_count __P ((__const char *__argz, size_t __len));
51 extern size_t argz_count __P ((__const char *__argz, size_t __len));
53 /* Puts pointers to each string in ARGZ into ARGV, which must be large enough
54 to hold them all. */
55 extern void __argz_extract __P ((char *__argz, size_t __len, char **__argv));
56 extern void argz_extract __P ((char *__argz, size_t __len, char **__argv));
58 /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
59 except the last into the character SEP. */
60 extern void __argz_stringify __P ((char *__argz, size_t __len, int __sep));
61 extern void argz_stringify __P ((char *__argz, size_t __len, int __sep));
63 /* Append BUF, of length BUF_LEN to the argz vector in ARGZ & ARGZ_LEN. */
64 extern error_t __argz_append __P ((char **__argz, size_t *__argz_len,
65 __const char *__buf, size_t _buf_len));
66 extern error_t argz_append __P ((char **__argz, size_t *__argz_len,
67 __const char *__buf, size_t __buf_len));
69 /* Append STR to the argz vector in ARGZ & ARGZ_LEN. */
70 extern error_t __argz_add __P ((char **__argz, size_t *__argz_len,
71 __const char *__str));
72 extern error_t argz_add __P ((char **__argz, size_t *__argz_len,
73 __const char *__str));
75 /* Delete ENTRY from ARGZ & ARGZ_LEN, if it appears there. */
76 extern void __argz_delete __P ((char **__argz, size_t *__argz_len,
77 char *__entry));
78 extern void argz_delete __P ((char **__argz, size_t *__argz_len,
79 char *__entry));
81 /* Insert ENTRY into ARGZ & ARGZ_LEN before BEFORE, which should be an
82 existing entry in ARGZ; if BEFORE is NULL, ENTRY is appended to the end.
83 Since ARGZ's first entry is the same as ARGZ, argz_insert (ARGZ, ARGZ_LEN,
84 ARGZ, ENTRY) will insert ENTRY at the beginning of ARGZ. If BEFORE is not
85 in ARGZ, EINVAL is returned, else if memory can't be allocated for the new
86 ARGZ, ENOMEM is returned, else 0. */
87 extern error_t __argz_insert __P ((char **__argz, size_t *__argz_len,
88 char *__before, __const char *__entry));
89 extern error_t argz_insert __P ((char **__argz, size_t *__argz_len,
90 char *__before, __const char *__entry));
92 /* Returns the next entry in ARGZ & ARGZ_LEN after ENTRY, or NULL if there
93 are no more. If entry is NULL, then the first entry is returned. This
94 behavior allows two convenient iteration styles:
96 char *entry = 0;
97 while ((entry = argz_next (argz, argz_len, entry)))
98 ...;
102 char *entry;
103 for (entry = argz; entry; entry = argz_next (argz, argz_len, entry))
104 ...;
106 extern char *__argz_next __P ((char *argz, size_t __argz_len,
107 __const char *entry));
108 extern char *argz_next __P ((char *argz, size_t __argz_len,
109 __const char *entry));
111 #if defined (__OPTIMIZE__) && __GNUC__ >= 2
112 extern inline char *
113 __argz_next (char *__argz, size_t __argz_len, const char *__entry)
115 if (__entry)
117 if (__entry < __argz + __argz_len)
118 __entry = strchr (__entry, '\0') + 1;
120 return __entry >= __argz + __argz_len ? NULL : (char *) __entry;
122 else
123 if (__argz_len > 0)
124 return __argz;
125 else
126 return 0;
128 extern inline char *
129 argz_next (char *__argz, size_t __argz_len, const char *__entry)
131 return __argz_next (__argz, __argz_len, __entry);
133 #endif /* optimizing GCC2 */
135 #endif /* use GNU */
137 #endif /* __ARGZ_H__ */