Imported Upstream version 2.5.1
[ossec-hids.git] / src / shared / mem_op.c
1 /* @(#) $Id$ */
2
3 /* Copyright (C) 2009 Trend Micro Inc.
4  * All rights reserved.
5  *
6  * This program is a free software; you can redistribute it
7  * and/or modify it under the terms of the GNU General Public
8  * License (version 2) as published by the FSF - Free Software
9  * Foundation
10  */
11
12
13 #include "mem_op.h"
14
15
16 /* Add pointer to array. */
17 void **os_AddPtArray(void *pt, void **array)
18 {
19     int i = 0;
20     void **ret = NULL;
21
22     if(array)
23     {
24         while(array[i])
25         {
26             i++;
27         }
28     }
29
30     os_realloc(array, (i + 2)*sizeof(char *), ret);
31     ret[i] = pt;
32     ret[i + 1] = NULL;
33
34     return(ret);
35 }
36
37
38 /* Add a string to an array. */
39 char **os_AddStrArray(char *str, char **array)
40 {
41     int i = 0;
42     char **ret = NULL;
43     if(array)
44     {
45         while(array[i])
46         {
47             i++;
48         }
49     }
50
51     os_realloc(array, (i + 2)*sizeof(char *), ret);
52     os_strdup(str, ret[i]);
53     ret[i + 1] = NULL;
54
55     return(ret);
56 }
57
58
59 /* Check if String is on array (Must be NULL terminated) */
60 int os_IsStrOnArray(char *str, char **array)
61 {
62     if(!str || !array)
63     {
64         return(0);
65     }
66
67     while(*array)
68     {
69         if(strcmp(*array, str) == 0)
70         {
71             return(1);
72         }
73         array++;
74     }
75     return(0);
76 }
77
78
79 /* Clear the memory of one char and one char** */
80 void os_FreeArray(char *ch1, char **ch2)
81 {
82     /* Cleaning char * */
83     if(ch1)
84     {
85         free(ch1);
86         ch1 = NULL;
87     }
88     
89     /* Cleaning chat ** */
90     if(ch2)
91     {
92         char **nch2 = ch2;
93             
94         while(*ch2 != NULL)
95         {
96             free(*ch2);
97             ch2++;
98         }
99     
100         free(nch2);
101         nch2 = NULL;
102     }
103     
104     return;
105 }
106
107
108 /* os_LoadString: v0.1
109  * Allocate memory at "*at" and copy *str to it.
110  * If *at already exist, realloc the memory and strcat str
111  * on it.
112  * It will return the new string on success or NULL on memory error.
113  */
114 char *os_LoadString(char *at, char *str)
115 {
116     if(at == NULL)
117     {
118         at = strdup(str);
119         if(!at)
120         {
121             merror(MEM_ERROR,ARGV0);
122         }
123         return(at);
124     }
125     else /*at is not null. Need to reallocat its memory and copy str to it*/
126     {
127         char *newat;
128         int strsize = strlen(str);
129         int finalsize = strsize + strlen(at) + 1;
130
131         newat = realloc(at, finalsize*sizeof(char));
132         if(newat == NULL)
133         {
134             free(at);
135             merror(MEM_ERROR,ARGV0);
136             return(NULL);
137         }
138         at = newat;
139
140         strncat(at, str, strsize);
141         at[finalsize -1] = '\0';
142
143         return(at);
144     }
145
146     return(NULL);
147 }
148
149
150 /* EOF */