2 functions taken from samba4 for quick prototyping of ctdb. These are
3 not intended to remain part of ctdb
7 #include "system/filesys.h"
10 static char *fd_load(int fd, size_t *size, TALLOC_CTX *mem_ctx)
15 if (fstat(fd, &sbuf) != 0) return NULL;
17 p = (char *)talloc_size(mem_ctx, sbuf.st_size+1);
20 if (read(fd, p, sbuf.st_size) != sbuf.st_size) {
26 if (size) *size = sbuf.st_size;
32 static char *file_load(const char *fname, size_t *size, TALLOC_CTX *mem_ctx)
37 if (!fname || !*fname) return NULL;
39 fd = open(fname,O_RDONLY);
40 if (fd == -1) return NULL;
42 p = fd_load(fd, size, mem_ctx);
51 parse a buffer into lines
52 'p' will be freed on error, and otherwise will be made a child of the returned array
54 static char **file_lines_parse(char *p, size_t size, int *numlines, TALLOC_CTX *mem_ctx)
61 for (s = p, i=0; s < p+size; s++) {
62 if (s[0] == '\n') i++;
65 ret = talloc_array(mem_ctx, char *, i+2);
73 memset(ret, 0, sizeof(ret[0])*(i+2));
74 if (numlines) *numlines = i;
77 for (s = p, i=0; s < p+size; s++) {
83 if (s[0] == '\r') s[0] = 0;
91 load a file into memory and return an array of pointers to lines in the file
92 must be freed with talloc_free().
94 _PUBLIC_ char **file_lines_load(const char *fname, int *numlines, TALLOC_CTX *mem_ctx)
99 p = file_load(fname, &size, mem_ctx);
102 return file_lines_parse(p, size, numlines, mem_ctx);
105 char *hex_encode_talloc(TALLOC_CTX *mem_ctx, const unsigned char *buff_in, size_t len)
110 hex_buffer = talloc_array(mem_ctx, char, (len*2)+1);
112 for (i = 0; i < len; i++)
113 slprintf(&hex_buffer[i*2], 3, "%02X", buff_in[i]);
118 uint8_t *hex_decode_talloc(TALLOC_CTX *mem_ctx, const char *hex_in, size_t *len)
123 *len = strlen(hex_in) / 2;
124 buffer = talloc_array(mem_ctx, unsigned char, *len);
126 for (i=0; i<*len; i++) {
127 sscanf(&hex_in[i*2], "%02X", &num);
128 buffer[i] = (uint8_t)num;