/*
 * tools/lib/pv_read_already_red.c
 *
 * Copyright (C) 1997 - 2000  Heinz Mauelshagen, Germany
 *
 * March-May,October-November 1997
 * May,August,November 1998
 * September 1999
 *
 *
 * This LVM library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This LVM library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this LVM library; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA
 *
 */

/*
 * Changelog
 *
 *    29/10/1999 - fixed possible free() bug
 *
 */

#include <liblvm.h>


int pv_read_already_red ( char *pv_name) {
   int i = 0;
   static int pv_count = 0;
   static char* pv_name_cache = NULL;
   char* pv_name_cache_sav = NULL;


   if ( pv_name == 0 || strlen ( pv_name) > NAME_LEN - 1) return FALSE;

   for ( i = 0; i < pv_count; i++)
      if ( strcmp ( pv_name, &pv_name_cache[i*NAME_LEN]) == 0) return TRUE;

   pv_name_cache_sav = pv_name_cache;
   if ( ( pv_name_cache = realloc ( pv_name_cache,
                                    ( pv_count + 1) * NAME_LEN)) == NULL) {
      fprintf ( stderr, "realloc error in %s [line %d]\n",
                        __FILE__, __LINE__);
      if ( pv_name_cache_sav != NULL) free ( pv_name_cache_sav);
      pv_name_cache = NULL;
      return FALSE;
   }

   memset ( &pv_name_cache[pv_count * NAME_LEN], 0, NAME_LEN);
   strcpy ( &pv_name_cache[pv_count * NAME_LEN], pv_name);
   pv_count++;

   return FALSE;
}
