static void lstopo_argz_init (char *cmd, char **argzp, size_t *argz_lenp, char *extra_args[]) { char *extra; size_t extra_len; int e; char *argv[] = { cmd, "-i", "-", "--if", "xml", "--of", "console", NULL }; if ( (e = argz_create (argv, argzp, argz_lenp)) != 0 || (e = argz_create (extra_args, &extra, &extra_len)) != 0) log_msg_exit ("argz_create: %s", strerror (e)); /* Append any extra args in av[] */ if ((e = argz_append (argzp, argz_lenp, extra, extra_len)) != 0) log_msg_exit ("argz_append: %s", strerror (e)); free (extra); }
error_t argz_insert (char **pargz, size_t *pargz_len, char *before, const char *entry) { assert (pargz); assert (pargz_len); assert (entry && *entry); /* No BEFORE address indicates ENTRY should be inserted after the current last element. */ if (!before) return argz_append (pargz, pargz_len, entry, 1+ strlen (entry)); /* This probably indicates a programmer error, but to preserve semantics, scan back to the start of an entry if BEFORE points into the middle of it. */ while ((before > *pargz) && (before[-1] != EOS_CHAR)) --before; { size_t entry_len = 1+ strlen (entry); size_t argz_len = *pargz_len + entry_len; size_t offset = before - *pargz; char *argz = (char *) realloc (*pargz, argz_len); if (!argz) return ENOMEM; /* Make BEFORE point to the equivalent offset in ARGZ that it used to have in *PARGZ incase realloc() moved the block. */ before = argz + offset; /* Move the ARGZ entries starting at BEFORE up into the new space at the end -- making room to copy ENTRY into the resulting gap. */ memmove (before + entry_len, before, *pargz_len - offset); memcpy (before, entry, entry_len); /* Assign new values. */ *pargz = argz; *pargz_len = argz_len; } return 0; }
/* Add STR to the argz vector in ARGZ & ARGZ_LEN. This should be moved into argz.c in libshouldbelibc. */ error_t argz_add (char **argz, size_t *argz_len, const char *str) { return argz_append (argz, argz_len, str, strlen (str) + 1); }
/* Replace any occurrences of the string STR in ARGZ with WITH, reallocating ARGZ as necessary. If REPLACE_COUNT is non-zero, *REPLACE_COUNT will be incremented by number of replacements performed. */ error_t argz_replace (char **argz, size_t *argz_len, const char *str, const char *with, unsigned *replace_count) { error_t err = 0; if (str && *str) { char *arg = 0; char *src = *argz; size_t src_len = *argz_len; char *dst = 0; size_t dst_len = 0; int delayed_copy = 1; /* True while we've avoided copying anything. */ size_t str_len = strlen (str), with_len = strlen (with); while (!err && (arg = argz_next (src, src_len, arg))) { char *match = strstr (arg, str); if (match) { char *from = match + str_len; size_t to_len = match - arg; char *to = strndup (arg, to_len); while (to && from) { str_append (&to, &to_len, with, with_len); if (to) { match = strstr (from, str); if (match) { str_append (&to, &to_len, from, match - from); from = match + str_len; } else { str_append (&to, &to_len, from, strlen (from)); from = 0; } } } if (to) { if (delayed_copy) /* We avoided copying SRC to DST until we found a match; now that we've done so, copy everything from the start of SRC. */ { if (arg > src) err = argz_append (&dst, &dst_len, src, (arg - src)); delayed_copy = 0; } if (! err) err = argz_add (&dst, &dst_len, to); free (to); } else err = ENOMEM; if (replace_count) (*replace_count)++; } else if (! delayed_copy) err = argz_add (&dst, &dst_len, arg); } if (! err) { if (! delayed_copy) /* We never found any instances of str. */ { free (src); *argz = dst; *argz_len = dst_len; } } else if (dst_len > 0) free (dst); } return err; }