2 functions taken from samba4 for quick prototyping of ctdb. These are
3 not intended to remain part of ctdb
9 #include "system/filesys.h"
12 static char *fd_load(int fd
, size_t *size
, TALLOC_CTX
*mem_ctx
)
17 if (fstat(fd
, &sbuf
) != 0) return NULL
;
19 p
= (char *)talloc_size(mem_ctx
, sbuf
.st_size
+1);
22 if (read(fd
, p
, sbuf
.st_size
) != sbuf
.st_size
) {
28 if (size
) *size
= sbuf
.st_size
;
34 static char *file_load(const char *fname
, size_t *size
, TALLOC_CTX
*mem_ctx
)
39 if (!fname
|| !*fname
) return NULL
;
41 fd
= open(fname
,O_RDONLY
);
42 if (fd
== -1) return NULL
;
44 p
= fd_load(fd
, size
, mem_ctx
);
53 parse a buffer into lines
54 'p' will be freed on error, and otherwise will be made a child of the returned array
56 static char **file_lines_parse(char *p
, size_t size
, int *numlines
, TALLOC_CTX
*mem_ctx
)
63 for (s
= p
, i
=0; s
< p
+size
; s
++) {
64 if (s
[0] == '\n') i
++;
67 ret
= talloc_array(mem_ctx
, char *, i
+2);
75 memset(ret
, 0, sizeof(ret
[0])*(i
+2));
76 if (numlines
) *numlines
= i
;
79 for (s
= p
, i
=0; s
< p
+size
; s
++) {
85 if (s
[0] == '\r') s
[0] = 0;
93 load a file into memory and return an array of pointers to lines in the file
94 must be freed with talloc_free().
96 _PUBLIC_
char **file_lines_load(const char *fname
, int *numlines
, size_t maxsize
, TALLOC_CTX
*mem_ctx
)
101 assert(maxsize
== 0);
103 p
= file_load(fname
, &size
, mem_ctx
);
106 return file_lines_parse(p
, size
, numlines
, mem_ctx
);
109 char *hex_encode_talloc(TALLOC_CTX
*mem_ctx
, const unsigned char *buff_in
, size_t len
)
114 hex_buffer
= talloc_array(mem_ctx
, char, (len
*2)+1);
116 for (i
= 0; i
< len
; i
++)
117 slprintf(&hex_buffer
[i
*2], 3, "%02X", buff_in
[i
]);