Blame string/argz-replace.c

Packit 6c4009
/* String replacement in an argz vector
Packit 6c4009
   Copyright (C) 1997-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Written by Miles Bader <miles@gnu.ai.mit.edu>
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <argz.h>
Packit 6c4009
Packit 6c4009
/* Append BUF, of length BUF_LEN to *TO, of length *TO_LEN, reallocating and
Packit 6c4009
   updating *TO & *TO_LEN appropriately.  If an allocation error occurs,
Packit 6c4009
   *TO's old value is freed, and *TO is set to 0.  */
Packit 6c4009
static void
Packit 6c4009
str_append (char **to, size_t *to_len, const char *buf, const size_t buf_len)
Packit 6c4009
{
Packit 6c4009
  size_t new_len = *to_len + buf_len;
Packit 6c4009
  char *new_to = realloc (*to, new_len + 1);
Packit 6c4009
Packit 6c4009
  if (new_to)
Packit 6c4009
    {
Packit 6c4009
      *((char *) __mempcpy (new_to + *to_len, buf, buf_len)) = '\0';
Packit 6c4009
      *to = new_to;
Packit 6c4009
      *to_len = new_len;
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      free (*to);
Packit 6c4009
      *to = 0;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Replace any occurrences of the string STR in ARGZ with WITH, reallocating
Packit 6c4009
   ARGZ as necessary.  If REPLACE_COUNT is non-zero, *REPLACE_COUNT will be
Packit 6c4009
   incremented by number of replacements performed.  */
Packit 6c4009
error_t
Packit 6c4009
__argz_replace (char **argz, size_t *argz_len, const char *str, const char *with,
Packit 6c4009
		unsigned *replace_count)
Packit 6c4009
{
Packit 6c4009
  error_t err = 0;
Packit 6c4009
Packit 6c4009
  if (str && *str)
Packit 6c4009
    {
Packit 6c4009
      char *arg = 0;
Packit 6c4009
      char *src = *argz;
Packit 6c4009
      size_t src_len = *argz_len;
Packit 6c4009
      char *dst = 0;
Packit 6c4009
      size_t dst_len = 0;
Packit 6c4009
      int delayed_copy = 1;	/* True while we've avoided copying anything.  */
Packit 6c4009
      size_t str_len = strlen (str), with_len = strlen (with);
Packit 6c4009
Packit 6c4009
      while (!err && (arg = argz_next (src, src_len, arg)))
Packit 6c4009
	{
Packit 6c4009
	  char *match = strstr (arg, str);
Packit 6c4009
	  if (match)
Packit 6c4009
	    {
Packit 6c4009
	      char *from = match + str_len;
Packit 6c4009
	      size_t to_len = match - arg;
Packit 6c4009
	      char *to = __strndup (arg, to_len);
Packit 6c4009
Packit 6c4009
	      while (to && from)
Packit 6c4009
		{
Packit 6c4009
		  str_append (&to, &to_len, with, with_len);
Packit 6c4009
		  if (to)
Packit 6c4009
		    {
Packit 6c4009
		      match = strstr (from, str);
Packit 6c4009
		      if (match)
Packit 6c4009
			{
Packit 6c4009
			  str_append (&to, &to_len, from, match - from);
Packit 6c4009
			  from = match + str_len;
Packit 6c4009
			}
Packit 6c4009
		      else
Packit 6c4009
			{
Packit 6c4009
			  str_append (&to, &to_len, from, strlen (from));
Packit 6c4009
			  from = 0;
Packit 6c4009
			}
Packit 6c4009
		    }
Packit 6c4009
		}
Packit 6c4009
Packit 6c4009
	      if (to)
Packit 6c4009
		{
Packit 6c4009
		  if (delayed_copy)
Packit 6c4009
		    /* We avoided copying SRC to DST until we found a match;
Packit 6c4009
                       now that we've done so, copy everything from the start
Packit 6c4009
                       of SRC.  */
Packit 6c4009
		    {
Packit 6c4009
		      if (arg > src)
Packit 6c4009
			err = __argz_append (&dst, &dst_len, src, (arg - src));
Packit 6c4009
		      delayed_copy = 0;
Packit 6c4009
		    }
Packit 6c4009
		  if (! err)
Packit 6c4009
		    err = __argz_add (&dst, &dst_len, to);
Packit 6c4009
		  free (to);
Packit 6c4009
		}
Packit 6c4009
	      else
Packit 6c4009
		err = ENOMEM;
Packit 6c4009
Packit 6c4009
	      if (replace_count)
Packit 6c4009
		(*replace_count)++;
Packit 6c4009
	    }
Packit 6c4009
	  else if (! delayed_copy)
Packit 6c4009
	    err = __argz_add (&dst, &dst_len, arg);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (! err)
Packit 6c4009
	{
Packit 6c4009
	  if (! delayed_copy)
Packit 6c4009
	    /* We never found any instances of str.  */
Packit 6c4009
	    {
Packit 6c4009
	      free (src);
Packit 6c4009
	      *argz = dst;
Packit 6c4009
	      *argz_len = dst_len;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else if (dst_len > 0)
Packit 6c4009
	free (dst);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return err;
Packit 6c4009
}
Packit 6c4009
weak_alias (__argz_replace, argz_replace)