-/* @(#) $Id: mem_op.c,v 1.7 2009/06/24 18:53:08 dcid Exp $ */
+/* @(#) $Id: ./src/shared/mem_op.c, 2011/09/08 dcid Exp $
+ */
/* Copyright (C) 2009 Trend Micro Inc.
* All rights reserved.
*
* This program is a free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
- * License (version 3) as published by the FSF - Free Software
+ * License (version 2) as published by the FSF - Free Software
* Foundation
*/
#include "mem_op.h"
+/* Add pointer to array. */
+void **os_AddPtArray(void *pt, void **array)
+{
+ int i = 0;
+ void **ret = NULL;
+
+ if(array)
+ {
+ while(array[i])
+ {
+ i++;
+ }
+ }
+
+ os_realloc(array, (i + 2)*sizeof(char *), ret);
+ ret[i] = pt;
+ ret[i + 1] = NULL;
+
+ return(ret);
+}
+
+
+/* Add a string to an array. */
+char **os_AddStrArray(char *str, char **array)
+{
+ int i = 0;
+ char **ret = NULL;
+ if(array)
+ {
+ while(array[i])
+ {
+ i++;
+ }
+ }
+
+ os_realloc(array, (i + 2)*sizeof(char *), ret);
+ os_strdup(str, ret[i]);
+ ret[i + 1] = NULL;
+
+ return(ret);
+}
+
+
/* Check if String is on array (Must be NULL terminated) */
int os_IsStrOnArray(char *str, char **array)
{
free(ch1);
ch1 = NULL;
}
-
+
/* Cleaning chat ** */
if(ch2)
{
char **nch2 = ch2;
-
+
while(*ch2 != NULL)
{
free(*ch2);
ch2++;
}
-
+
free(nch2);
nch2 = NULL;
}
-
+
return;
}
{
if(at == NULL)
{
- int strsize = 0;
- if((strsize = strlen(str)) < OS_SIZE_2048)
+ at = strdup(str);
+ if(!at)
{
- at = calloc(strsize+1,sizeof(char));
- if(at == NULL)
- {
- merror(MEM_ERROR,ARGV0);
- return(NULL);
- }
- strncpy(at, str, strsize);
- return(at);
- }
- else
- {
- merror(SIZE_ERROR,ARGV0,str);
- return(NULL);
+ merror(MEM_ERROR,ARGV0);
}
+ return(at);
}
else /*at is not null. Need to reallocat its memory and copy str to it*/
{
+ char *newat;
int strsize = strlen(str);
- int atsize = strlen(at);
- int finalsize = atsize+strsize+1;
+ int finalsize = strsize + strlen(at) + 1;
- if((atsize > OS_SIZE_2048) || (strsize > OS_SIZE_2048))
- {
- merror(SIZE_ERROR,ARGV0,str);
- return(NULL);
- }
-
- at = realloc(at, (finalsize)*sizeof(char));
-
- if(at == NULL)
+ newat = realloc(at, finalsize*sizeof(char));
+ if(newat == NULL)
{
+ free(at);
merror(MEM_ERROR,ARGV0);
return(NULL);
}
+ at = newat;
- strncat(at,str,strsize);
-
- at[finalsize-1] = '\0';
+ strncat(at, str, strsize);
+ at[finalsize -1] = '\0';
return(at);
}
+
return(NULL);
}