izmjene licence
[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 #include "shared.h"
16
17
18 /* Add pointer to array. */
19 void **os_AddPtArray(void *pt, void **array)
20 {
21     int i = 0;
22     void **ret = NULL;
23
24     if(array)
25     {
26         while(array[i])
27         {
28             i++;
29         }
30     }
31
32     os_realloc(array, (i + 2)*sizeof(char *), ret);
33     ret[i] = pt;
34     ret[i + 1] = NULL;
35
36     return(ret);
37 }
38
39
40 /* Add a string to an array. */
41 char **os_AddStrArray(char *str, char **array)
42 {
43     int i = 0;
44     char **ret = NULL;
45     if(array)
46     {
47         while(array[i])
48         {
49             i++;
50         }
51     }
52
53     os_realloc(array, (i + 2)*sizeof(char *), ret);
54     os_strdup(str, ret[i]);
55     ret[i + 1] = NULL;
56
57     return(ret);
58 }
59
60
61 /* Check if String is on array (Must be NULL terminated) */
62 int os_IsStrOnArray(char *str, char **array)
63 {
64     if(!str || !array)
65     {
66         return(0);
67     }
68
69     while(*array)
70     {
71         if(strcmp(*array, str) == 0)
72         {
73             return(1);
74         }
75         array++;
76     }
77     return(0);
78 }
79
80
81 /* Clear the memory of one char and one char** */
82 void os_FreeArray(char *ch1, char **ch2)
83 {
84     /* Cleaning char * */
85     if(ch1)
86     {
87         free(ch1);
88         ch1 = NULL;
89     }
90
91     /* Cleaning chat ** */
92     if(ch2)
93     {
94         char **nch2 = ch2;
95
96         while(*ch2 != NULL)
97         {
98             free(*ch2);
99             ch2++;
100         }
101
102         free(nch2);
103         nch2 = NULL;
104     }
105
106     return;
107 }
108
109
110 /* os_LoadString: v0.1
111  * Allocate memory at "*at" and copy *str to it.
112  * If *at already exist, realloc the memory and strcat str
113  * on it.
114  * It will return the new string on success or NULL on memory error.
115  */
116 char *os_LoadString(char *at, char *str)
117 {
118     if(at == NULL)
119     {
120         at = strdup(str);
121         if(!at)
122         {
123             merror(MEM_ERROR,ARGV0);
124         }
125         return(at);
126     }
127     else /*at is not null. Need to reallocat its memory and copy str to it*/
128     {
129         char *newat;
130         int strsize = strlen(str);
131         int finalsize = strsize + strlen(at) + 1;
132
133         newat = realloc(at, finalsize*sizeof(char));
134         if(newat == NULL)
135         {
136             free(at);
137             merror(MEM_ERROR,ARGV0);
138             return(NULL);
139         }
140         at = newat;
141
142         strncat(at, str, strsize);
143         at[finalsize -1] = '\0';
144
145         return(at);
146     }
147
148     return(NULL);
149 }
150
151
152 /* EOF */