s4:gensec/gssapi: make calculation of gensec_gssapi_sig_size() for aes keys more...
[Samba.git] / lib / util / util_file.c
blob1ec3582d5505acc8c02bf1f9ce320f1169868e92
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/debug.h"
29 /**
30 * @file
31 * @brief File-related utility functions
34 /**
35 read a line from a file with possible \ continuation chars.
36 Blanks at the start or end of a line are stripped.
37 The string will be allocated if s2 is NULL
38 **/
39 _PUBLIC_ char *fgets_slash(char *s2,int maxlen,XFILE *f)
41 char *s=s2;
42 int len = 0;
43 int c;
44 bool start_of_line = true;
46 if (x_feof(f))
47 return(NULL);
49 if (maxlen <2) return(NULL);
51 if (!s2)
53 maxlen = MIN(maxlen,8);
54 s = (char *)malloc(maxlen);
57 if (!s) return(NULL);
59 *s = 0;
61 while (len < maxlen-1)
63 c = x_getc(f);
64 switch (c)
66 case '\r':
67 break;
68 case '\n':
69 while (len > 0 && s[len-1] == ' ')
71 s[--len] = 0;
73 if (len > 0 && s[len-1] == '\\')
75 s[--len] = 0;
76 start_of_line = true;
77 break;
79 return(s);
80 case EOF:
81 if (len <= 0 && !s2)
82 SAFE_FREE(s);
83 return(len>0?s:NULL);
84 case ' ':
85 if (start_of_line)
86 break;
87 /* fall through */
88 default:
89 start_of_line = false;
90 s[len++] = c;
91 s[len] = 0;
93 if (!s2 && len > maxlen-3)
95 char *t;
97 maxlen *= 2;
98 t = realloc_p(s, char, maxlen);
99 if (!t) {
100 DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
101 SAFE_FREE(s);
102 return(NULL);
103 } else s = t;
106 return(s);
110 * Read one line (data until next newline or eof) and allocate it
112 _PUBLIC_ char *afdgets(int fd, TALLOC_CTX *mem_ctx, size_t hint)
114 char *data = NULL;
115 ssize_t alloc_size = 0, offset = 0, ret;
116 int p;
118 if (hint <= 0) hint = 0x100;
120 do {
121 alloc_size += hint;
123 data = talloc_realloc(mem_ctx, data, char, alloc_size);
125 if (!data)
126 return NULL;
128 ret = read(fd, data + offset, hint);
130 if (ret == 0) {
131 return NULL;
134 if (ret == -1) {
135 talloc_free(data);
136 return NULL;
139 /* Find newline */
140 for (p = 0; p < ret; p++) {
141 if (data[offset + p] == '\n')
142 break;
145 if (p < ret) {
146 data[offset + p] = '\0';
148 /* Go back to position of newline */
149 lseek(fd, p - ret + 1, SEEK_CUR);
150 return data;
153 offset += ret;
155 } while (ret == hint);
157 data[offset] = '\0';
159 return data;
164 load a file into memory from a fd.
166 _PUBLIC_ char *fd_load(int fd, size_t *psize, size_t maxsize, TALLOC_CTX *mem_ctx)
168 struct stat sbuf;
169 char *p;
170 size_t size;
172 if (fstat(fd, &sbuf) != 0) return NULL;
174 size = sbuf.st_size;
176 if (maxsize) {
177 size = MIN(size, maxsize);
180 p = (char *)talloc_size(mem_ctx, size+1);
181 if (!p) return NULL;
183 if (read(fd, p, size) != size) {
184 talloc_free(p);
185 return NULL;
187 p[size] = 0;
189 if (psize) *psize = size;
191 return p;
195 load a file into memory
197 _PUBLIC_ char *file_load(const char *fname, size_t *size, size_t maxsize, TALLOC_CTX *mem_ctx)
199 int fd;
200 char *p;
202 if (!fname || !*fname) return NULL;
204 fd = open(fname,O_RDONLY);
205 if (fd == -1) return NULL;
207 p = fd_load(fd, size, maxsize, mem_ctx);
209 close(fd);
211 return p;
215 parse a buffer into lines
216 'p' will be freed on error, and otherwise will be made a child of the returned array
218 char **file_lines_parse(char *p, size_t size, int *numlines, TALLOC_CTX *mem_ctx)
220 int i;
221 char *s, **ret;
223 if (!p) return NULL;
225 for (s = p, i=0; s < p+size; s++) {
226 if (s[0] == '\n') i++;
229 ret = talloc_zero_array(mem_ctx, char *, i+2);
230 if (!ret) {
231 talloc_free(p);
232 return NULL;
235 talloc_steal(ret, p);
237 ret[0] = p;
238 for (s = p, i=0; s < p+size; s++) {
239 if (s[0] == '\n') {
240 s[0] = 0;
241 i++;
242 ret[i] = s+1;
244 if (s[0] == '\r') s[0] = 0;
247 /* remove any blank lines at the end */
248 while (i > 0 && ret[i-1][0] == 0) {
249 i--;
252 if (numlines) *numlines = i;
254 return ret;
259 load a file into memory and return an array of pointers to lines in the file
260 must be freed with talloc_free().
262 _PUBLIC_ char **file_lines_load(const char *fname, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx)
264 char *p;
265 size_t size;
267 p = file_load(fname, &size, maxsize, mem_ctx);
268 if (!p) return NULL;
270 return file_lines_parse(p, size, numlines, mem_ctx);
274 load a fd into memory and return an array of pointers to lines in the file
275 must be freed with talloc_free(). If convert is true calls unix_to_dos on
276 the list.
278 _PUBLIC_ char **fd_lines_load(int fd, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx)
280 char *p;
281 size_t size;
283 p = fd_load(fd, &size, maxsize, mem_ctx);
284 if (!p) return NULL;
286 return file_lines_parse(p, size, numlines, mem_ctx);
289 _PUBLIC_ bool file_save_mode(const char *fname, const void *packet,
290 size_t length, mode_t mode)
292 int fd;
293 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, mode);
294 if (fd == -1) {
295 return false;
297 if (write(fd, packet, length) != (size_t)length) {
298 close(fd);
299 return false;
301 close(fd);
302 return true;
306 save a lump of data into a file. Mostly used for debugging
308 _PUBLIC_ bool file_save(const char *fname, const void *packet, size_t length)
310 return file_save_mode(fname, packet, length, 0644);
313 _PUBLIC_ int vfdprintf(int fd, const char *format, va_list ap)
315 char *p;
316 int len, ret;
317 va_list ap2;
319 va_copy(ap2, ap);
320 len = vasprintf(&p, format, ap2);
321 va_end(ap2);
322 if (len <= 0) return len;
323 ret = write(fd, p, len);
324 SAFE_FREE(p);
325 return ret;
328 _PUBLIC_ int fdprintf(int fd, const char *format, ...)
330 va_list ap;
331 int ret;
333 va_start(ap, format);
334 ret = vfdprintf(fd, format, ap);
335 va_end(ap);
336 return ret;
341 compare two files, return true if the two files have the same content
343 bool file_compare(const char *path1, const char *path2)
345 size_t size1, size2;
346 char *p1, *p2;
347 TALLOC_CTX *mem_ctx = talloc_new(NULL);
349 p1 = file_load(path1, &size1, 0, mem_ctx);
350 p2 = file_load(path2, &size2, 0, mem_ctx);
351 if (!p1 || !p2 || size1 != size2) {
352 talloc_free(mem_ctx);
353 return false;
355 if (memcmp(p1, p2, size1) != 0) {
356 talloc_free(mem_ctx);
357 return false;
359 talloc_free(mem_ctx);
360 return true;