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