2 * Unix SMB/CIFS implementation.
3 * SMB parameters and setup
4 * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 675
18 * Mass Ave, Cambridge, MA 02139, USA.
22 #include "system/shmem.h"
23 #include "system/filesys.h"
25 /*************************************************************************
26 gets a line out of a file.
27 line is of format "xxxx:xxxxxx:xxxxx:".
28 lines with "#" at the front are ignored.
29 *************************************************************************/
30 int getfileline(void *vp, char *linebuf, int linebuf_size)
32 /* Static buffers we will return. */
33 FILE *fp = (FILE *)vp;
40 DEBUG(0,("getfileline: Bad file pointer.\n"));
45 * Scan the file, a line at a time.
51 fgets(linebuf, linebuf_size, fp);
58 * Check if the string is terminated with a newline - if not
59 * then we must keep reading and discard until we get one.
62 linebuf_len = strlen(linebuf);
69 if (linebuf[linebuf_len - 1] != '\n')
72 while (!ferror(fp) && !feof(fp))
83 linebuf[linebuf_len - 1] = '\0';
87 DEBUG(100, ("getfileline: got line |%s|\n", linebuf));
89 if ((linebuf[0] == 0) && feof(fp))
91 DEBUG(4, ("getfileline: end of file reached\n"));
95 if (linebuf[0] == '#' || linebuf[0] == '\0')
97 DEBUG(6, ("getfileline: skipping comment or blank line\n"));
101 p = (uint8_t *) strchr_m(linebuf, ':');
104 DEBUG(0, ("getfileline: malformed line entry (no :)\n"));
113 /****************************************************************************
114 read a line from a file with possible \ continuation chars.
115 Blanks at the start or end of a line are stripped.
116 The string will be allocated if s2 is NULL
117 ****************************************************************************/
118 char *fgets_slash(char *s2,int maxlen,XFILE *f)
123 BOOL start_of_line = True;
128 if (maxlen <2) return(NULL);
132 maxlen = MIN(maxlen,8);
133 s = (char *)malloc(maxlen);
136 if (!s) return(NULL);
140 while (len < maxlen-1)
148 while (len > 0 && s[len-1] == ' ')
152 if (len > 0 && s[len-1] == '\\')
155 start_of_line = True;
162 return(len>0?s:NULL);
167 start_of_line = False;
171 if (!s2 && len > maxlen-3)
176 t = realloc_p(s, char, maxlen);
178 DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
189 /****************************************************************************
190 load a file into memory from a fd.
191 ****************************************************************************/
193 char *fd_load(int fd, size_t *size, TALLOC_CTX *mem_ctx)
198 if (fstat(fd, &sbuf) != 0) return NULL;
200 p = (char *)talloc_size(mem_ctx, sbuf.st_size+1);
203 if (read(fd, p, sbuf.st_size) != sbuf.st_size) {
209 if (size) *size = sbuf.st_size;
214 /****************************************************************************
215 load a file into memory
216 ****************************************************************************/
217 char *file_load(const char *fname, size_t *size, TALLOC_CTX *mem_ctx)
222 if (!fname || !*fname) return NULL;
224 fd = open(fname,O_RDONLY);
225 if (fd == -1) return NULL;
227 p = fd_load(fd, size, mem_ctx);
235 /*******************************************************************
236 mmap (if possible) or read a file
237 ********************************************************************/
238 void *map_file(char *fname, size_t size)
244 fd = open(fname, O_RDONLY, 0);
246 DEBUG(2,("Failed to load %s - %s\n", fname, strerror(errno)));
249 p = mmap(NULL, size, PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
251 if (p == MAP_FAILED) {
252 DEBUG(1,("Failed to mmap %s - %s\n", fname, strerror(errno)));
257 p = file_load(fname, &s2, talloc_autofree_context());
260 DEBUG(1,("incorrect size for %s - got %d expected %d\n",
261 fname, (int)s2, (int)size));
271 /****************************************************************************
272 parse a buffer into lines
273 ****************************************************************************/
274 static char **file_lines_parse(char *p, size_t size, int *numlines)
281 for (s = p, i=0; s < p+size; s++) {
282 if (s[0] == '\n') i++;
285 ret = malloc_array_p(char *, i+2);
290 memset(ret, 0, sizeof(ret[0])*(i+2));
291 if (numlines) *numlines = i;
294 for (s = p, i=0; s < p+size; s++) {
300 if (s[0] == '\r') s[0] = 0;
307 /****************************************************************************
308 load a file into memory and return an array of pointers to lines in the file
309 must be freed with file_lines_free().
310 ****************************************************************************/
311 char **file_lines_load(const char *fname, int *numlines, TALLOC_CTX *mem_ctx)
316 p = file_load(fname, &size, mem_ctx);
319 return file_lines_parse(p, size, numlines);
322 /****************************************************************************
323 load a fd into memory and return an array of pointers to lines in the file
324 must be freed with file_lines_free(). If convert is true calls unix_to_dos on
326 ****************************************************************************/
327 char **fd_lines_load(int fd, int *numlines, TALLOC_CTX *mem_ctx)
332 p = fd_load(fd, &size, mem_ctx);
335 return file_lines_parse(p, size, numlines);
339 /****************************************************************************
340 take a lislist of lines and modify them to produce a list where \ continues
342 ****************************************************************************/
343 void file_lines_slashcont(char **lines)
347 for (i=0; lines[i];) {
348 int len = strlen(lines[i]);
349 if (lines[i][len-1] == '\\') {
350 lines[i][len-1] = ' ';
352 char *p = &lines[i][len];
353 while (p < lines[i+1]) *p++ = ' ';
354 for (j = i+1; lines[j]; j++) lines[j] = lines[j+1];
363 save a lump of data into a file. Mostly used for debugging
365 BOOL file_save(const char *fname, void *packet, size_t length)
368 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
372 if (write(fd, packet, length) != (size_t)length) {
382 BOOL file_exists(const char *path)
385 return (stat(path, &st) == 0);