]>
Commit | Line | Data |
---|---|---|
392d7920 | 1 | /* Routines for dealing with '\0' separated arg vectors. |
688903eb | 2 | Copyright (C) 1995-2018 Free Software Foundation, Inc. |
c84142e8 | 3 | This file is part of the GNU C Library. |
392d7920 RM |
4 | Written by Miles Bader <miles@gnu.ai.mit.edu> |
5 | ||
c84142e8 | 6 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
392d7920 | 10 | |
c84142e8 UD |
11 | The GNU C Library is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
392d7920 | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
41bdb6e2 | 14 | Lesser General Public License for more details. |
392d7920 | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
17 | License along with the GNU C Library; if not, see |
18 | <http://www.gnu.org/licenses/>. */ | |
392d7920 RM |
19 | |
20 | #include <argz.h> | |
21 | #include <string.h> | |
22 | #include <stdlib.h> | |
23 | ||
24 | /* Insert ENTRY into ARGZ & ARGZ_LEN before BEFORE, which should be an | |
25 | existing entry in ARGZ; if BEFORE is NULL, ENTRY is appended to the end. | |
26 | Since ARGZ's first entry is the same as ARGZ, argz_insert (ARGZ, ARGZ_LEN, | |
27 | ARGZ, ENTRY) will insert ENTRY at the beginning of ARGZ. If BEFORE is not | |
28 | in ARGZ, EINVAL is returned, else if memory can't be allocated for the new | |
29 | ARGZ, ENOMEM is returned, else 0. */ | |
30 | error_t | |
31 | __argz_insert (char **argz, size_t *argz_len, char *before, const char *entry) | |
32 | { | |
33 | if (! before) | |
34 | return __argz_add (argz, argz_len, entry); | |
35 | ||
36 | if (before < *argz || before >= *argz + *argz_len) | |
37 | return EINVAL; | |
38 | ||
39 | if (before > *argz) | |
40 | /* Make sure before is actually the beginning of an entry. */ | |
41 | while (before[-1]) | |
42 | before--; | |
43 | ||
44 | { | |
45 | size_t after_before = *argz_len - (before - *argz); | |
46 | size_t entry_len = strlen (entry) + 1; | |
47 | size_t new_argz_len = *argz_len + entry_len; | |
48 | char *new_argz = realloc (*argz, new_argz_len); | |
49 | ||
50 | if (new_argz) | |
51 | { | |
52 | before = new_argz + (before - *argz); | |
18e8dd4d TBB |
53 | memmove (before + entry_len, before, after_before); |
54 | memmove (before, entry, entry_len); | |
392d7920 RM |
55 | *argz = new_argz; |
56 | *argz_len = new_argz_len; | |
57 | return 0; | |
58 | } | |
59 | else | |
60 | return ENOMEM; | |
61 | } | |
62 | } | |
63 | weak_alias (__argz_insert, argz_insert) |