]>
Commit | Line | Data |
---|---|---|
392d7920 | 1 | /* Routines for dealing with '\0' separated arg vectors. |
392d7920 | 2 | Copyright (C) 1995, 1996 Free Software Foundation, Inc. |
392d7920 RM |
3 | Written by Miles Bader <miles@gnu.ai.mit.edu> |
4 | ||
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. | |
9 | ||
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. | |
14 | ||
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. */ | |
18 | ||
19 | #ifndef __ARGZ_H__ | |
7a12c6bb | 20 | |
23396375 | 21 | #define __ARGZ_H__ 1 |
7a12c6bb RM |
22 | #include <features.h> |
23 | ||
392d7920 RM |
24 | #include <errno.h> /* Define error_t. */ |
25 | #include <string.h> /* Need size_t, and strchr is called below. */ | |
26 | ||
7a12c6bb RM |
27 | |
28 | __BEGIN_DECLS | |
29 | ||
392d7920 RM |
30 | /* Make a '\0' separated arg vector from a unix argv vector, returning it in |
31 | ARGZ, and the total length in LEN. If a memory allocation error occurs, | |
32 | ENOMEM is returned, otherwise 0. The result can be destroyed using free. */ | |
23396375 UD |
33 | extern error_t __argz_create __P ((char *const __argv[], char **__argz, |
34 | size_t *__len)); | |
35 | extern error_t argz_create __P ((char *const __argv[], char **__argz, | |
36 | size_t *__len)); | |
7a12c6bb RM |
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. */ | |
23396375 UD |
42 | extern error_t __argz_create_sep __P ((__const char *__string, int __sep, |
43 | char **__argz, size_t *__len)); | |
44 | extern error_t argz_create_sep __P ((__const char *__string, int __sep, | |
45 | char **__argz, size_t *__len)); | |
392d7920 RM |
46 | |
47 | /* Returns the number of strings in ARGZ. */ | |
23396375 UD |
48 | extern size_t __argz_count __P ((__const char *__argz, size_t __len)); |
49 | extern size_t argz_count __P ((__const char *__argz, size_t __len)); | |
392d7920 RM |
50 | |
51 | /* Puts pointers to each string in ARGZ into ARGV, which must be large enough | |
52 | to hold them all. */ | |
23396375 UD |
53 | extern void __argz_extract __P ((char *__argz, size_t __len, char **__argv)); |
54 | extern void argz_extract __P ((char *__argz, size_t __len, char **__argv)); | |
392d7920 RM |
55 | |
56 | /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's | |
57 | except the last into the character SEP. */ | |
23396375 UD |
58 | extern void __argz_stringify __P ((char *__argz, size_t __len, int __sep)); |
59 | extern void argz_stringify __P ((char *__argz, size_t __len, int __sep)); | |
392d7920 RM |
60 | |
61 | /* Append BUF, of length BUF_LEN to the argz vector in ARGZ & ARGZ_LEN. */ | |
23396375 UD |
62 | extern error_t __argz_append __P ((char **__argz, size_t *__argz_len, |
63 | __const char *__buf, size_t _buf_len)); | |
64 | extern error_t argz_append __P ((char **__argz, size_t *__argz_len, | |
65 | __const char *__buf, size_t __buf_len)); | |
392d7920 RM |
66 | |
67 | /* Append STR to the argz vector in ARGZ & ARGZ_LEN. */ | |
23396375 UD |
68 | extern error_t __argz_add __P ((char **__argz, size_t *__argz_len, |
69 | __const char *__str)); | |
70 | extern error_t argz_add __P ((char **__argz, size_t *__argz_len, | |
71 | __const char *__str)); | |
392d7920 RM |
72 | |
73 | /* Delete ENTRY from ARGZ & ARGZ_LEN, if it appears there. */ | |
23396375 UD |
74 | extern void __argz_delete __P ((char **__argz, size_t *__argz_len, |
75 | char *__entry)); | |
76 | extern void argz_delete __P ((char **__argz, size_t *__argz_len, | |
77 | char *__entry)); | |
392d7920 RM |
78 | |
79 | /* Insert ENTRY into ARGZ & ARGZ_LEN before BEFORE, which should be an | |
80 | existing entry in ARGZ; if BEFORE is NULL, ENTRY is appended to the end. | |
81 | Since ARGZ's first entry is the same as ARGZ, argz_insert (ARGZ, ARGZ_LEN, | |
82 | ARGZ, ENTRY) will insert ENTRY at the beginning of ARGZ. If BEFORE is not | |
83 | in ARGZ, EINVAL is returned, else if memory can't be allocated for the new | |
84 | ARGZ, ENOMEM is returned, else 0. */ | |
23396375 UD |
85 | extern error_t __argz_insert __P ((char **__argz, size_t *__argz_len, |
86 | char *__before, __const char *__entry)); | |
87 | extern error_t argz_insert __P ((char **__argz, size_t *__argz_len, | |
88 | char *__before, __const char *__entry)); | |
392d7920 RM |
89 | \f |
90 | /* Returns the next entry in ARGZ & ARGZ_LEN after ENTRY, or NULL if there | |
91 | are no more. If entry is NULL, then the first entry is returned. This | |
92 | behavior allows two convenient iteration styles: | |
93 | ||
94 | char *entry = 0; | |
7a12c6bb | 95 | while ((entry = argz_next (argz, argz_len, entry))) |
392d7920 RM |
96 | ...; |
97 | ||
98 | or | |
99 | ||
100 | char *entry; | |
101 | for (entry = argz; entry; entry = argz_next (argz, argz_len, entry)) | |
102 | ...; | |
103 | */ | |
8f0c527e RM |
104 | extern char *__argz_next __P ((char *argz, size_t __argz_len, |
105 | __const char *entry)); | |
106 | extern char *argz_next __P ((char *argz, size_t __argz_len, | |
107 | __const char *entry)); | |
7a12c6bb RM |
108 | |
109 | #if defined (__OPTIMIZE__) && __GNUC__ >= 2 | |
392d7920 | 110 | extern inline char * |
23396375 | 111 | __argz_next (char *__argz, size_t __argz_len, const char *__entry) |
392d7920 | 112 | { |
23396375 | 113 | if (__entry) |
7a12c6bb | 114 | { |
23396375 UD |
115 | if (__entry < __argz + __argz_len) |
116 | __entry = strchr (__entry, '\0') + 1; | |
7a12c6bb | 117 | |
23396375 | 118 | return __entry >= __argz + __argz_len ? NULL : (char *) __entry; |
7a12c6bb | 119 | } |
392d7920 | 120 | else |
23396375 UD |
121 | if (__argz_len > 0) |
122 | return __argz; | |
392d7920 RM |
123 | else |
124 | return 0; | |
125 | } | |
7a12c6bb | 126 | extern inline char * |
23396375 | 127 | argz_next (char *__argz, size_t __argz_len, const char *__entry) |
7a12c6bb | 128 | { |
23396375 | 129 | return __argz_next (__argz, __argz_len, __entry); |
7a12c6bb RM |
130 | } |
131 | #endif /* optimizing GCC2 */ | |
392d7920 RM |
132 | |
133 | #endif /* __ARGZ_H__ */ |