Back to index

glibc  2.9
argz-replace.c
Go to the documentation of this file.
00001 /* String replacement in an argz vector
00002    Copyright (C) 1997, 1998, 2006 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Written by Miles Bader <miles@gnu.ai.mit.edu>
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <stdlib.h>
00022 #include <string.h>
00023 #include <argz.h>
00024 
00025 /* Append BUF, of length BUF_LEN to *TO, of length *TO_LEN, reallocating and
00026    updating *TO & *TO_LEN appropriately.  If an allocation error occurs,
00027    *TO's old value is freed, and *TO is set to 0.  */
00028 static void
00029 str_append (char **to, size_t *to_len, const char *buf, const size_t buf_len)
00030 {
00031   size_t new_len = *to_len + buf_len;
00032   char *new_to = realloc (*to, new_len + 1);
00033 
00034   if (new_to)
00035     {
00036       *((char *) __mempcpy (new_to + *to_len, buf, buf_len)) = '\0';
00037       *to = new_to;
00038       *to_len = new_len;
00039     }
00040   else
00041     {
00042       free (*to);
00043       *to = 0;
00044     }
00045 }
00046 
00047 /* Replace any occurrences of the string STR in ARGZ with WITH, reallocating
00048    ARGZ as necessary.  If REPLACE_COUNT is non-zero, *REPLACE_COUNT will be
00049    incremented by number of replacements performed.  */
00050 error_t
00051 __argz_replace (char **argz, size_t *argz_len, const char *str, const char *with,
00052               unsigned *replace_count)
00053 {
00054   error_t err = 0;
00055 
00056   if (str && *str)
00057     {
00058       char *arg = 0;
00059       char *src = *argz;
00060       size_t src_len = *argz_len;
00061       char *dst = 0;
00062       size_t dst_len = 0;
00063       int delayed_copy = 1; /* True while we've avoided copying anything.  */
00064       size_t str_len = strlen (str), with_len = strlen (with);
00065 
00066       while (!err && (arg = argz_next (src, src_len, arg)))
00067        {
00068          char *match = strstr (arg, str);
00069          if (match)
00070            {
00071              char *from = match + str_len;
00072              size_t to_len = match - arg;
00073              char *to = __strndup (arg, to_len);
00074 
00075              while (to && from)
00076               {
00077                 str_append (&to, &to_len, with, with_len);
00078                 if (to)
00079                   {
00080                     match = strstr (from, str);
00081                     if (match)
00082                      {
00083                        str_append (&to, &to_len, from, match - from);
00084                        from = match + str_len;
00085                      }
00086                     else
00087                      {
00088                        str_append (&to, &to_len, from, strlen (from));
00089                        from = 0;
00090                      }
00091                   }
00092               }
00093 
00094              if (to)
00095               {
00096                 if (delayed_copy)
00097                   /* We avoided copying SRC to DST until we found a match;
00098                        now that we've done so, copy everything from the start
00099                        of SRC.  */
00100                   {
00101                     if (arg > src)
00102                      err = __argz_append (&dst, &dst_len, src, (arg - src));
00103                     delayed_copy = 0;
00104                   }
00105                 if (! err)
00106                   err = __argz_add (&dst, &dst_len, to);
00107                 free (to);
00108               }
00109              else
00110               err = ENOMEM;
00111 
00112              if (replace_count)
00113               (*replace_count)++;
00114            }
00115          else if (! delayed_copy)
00116            err = __argz_add (&dst, &dst_len, arg);
00117        }
00118 
00119       if (! err)
00120        {
00121          if (! delayed_copy)
00122            /* We never found any instances of str.  */
00123            {
00124              free (src);
00125              *argz = dst;
00126              *argz_len = dst_len;
00127            }
00128        }
00129       else if (dst_len > 0)
00130        free (dst);
00131     }
00132 
00133   return err;
00134 }
00135 weak_alias (__argz_replace, argz_replace)