Search for location of waf script
[Samba.git] / lib / util / util_file.c
blob90d39f7cdd356087d76382b98bf3b92917060714
1 /*
2 * Unix SMB/CIFS implementation.
3 * SMB parameters and setup
4 * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
6 * Added afdgets() Jelmer Vernooij 2005
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at your option)
11 * any later version.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "replace.h"
23 #include "system/shmem.h"
24 #include "system/filesys.h"
25 #include <talloc.h>
26 #include "lib/util/samba_util.h"
27 #include "lib/util/sys_popen.h"
28 #include "lib/util/sys_rw.h"
29 #include "lib/util/debug.h"
31 /**
32 * Read one line (data until next newline or eof) and allocate it
34 _PUBLIC_ char *afdgets(int fd, TALLOC_CTX *mem_ctx, size_t hint)
36 char *data = NULL;
37 ssize_t alloc_size = 0, offset = 0, ret;
38 int p;
40 if (hint <= 0) hint = 0x100;
42 do {
43 alloc_size += hint;
45 data = talloc_realloc(mem_ctx, data, char, alloc_size);
47 if (!data)
48 return NULL;
50 ret = read(fd, data + offset, hint);
52 if (ret == 0) {
53 return NULL;
56 if (ret == -1) {
57 talloc_free(data);
58 return NULL;
61 /* Find newline */
62 for (p = 0; p < ret; p++) {
63 if (data[offset + p] == '\n')
64 break;
67 if (p < ret) {
68 data[offset + p] = '\0';
70 /* Go back to position of newline */
71 lseek(fd, p - ret + 1, SEEK_CUR);
72 return data;
75 offset += ret;
77 } while (ret == hint);
79 data[offset] = '\0';
81 return data;
84 char *fgets_slash(TALLOC_CTX *mem_ctx, char *s2, size_t maxlen, FILE *f)
86 char *s = s2;
87 size_t len = 0;
88 int c;
89 bool start_of_line = true;
91 if (feof(f)) {
92 return NULL;
95 if (maxlen < 2) {
96 return NULL;
99 if (s2 == NULL) {
100 maxlen = MIN(maxlen,8);
101 s = talloc_array(mem_ctx, char, maxlen);
104 if (s == NULL) {
105 return NULL;
108 *s = 0;
110 while (len < maxlen-1) {
111 c = getc(f);
112 switch (c)
114 case '\r':
115 break;
116 case '\n':
117 while (len > 0 && s[len-1] == ' ') {
118 s[--len] = 0;
120 if (len > 0 && s[len-1] == '\\') {
121 s[--len] = 0;
122 start_of_line = true;
123 break;
125 return s;
126 case EOF:
127 if (len <= 0 && (s2 == NULL)) {
128 TALLOC_FREE(s);
130 return (len>0) ? s : NULL;
131 case ' ':
132 if (start_of_line) {
133 break;
136 FALL_THROUGH;
137 default:
138 start_of_line = false;
139 s[len++] = c;
140 s[len] = 0;
142 if ((s2 == NULL) && (len > maxlen-3)) {
143 int m;
144 char *t;
146 m = maxlen * 2;
147 if (m < maxlen) {
148 DBG_ERR("length overflow");
149 TALLOC_FREE(s);
150 return NULL;
152 maxlen = m;
154 t = talloc_realloc(mem_ctx, s, char, maxlen);
155 if (t == NULL) {
156 DBG_ERR("failed to expand buffer!\n");
157 TALLOC_FREE(s);
158 return NULL;
161 s = t;
165 return s;
169 load a file into memory from a fd.
171 _PUBLIC_ char *fd_load(int fd, size_t *psize, size_t maxsize, TALLOC_CTX *mem_ctx)
173 struct stat sbuf;
174 char *p;
175 size_t size;
177 if (fstat(fd, &sbuf) != 0) return NULL;
179 size = sbuf.st_size;
181 if (maxsize) {
182 size = MIN(size, maxsize);
185 p = (char *)talloc_size(mem_ctx, size+1);
186 if (!p) return NULL;
188 if (read(fd, p, size) != size) {
189 talloc_free(p);
190 return NULL;
192 p[size] = 0;
194 if (psize) *psize = size;
196 return p;
200 load a file into memory
202 _PUBLIC_ char *file_load(const char *fname, size_t *size, size_t maxsize, TALLOC_CTX *mem_ctx)
204 int fd;
205 char *p;
207 if (!fname || !*fname) return NULL;
209 fd = open(fname,O_RDONLY);
210 if (fd == -1) return NULL;
212 p = fd_load(fd, size, maxsize, mem_ctx);
214 close(fd);
216 return p;
220 parse a buffer into lines
221 'p' will be freed on error, and otherwise will be made a child of the returned array
223 char **file_lines_parse(char *p, size_t size, int *numlines, TALLOC_CTX *mem_ctx)
225 unsigned int i;
226 char *s, **ret;
228 if (!p) return NULL;
230 for (s = p, i=0; s < p+size; s++) {
231 if (s[0] == '\n') i++;
234 ret = talloc_zero_array(mem_ctx, char *, i+2);
235 if (!ret) {
236 talloc_free(p);
237 return NULL;
240 talloc_steal(ret, p);
242 ret[0] = p;
243 for (s = p, i=1; s < p+size; s++) {
244 if (s[0] == '\n') {
245 s[0] = 0;
246 ret[i] = s+1;
247 i++;
249 if (s[0] == '\r') s[0] = 0;
252 /* remove any blank lines at the end */
253 while (i > 0 && ret[i-1][0] == 0) {
254 i--;
257 if (numlines) *numlines = i;
259 return ret;
264 load a file into memory and return an array of pointers to lines in the file
265 must be freed with talloc_free().
267 _PUBLIC_ char **file_lines_load(const char *fname, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx)
269 char *p;
270 size_t size;
272 p = file_load(fname, &size, maxsize, mem_ctx);
273 if (!p) return NULL;
275 return file_lines_parse(p, size, numlines, mem_ctx);
279 load a fd into memory and return an array of pointers to lines in the file
280 must be freed with talloc_free(). If convert is true calls unix_to_dos on
281 the list.
283 _PUBLIC_ char **fd_lines_load(int fd, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx)
285 char *p;
286 size_t size;
288 p = fd_load(fd, &size, maxsize, mem_ctx);
289 if (!p) return NULL;
291 return file_lines_parse(p, size, numlines, mem_ctx);
294 _PUBLIC_ bool file_save_mode(const char *fname, const void *packet,
295 size_t length, mode_t mode)
297 int fd;
298 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, mode);
299 if (fd == -1) {
300 return false;
302 if (write(fd, packet, length) != (size_t)length) {
303 close(fd);
304 return false;
306 close(fd);
307 return true;
311 save a lump of data into a file. Mostly used for debugging
313 _PUBLIC_ bool file_save(const char *fname, const void *packet, size_t length)
315 return file_save_mode(fname, packet, length, 0644);
318 _PUBLIC_ int vfdprintf(int fd, const char *format, va_list ap)
320 char *p;
321 int len, ret;
322 va_list ap2;
324 va_copy(ap2, ap);
325 len = vasprintf(&p, format, ap2);
326 va_end(ap2);
327 if (len <= 0) return len;
328 ret = write(fd, p, len);
329 SAFE_FREE(p);
330 return ret;
333 _PUBLIC_ int fdprintf(int fd, const char *format, ...)
335 va_list ap;
336 int ret;
338 va_start(ap, format);
339 ret = vfdprintf(fd, format, ap);
340 va_end(ap);
341 return ret;
346 compare two files, return true if the two files have the same content
348 bool file_compare(const char *path1, const char *path2)
350 size_t size1, size2;
351 char *p1, *p2;
352 TALLOC_CTX *mem_ctx = talloc_new(NULL);
354 p1 = file_load(path1, &size1, 0, mem_ctx);
355 p2 = file_load(path2, &size2, 0, mem_ctx);
356 if (!p1 || !p2 || size1 != size2) {
357 talloc_free(mem_ctx);
358 return false;
360 if (memcmp(p1, p2, size1) != 0) {
361 talloc_free(mem_ctx);
362 return false;
364 talloc_free(mem_ctx);
365 return true;
370 Load from a pipe into memory.
372 char *file_pload(const char *syscmd, size_t *size)
374 int fd, n;
375 char *p;
376 char buf[1024];
377 size_t total;
379 fd = sys_popen(syscmd);
380 if (fd == -1) {
381 return NULL;
384 p = NULL;
385 total = 0;
387 while ((n = sys_read(fd, buf, sizeof(buf))) > 0) {
388 p = talloc_realloc(NULL, p, char, total + n + 1);
389 if (!p) {
390 DEBUG(0,("file_pload: failed to expand buffer!\n"));
391 close(fd);
392 return NULL;
394 memcpy(p+total, buf, n);
395 total += n;
398 if (p) {
399 p[total] = 0;
402 /* FIXME: Perhaps ought to check that the command completed
403 * successfully (returned 0); if not the data may be
404 * truncated. */
405 sys_pclose(fd);
407 if (size) {
408 *size = total;
411 return p;