charset/tests: assert the exact values of str[n]casecmp_m()
[Samba.git] / lib / util / util_file.c
blobac8206008a3ccf9d23ad8f14c8534cdb8b9d0ef9
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 * Read one line (data until next newline or eof) and allocate it
32 _PUBLIC_ char *afdgets(int fd, TALLOC_CTX *mem_ctx, size_t hint)
34 char *data = NULL;
35 ssize_t alloc_size = 0, offset = 0, ret;
36 int p;
38 if (hint <= 0) hint = 0x100;
40 do {
41 alloc_size += hint;
43 data = talloc_realloc(mem_ctx, data, char, alloc_size);
45 if (!data)
46 return NULL;
48 ret = read(fd, data + offset, hint);
50 if (ret == 0) {
51 return NULL;
54 if (ret == -1) {
55 talloc_free(data);
56 return NULL;
59 /* Find newline */
60 for (p = 0; p < ret; p++) {
61 if (data[offset + p] == '\n')
62 break;
65 if (p < ret) {
66 data[offset + p] = '\0';
68 /* Go back to position of newline */
69 lseek(fd, p - ret + 1, SEEK_CUR);
70 return data;
73 offset += ret;
75 } while (ret == hint);
77 data[offset] = '\0';
79 return data;
82 char *fgets_slash(TALLOC_CTX *mem_ctx, char *s2, int maxlen, FILE *f)
84 char *s = s2;
85 int len = 0;
86 int c;
87 bool start_of_line = true;
89 if (feof(f)) {
90 return NULL;
93 if (maxlen < 2) {
94 return NULL;
97 if (s2 == NULL) {
98 maxlen = MIN(maxlen,8);
99 s = talloc_array(mem_ctx, char, maxlen);
102 if (s == NULL) {
103 return NULL;
106 *s = 0;
108 while (len < maxlen-1) {
109 c = getc(f);
110 switch (c)
112 case '\r':
113 break;
114 case '\n':
115 while (len > 0 && s[len-1] == ' ') {
116 s[--len] = 0;
118 if (len > 0 && s[len-1] == '\\') {
119 s[--len] = 0;
120 start_of_line = true;
121 break;
123 return s;
124 case EOF:
125 if (len <= 0 && (s2 == NULL)) {
126 TALLOC_FREE(s);
128 return (len>0) ? s : NULL;
129 case ' ':
130 if (start_of_line) {
131 break;
133 /* fall through */
134 default:
135 start_of_line = false;
136 s[len++] = c;
137 s[len] = 0;
139 if ((s2 == NULL) && (len > maxlen-3)) {
140 int m;
141 char *t;
143 m = maxlen * 2;
144 if (m < maxlen) {
145 DBG_ERR("length overflow");
146 TALLOC_FREE(s);
147 return NULL;
149 maxlen = m;
151 t = talloc_realloc(mem_ctx, s, char, maxlen);
152 if (t == NULL) {
153 DBG_ERR("failed to expand buffer!\n");
154 TALLOC_FREE(s);
155 return NULL;
158 s = t;
162 return s;
166 load a file into memory from a fd.
168 _PUBLIC_ char *fd_load(int fd, size_t *psize, size_t maxsize, TALLOC_CTX *mem_ctx)
170 struct stat sbuf;
171 char *p;
172 size_t size;
174 if (fstat(fd, &sbuf) != 0) return NULL;
176 size = sbuf.st_size;
178 if (maxsize) {
179 size = MIN(size, maxsize);
182 p = (char *)talloc_size(mem_ctx, size+1);
183 if (!p) return NULL;
185 if (read(fd, p, size) != size) {
186 talloc_free(p);
187 return NULL;
189 p[size] = 0;
191 if (psize) *psize = size;
193 return p;
197 load a file into memory
199 _PUBLIC_ char *file_load(const char *fname, size_t *size, size_t maxsize, TALLOC_CTX *mem_ctx)
201 int fd;
202 char *p;
204 if (!fname || !*fname) return NULL;
206 fd = open(fname,O_RDONLY);
207 if (fd == -1) return NULL;
209 p = fd_load(fd, size, maxsize, mem_ctx);
211 close(fd);
213 return p;
217 parse a buffer into lines
218 'p' will be freed on error, and otherwise will be made a child of the returned array
220 char **file_lines_parse(char *p, size_t size, int *numlines, TALLOC_CTX *mem_ctx)
222 int i;
223 char *s, **ret;
225 if (!p) return NULL;
227 for (s = p, i=0; s < p+size; s++) {
228 if (s[0] == '\n') i++;
231 ret = talloc_zero_array(mem_ctx, char *, i+2);
232 if (!ret) {
233 talloc_free(p);
234 return NULL;
237 talloc_steal(ret, p);
239 ret[0] = p;
240 for (s = p, i=0; s < p+size; s++) {
241 if (s[0] == '\n') {
242 s[0] = 0;
243 i++;
244 ret[i] = s+1;
246 if (s[0] == '\r') s[0] = 0;
249 /* remove any blank lines at the end */
250 while (i > 0 && ret[i-1][0] == 0) {
251 i--;
254 if (numlines) *numlines = i;
256 return ret;
261 load a file into memory and return an array of pointers to lines in the file
262 must be freed with talloc_free().
264 _PUBLIC_ char **file_lines_load(const char *fname, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx)
266 char *p;
267 size_t size;
269 p = file_load(fname, &size, maxsize, mem_ctx);
270 if (!p) return NULL;
272 return file_lines_parse(p, size, numlines, mem_ctx);
276 load a fd into memory and return an array of pointers to lines in the file
277 must be freed with talloc_free(). If convert is true calls unix_to_dos on
278 the list.
280 _PUBLIC_ char **fd_lines_load(int fd, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx)
282 char *p;
283 size_t size;
285 p = fd_load(fd, &size, maxsize, mem_ctx);
286 if (!p) return NULL;
288 return file_lines_parse(p, size, numlines, mem_ctx);
291 _PUBLIC_ bool file_save_mode(const char *fname, const void *packet,
292 size_t length, mode_t mode)
294 int fd;
295 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, mode);
296 if (fd == -1) {
297 return false;
299 if (write(fd, packet, length) != (size_t)length) {
300 close(fd);
301 return false;
303 close(fd);
304 return true;
308 save a lump of data into a file. Mostly used for debugging
310 _PUBLIC_ bool file_save(const char *fname, const void *packet, size_t length)
312 return file_save_mode(fname, packet, length, 0644);
315 _PUBLIC_ int vfdprintf(int fd, const char *format, va_list ap)
317 char *p;
318 int len, ret;
319 va_list ap2;
321 va_copy(ap2, ap);
322 len = vasprintf(&p, format, ap2);
323 va_end(ap2);
324 if (len <= 0) return len;
325 ret = write(fd, p, len);
326 SAFE_FREE(p);
327 return ret;
330 _PUBLIC_ int fdprintf(int fd, const char *format, ...)
332 va_list ap;
333 int ret;
335 va_start(ap, format);
336 ret = vfdprintf(fd, format, ap);
337 va_end(ap);
338 return ret;
343 compare two files, return true if the two files have the same content
345 bool file_compare(const char *path1, const char *path2)
347 size_t size1, size2;
348 char *p1, *p2;
349 TALLOC_CTX *mem_ctx = talloc_new(NULL);
351 p1 = file_load(path1, &size1, 0, mem_ctx);
352 p2 = file_load(path2, &size2, 0, mem_ctx);
353 if (!p1 || !p2 || size1 != size2) {
354 talloc_free(mem_ctx);
355 return false;
357 if (memcmp(p1, p2, size1) != 0) {
358 talloc_free(mem_ctx);
359 return false;
361 talloc_free(mem_ctx);
362 return true;