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