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)
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
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/>.
23 #include "system/shmem.h"
24 #include "system/filesys.h"
26 #include "lib/util/samba_util.h"
27 #include "lib/util/debug.h"
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
)
35 ssize_t alloc_size
= 0, offset
= 0, ret
;
38 if (hint
<= 0) hint
= 0x100;
43 data
= talloc_realloc(mem_ctx
, data
, char, alloc_size
);
48 ret
= read(fd
, data
+ offset
, hint
);
60 for (p
= 0; p
< ret
; p
++) {
61 if (data
[offset
+ p
] == '\n')
66 data
[offset
+ p
] = '\0';
68 /* Go back to position of newline */
69 lseek(fd
, p
- ret
+ 1, SEEK_CUR
);
75 } while (ret
== hint
);
82 char *fgets_slash(TALLOC_CTX
*mem_ctx
, char *s2
, size_t maxlen
, FILE *f
)
87 bool start_of_line
= true;
98 maxlen
= MIN(maxlen
,8);
99 s
= talloc_array(mem_ctx
, char, maxlen
);
108 while (len
< maxlen
-1) {
115 while (len
> 0 && s
[len
-1] == ' ') {
118 if (len
> 0 && s
[len
-1] == '\\') {
120 start_of_line
= true;
125 if (len
<= 0 && (s2
== NULL
)) {
128 return (len
>0) ? s
: NULL
;
136 start_of_line
= false;
140 if ((s2
== NULL
) && (len
> maxlen
-3)) {
146 DBG_ERR("length overflow");
152 t
= talloc_realloc(mem_ctx
, s
, char, maxlen
);
154 DBG_ERR("failed to expand buffer!\n");
167 load a file into memory from a fd.
169 _PUBLIC_
char *fd_load(int fd
, size_t *psize
, size_t maxsize
, TALLOC_CTX
*mem_ctx
)
175 if (fstat(fd
, &sbuf
) != 0) return NULL
;
180 size
= MIN(size
, maxsize
);
183 p
= (char *)talloc_size(mem_ctx
, size
+1);
186 if (read(fd
, p
, size
) != size
) {
192 if (psize
) *psize
= size
;
198 load a file into memory
200 _PUBLIC_
char *file_load(const char *fname
, size_t *size
, size_t maxsize
, TALLOC_CTX
*mem_ctx
)
205 if (!fname
|| !*fname
) return NULL
;
207 fd
= open(fname
,O_RDONLY
);
208 if (fd
== -1) return NULL
;
210 p
= fd_load(fd
, size
, maxsize
, mem_ctx
);
218 parse a buffer into lines
219 'p' will be freed on error, and otherwise will be made a child of the returned array
221 char **file_lines_parse(char *p
, size_t size
, int *numlines
, TALLOC_CTX
*mem_ctx
)
228 for (s
= p
, i
=0; s
< p
+size
; s
++) {
229 if (s
[0] == '\n') i
++;
232 ret
= talloc_zero_array(mem_ctx
, char *, i
+2);
238 talloc_steal(ret
, p
);
241 for (s
= p
, i
=0; s
< p
+size
; s
++) {
247 if (s
[0] == '\r') s
[0] = 0;
250 /* remove any blank lines at the end */
251 while (i
> 0 && ret
[i
-1][0] == 0) {
255 if (numlines
) *numlines
= i
;
262 load a file into memory and return an array of pointers to lines in the file
263 must be freed with talloc_free().
265 _PUBLIC_
char **file_lines_load(const char *fname
, int *numlines
, size_t maxsize
, TALLOC_CTX
*mem_ctx
)
270 p
= file_load(fname
, &size
, maxsize
, mem_ctx
);
273 return file_lines_parse(p
, size
, numlines
, mem_ctx
);
277 load a fd into memory and return an array of pointers to lines in the file
278 must be freed with talloc_free(). If convert is true calls unix_to_dos on
281 _PUBLIC_
char **fd_lines_load(int fd
, int *numlines
, size_t maxsize
, TALLOC_CTX
*mem_ctx
)
286 p
= fd_load(fd
, &size
, maxsize
, mem_ctx
);
289 return file_lines_parse(p
, size
, numlines
, mem_ctx
);
292 _PUBLIC_
bool file_save_mode(const char *fname
, const void *packet
,
293 size_t length
, mode_t mode
)
296 fd
= open(fname
, O_WRONLY
|O_CREAT
|O_TRUNC
, mode
);
300 if (write(fd
, packet
, length
) != (size_t)length
) {
309 save a lump of data into a file. Mostly used for debugging
311 _PUBLIC_
bool file_save(const char *fname
, const void *packet
, size_t length
)
313 return file_save_mode(fname
, packet
, length
, 0644);
316 _PUBLIC_
int vfdprintf(int fd
, const char *format
, va_list ap
)
323 len
= vasprintf(&p
, format
, ap2
);
325 if (len
<= 0) return len
;
326 ret
= write(fd
, p
, len
);
331 _PUBLIC_
int fdprintf(int fd
, const char *format
, ...)
336 va_start(ap
, format
);
337 ret
= vfdprintf(fd
, format
, ap
);
344 compare two files, return true if the two files have the same content
346 bool file_compare(const char *path1
, const char *path2
)
350 TALLOC_CTX
*mem_ctx
= talloc_new(NULL
);
352 p1
= file_load(path1
, &size1
, 0, mem_ctx
);
353 p2
= file_load(path2
, &size2
, 0, mem_ctx
);
354 if (!p1
|| !p2
|| size1
!= size2
) {
355 talloc_free(mem_ctx
);
358 if (memcmp(p1
, p2
, size1
) != 0) {
359 talloc_free(mem_ctx
);
362 talloc_free(mem_ctx
);