1 /* @(#) $Id: ./src/os_xml/os_xml_writer.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
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
14 * Available at http://www.ossec.net/
21 #include "os_xml_writer.h"
35 /* Internal functions */
36 int _oswcomment(FILE *fp_in, FILE *fp_out);
37 int _WReadElem(FILE *fp_in, FILE *fp_out, int position, int parent,
38 char **node, char *value, int node_pos);
46 int _xml_wfgetc(FILE *fp_in, FILE *fp_out)
50 /* Putting on fp_out, whatever we read */
57 if(c == '\n') /* add new line */
63 #define FWGETC(fp_in, fp_out) _xml_wfgetc(fp_in, fp_out)
68 * Write an XML file, based on the input and values to change.
70 int OS_WriteXML(char *infile, char *outfile, char **nodes, char *attr,
71 char *oldval, char *newval, int type)
79 /* Nodes and newval must be set. */
86 fp_in = fopen(infile,"r");
93 /* Opening out file */
94 fp_out = fopen(outfile,"w");
102 if((r = _WReadElem(fp_in, fp_out, 0, 0,
103 nodes, newval, node_pos)) < 0) /* First position */
110 /* We didn't find an entry, add at the end. */
111 if(!oldval && r == 0)
116 fseek(fp_out, 0, SEEK_END);
117 fprintf(fp_out, "\n");
119 /* Printing each node. */
122 fprintf(fp_out, "%*c<%s>", rwidth, ' ', nodes[r]);
127 fprintf(fp_out, "\n");
133 fprintf(fp_out, "%s</%s>\n", newval, nodes[r]);
137 /* Closing each node. */
140 fprintf(fp_out, "%*c</%s>\n", rwidth, ' ', nodes[r]);
153 /* Getting comments */
154 int _oswcomment(FILE *fp_in, FILE *fp_out)
157 if((c = fgetc(fp_in)) == _R_COM)
160 while((c = FWGETC(fp_in, fp_out)) != EOF)
164 if((c=fgetc(fp_in)) == _R_CONFE)
171 else if(c == '-') /* W3C way of finish comments */
173 if((c = fgetc(fp_in)) == '-')
176 if((c = fgetc(fp_in)) == _R_CONFE)
205 int _WReadElem(FILE *fp_in, FILE *fp_out,
206 int position, int parent, char **nodes, char *val, int node_pos)
210 unsigned int count = 0;
211 short int location = -1;
213 char elem[XML_MAXSIZE +1];
214 char cont[XML_MAXSIZE +1];
215 char closedelem[XML_MAXSIZE +1];
217 memset(elem,'\0',XML_MAXSIZE +1);
218 memset(cont,'\0',XML_MAXSIZE +1);
219 memset(closedelem,'\0',XML_MAXSIZE +1);
222 while((c = FWGETC(fp_in, fp_out)) != EOF)
225 if(count >= XML_MAXSIZE)
230 /* Checking for comments */
234 if((r = _oswcomment(fp_in, fp_out)) < 0)
248 /* Must be the opening element */
251 if((c = fgetc(fp_in)) == '/')
268 /* Looking for the closure */
269 else if((location == 0) && ((c == _R_CONFE) || (c == ' ')))
275 /* Removing the / at the end of the element name */
276 if(elem[count -1] == '/')
279 elem[count -1] = '\0';
283 /* If we may have more attributes */
286 /* Writing the attributes */
287 while((c = FWGETC(fp_in, fp_out)) != EOF)
297 /* If the element is closed already (finished in />) */
303 memset(elem,'\0',XML_MAXSIZE);
304 memset(closedelem,'\0',XML_MAXSIZE);
305 memset(cont,'\0',XML_MAXSIZE);
312 /* Location == means we are getting the content */
320 /* Checking position of the node */
321 if(node_pos > position)
326 /* Checking if the element name matches */
327 if(node_pos == position &&
328 nodes[node_pos] && strcmp(elem, nodes[node_pos]) == 0)
332 /* Latest node, printint value */
336 fprintf(fp_out, "%s", val);
338 while((c = fgetc(fp_in)) != EOF)
350 else if((location == 2) &&(c == _R_CONFE))
352 closedelem[count]='\0';
353 if(strcmp(closedelem,elem) != 0)
358 memset(elem,'\0',XML_MAXSIZE);
359 memset(closedelem,'\0',XML_MAXSIZE);
360 memset(cont,'\0',XML_MAXSIZE);
370 /* If we are reading the element */
371 else if((location == 1) &&(c == _R_CONFS))
373 if((c=fgetc(fp_in)) == '/')
385 ungetc(_R_CONFS,fp_in);
386 fseek(fp_out, -1, SEEK_CUR);
388 if((wret_code = _WReadElem(fp_in, fp_out, position+1, parent+1,
389 nodes, val, node_pos))< 0)
394 /* Setting final return code. */
409 else if(location == 1)
413 else if(location == 2)
415 closedelem[count++] = c;