]> git.ipfire.org Git - thirdparty/glibc.git/blob - string/argz.h
Mon Apr 29 00:11:59 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 #include <errno.h> /* Define error_t. */
24 #include <string.h> /* Need size_t, and strchr is called below. */
25
26 /* Make a '\0' separated arg vector from a unix argv vector, returning it in
27 ARGZ, and the total length in LEN. If a memory allocation error occurs,
28 ENOMEM is returned, otherwise 0. The result can be destroyed using free. */
29 error_t __argz_create (char **argv, char **argz, size_t *len);
30 error_t argz_create (char **argv, char **argz, size_t *len);
31
32 /* Returns the number of strings in ARGZ. */
33 size_t __argz_count (const char *argz, size_t len);
34 size_t argz_count (const char *argz, size_t len);
35
36 /* Puts pointers to each string in ARGZ into ARGV, which must be large enough
37 to hold them all. */
38 void __argz_extract (const char *argz, size_t len, char **argv);
39 void argz_extract (const char *argz, size_t len, char **argv);
40
41 /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
42 except the last into the character SEP. */
43 void __argz_stringify (char *argz, size_t len, int sep);
44 void argz_stringify (char *argz, size_t len, int sep);
45
46 /* Append BUF, of length BUF_LEN to the argz vector in ARGZ & ARGZ_LEN. */
47 error_t __argz_append (char **argz, size_t *argz_len,
48 const char *buf, size_t buf_len);
49 error_t argz_append (char **argz, size_t *argz_len,
50 const char *buf, size_t buf_len);
51
52 /* Append STR to the argz vector in ARGZ & ARGZ_LEN. */
53 error_t __argz_add (char **argz, size_t *argz_len, const char *str);
54 error_t argz_add (char **argz, size_t *argz_len, const char *str);
55
56 /* Delete ENTRY from ARGZ & ARGZ_LEN, if it appears there. */
57 void __argz_delete (char **argz, size_t *argz_len, char *entry);
58 void argz_delete (char **argz, size_t *argz_len, char *entry);
59
60 /* Insert ENTRY into ARGZ & ARGZ_LEN before BEFORE, which should be an
61 existing entry in ARGZ; if BEFORE is NULL, ENTRY is appended to the end.
62 Since ARGZ's first entry is the same as ARGZ, argz_insert (ARGZ, ARGZ_LEN,
63 ARGZ, ENTRY) will insert ENTRY at the beginning of ARGZ. If BEFORE is not
64 in ARGZ, EINVAL is returned, else if memory can't be allocated for the new
65 ARGZ, ENOMEM is returned, else 0. */
66 error_t __argz_insert (char **argz, size_t *argz_len,
67 char *before, const char *entry);
68 error_t argz_insert (char **argz, size_t *argz_len,
69 char *before, const char *entry);
70 \f
71 /* Returns the next entry in ARGZ & ARGZ_LEN after ENTRY, or NULL if there
72 are no more. If entry is NULL, then the first entry is returned. This
73 behavior allows two convenient iteration styles:
74
75 char *entry = 0;
76 while (entry = argz_next (argz, argz_len, entry))
77 ...;
78
79 or
80
81 char *entry;
82 for (entry = argz; entry; entry = argz_next (argz, argz_len, entry))
83 ...;
84 */
85 extern inline char *
86 argz_next (char *argz, size_t argz_len, const char *entry)
87 {
88 if (entry)
89 if (entry >= argz + argz_len)
90 return 0;
91 else
92 return strchr (entry, '\0') + 1;
93 else
94 if (argz_len > 0)
95 return argz;
96 else
97 return 0;
98 }
99
100 #endif /* __ARGZ_H__ */