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 2 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, write to the Free Software Foundation, Inc., 675
20 * Mass Ave, Cambridge, MA 02139, USA.
24 #include "system/shmem.h"
25 #include "system/filesys.h"
27 /****************************************************************************
28 read a line from a file with possible \ continuation chars.
29 Blanks at the start or end of a line are stripped.
30 The string will be allocated if s2 is NULL
31 ****************************************************************************/
32 char *fgets_slash(char *s2,int maxlen,XFILE *f)
37 BOOL start_of_line = True;
42 if (maxlen <2) return(NULL);
46 maxlen = MIN(maxlen,8);
47 s = (char *)malloc(maxlen);
54 while (len < maxlen-1)
62 while (len > 0 && s[len-1] == ' ')
66 if (len > 0 && s[len-1] == '\\')
81 start_of_line = False;
85 if (!s2 && len > maxlen-3)
90 t = realloc_p(s, char, maxlen);
92 DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
101 /* Read one line (data until next newline or eof) and allocate it */
102 char *afdgets(int fd, TALLOC_CTX *mem_ctx, size_t hint)
105 ssize_t alloc_size = 0, offset = 0, ret;
108 if (hint <= 0) hint = 0x100;
113 data = talloc_realloc(mem_ctx, data, char, alloc_size);
118 ret = read(fd, data + offset, hint);
126 for (p = 0; p < ret; p++) {
127 if (data[offset + p] == '\n')
132 data[offset + p] = '\0';
134 /* Go back to position of newline */
135 lseek(fd, p - ret + 1, SEEK_CUR);
141 } while (ret == hint);
149 /****************************************************************************
150 load a file into memory from a fd.
151 ****************************************************************************/
153 char *fd_load(int fd, size_t *size, TALLOC_CTX *mem_ctx)
158 if (fstat(fd, &sbuf) != 0) return NULL;
160 p = (char *)talloc_size(mem_ctx, sbuf.st_size+1);
163 if (read(fd, p, sbuf.st_size) != sbuf.st_size) {
169 if (size) *size = sbuf.st_size;
174 /****************************************************************************
175 load a file into memory
176 ****************************************************************************/
177 char *file_load(const char *fname, size_t *size, TALLOC_CTX *mem_ctx)
182 if (!fname || !*fname) return NULL;
184 fd = open(fname,O_RDONLY);
185 if (fd == -1) return NULL;
187 p = fd_load(fd, size, mem_ctx);
195 /*******************************************************************
196 mmap (if possible) or read a file
197 ********************************************************************/
198 void *map_file(const char *fname, size_t size)
204 fd = open(fname, O_RDONLY, 0);
206 DEBUG(2,("Failed to load %s - %s\n", fname, strerror(errno)));
209 p = mmap(NULL, size, PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
211 if (p == MAP_FAILED) {
212 DEBUG(1,("Failed to mmap %s - %s\n", fname, strerror(errno)));
217 p = file_load(fname, &s2, talloc_autofree_context());
220 DEBUG(1,("incorrect size for %s - got %d expected %d\n",
221 fname, (int)s2, (int)size));
231 /****************************************************************************
232 parse a buffer into lines
233 ****************************************************************************/
234 static char **file_lines_parse(char *p, size_t size, int *numlines, TALLOC_CTX *mem_ctx)
241 for (s = p, i=0; s < p+size; s++) {
242 if (s[0] == '\n') i++;
245 ret = talloc_array(mem_ctx, char *, i+2);
251 talloc_reference(ret, p);
253 memset(ret, 0, sizeof(ret[0])*(i+2));
254 if (numlines) *numlines = i;
257 for (s = p, i=0; s < p+size; s++) {
263 if (s[0] == '\r') s[0] = 0;
270 /****************************************************************************
271 load a file into memory and return an array of pointers to lines in the file
272 must be freed with talloc_free().
273 ****************************************************************************/
274 char **file_lines_load(const char *fname, int *numlines, TALLOC_CTX *mem_ctx)
280 p = file_load(fname, &size, mem_ctx);
283 lines = file_lines_parse(p, size, numlines, mem_ctx);
290 /****************************************************************************
291 load a fd into memory and return an array of pointers to lines in the file
292 must be freed with talloc_free(). If convert is true calls unix_to_dos on
294 ****************************************************************************/
295 char **fd_lines_load(int fd, int *numlines, TALLOC_CTX *mem_ctx)
301 p = fd_load(fd, &size, mem_ctx);
304 lines = file_lines_parse(p, size, numlines, mem_ctx);
312 /****************************************************************************
313 take a list of lines and modify them to produce a list where \ continues
315 ****************************************************************************/
316 void file_lines_slashcont(char **lines)
320 for (i=0; lines[i];) {
321 int len = strlen(lines[i]);
322 if (lines[i][len-1] == '\\') {
323 lines[i][len-1] = ' ';
325 char *p = &lines[i][len];
326 while (p < lines[i+1]) *p++ = ' ';
327 for (j = i+1; lines[j]; j++) lines[j] = lines[j+1];
336 save a lump of data into a file. Mostly used for debugging
338 BOOL file_save(const char *fname, const void *packet, size_t length)
341 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
345 if (write(fd, packet, length) != (size_t)length) {
355 BOOL file_exists(const char *path)
358 return (stat(path, &st) == 0);
361 int vfdprintf(int fd, const char *format, va_list ap) _PRINTF_ATTRIBUTE(2,0)
369 len = vasprintf(&p, format, ap2);
370 if (len <= 0) return len;
371 ret = write(fd, p, len);
376 int fdprintf(int fd, const char *format, ...) _PRINTF_ATTRIBUTE(2,3)
381 va_start(ap, format);
382 ret = vfdprintf(fd, format, ap);