+
+void grep_file_buffer(unsigned num)
+{
+ struct stat st;
+ void *xid, *buf;
+ int fd, res;
+
+ fd = open(_filenames[num], O_RDONLY);
+ if (fd == -1)
+ {
+ printf("read error opening file '%s'\n", _filenames[num]);
+ return;
+ }
+
+ fstat(fd, &st);
+ buf = malloc(st.st_size);
+ if (!buf)
+ {
+ printf("unable to allocate enough memory for reading.\n");
+ return;
+ }
+
+ res = read(fd, buf, st.st_size);
+ if (res == -1)
+ {
+ printf("unable to read from file '%s'.\n", _filenames[num]);
+ return;
+ }
+ close(fd);
+
+ xid = xmlInitBuffer(buf, st.st_size);
+ if (xid)
+ {
+ void *xrid = xmlMarkId(xid);
+ int r = 0;
+
+ walk_the_tree(num, xrid, _root);
+
+ r = xmlErrorGetNo(xrid, 0);
+ if (r)
+ {
+ size_t n = xmlErrorGetLineNo(xrid, 0);
+ size_t c = xmlErrorGetColumnNo(xrid, 0);
+ const char *s = xmlErrorGetString(xrid, 1); /* clear the error */
+ printf("%s: at line %u, column %u: '%s'\n",_filenames[num], n,c, s);
+ }
+
+ free(xrid);
+ }
+ else
+ {
+ fprintf(stderr, "Error reading file '%s'\n", _filenames[num]);
+ }
+
+ xmlClose(xid);
+ free(buf);
+}
+