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"
24 /*************************************************************************
25 gets a line out of a file.
26 line is of format "xxxx:xxxxxx:xxxxx:".
27 lines with "#" at the front are ignored.
28 *************************************************************************/
29 int getfileline(void *vp, char *linebuf, int linebuf_size)
31 /* Static buffers we will return. */
32 FILE *fp = (FILE *)vp;
39 DEBUG(0,("getfileline: Bad file pointer.\n"));
44 * Scan the file, a line at a time.
50 fgets(linebuf, linebuf_size, fp);
57 * Check if the string is terminated with a newline - if not
58 * then we must keep reading and discard until we get one.
61 linebuf_len = strlen(linebuf);
68 if (linebuf[linebuf_len - 1] != '\n')
71 while (!ferror(fp) && !feof(fp))
82 linebuf[linebuf_len - 1] = '\0';
86 DEBUG(100, ("getfileline: got line |%s|\n", linebuf));
88 if ((linebuf[0] == 0) && feof(fp))
90 DEBUG(4, ("getfileline: end of file reached\n"));
94 if (linebuf[0] == '#' || linebuf[0] == '\0')
96 DEBUG(6, ("getfileline: skipping comment or blank line\n"));
100 p = (uint8_t *) strchr_m(linebuf, ':');
103 DEBUG(0, ("getfileline: malformed line entry (no :)\n"));
112 /****************************************************************************
113 read a line from a file with possible \ continuation chars.
114 Blanks at the start or end of a line are stripped.
115 The string will be allocated if s2 is NULL
116 ****************************************************************************/
117 char *fgets_slash(char *s2,int maxlen,XFILE *f)
122 BOOL start_of_line = True;
127 if (maxlen <2) return(NULL);
131 maxlen = MIN(maxlen,8);
132 s = (char *)malloc(maxlen);
135 if (!s) return(NULL);
139 while (len < maxlen-1)
147 while (len > 0 && s[len-1] == ' ')
151 if (len > 0 && s[len-1] == '\\')
154 start_of_line = True;
161 return(len>0?s:NULL);
166 start_of_line = False;
170 if (!s2 && len > maxlen-3)
175 t = realloc_p(s, char, maxlen);
177 DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
188 /****************************************************************************
189 load a file into memory from a fd.
190 ****************************************************************************/
192 char *fd_load(int fd, size_t *size)
197 if (fstat(fd, &sbuf) != 0) return NULL;
199 p = (char *)malloc(sbuf.st_size+1);
202 if (read(fd, p, sbuf.st_size) != sbuf.st_size) {
208 if (size) *size = sbuf.st_size;
213 /****************************************************************************
214 load a file into memory
215 ****************************************************************************/
216 char *file_load(const char *fname, size_t *size)
221 if (!fname || !*fname) return NULL;
223 fd = open(fname,O_RDONLY);
224 if (fd == -1) return NULL;
226 p = fd_load(fd, size);
234 /*******************************************************************
235 mmap (if possible) or read a file
236 ********************************************************************/
237 void *map_file(char *fname, size_t size)
243 fd = open(fname, O_RDONLY, 0);
245 DEBUG(2,("Failed to load %s - %s\n", fname, strerror(errno)));
248 p = mmap(NULL, size, PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
250 if (p == MAP_FAILED) {
251 DEBUG(1,("Failed to mmap %s - %s\n", fname, strerror(errno)));
256 p = file_load(fname, &s2);
259 DEBUG(1,("incorrect size for %s - got %d expected %d\n",
270 /****************************************************************************
271 parse a buffer into lines
272 ****************************************************************************/
273 static char **file_lines_parse(char *p, size_t size, int *numlines)
280 for (s = p, i=0; s < p+size; s++) {
281 if (s[0] == '\n') i++;
284 ret = (char **)malloc(sizeof(ret[0])*(i+2));
289 memset(ret, 0, sizeof(ret[0])*(i+2));
290 if (numlines) *numlines = i;
293 for (s = p, i=0; s < p+size; s++) {
299 if (s[0] == '\r') s[0] = 0;
306 /****************************************************************************
307 load a file into memory and return an array of pointers to lines in the file
308 must be freed with file_lines_free().
309 ****************************************************************************/
310 char **file_lines_load(const char *fname, int *numlines)
315 p = file_load(fname, &size);
318 return file_lines_parse(p, size, numlines);
321 /****************************************************************************
322 load a fd into memory and return an array of pointers to lines in the file
323 must be freed with file_lines_free(). If convert is true calls unix_to_dos on
325 ****************************************************************************/
326 char **fd_lines_load(int fd, int *numlines)
331 p = fd_load(fd, &size);
334 return file_lines_parse(p, size, numlines);
338 /****************************************************************************
339 free lines loaded with file_lines_load
340 ****************************************************************************/
341 void file_lines_free(char **lines)
349 /****************************************************************************
350 take a lislist of lines and modify them to produce a list where \ continues
352 ****************************************************************************/
353 void file_lines_slashcont(char **lines)
357 for (i=0; lines[i];) {
358 int len = strlen(lines[i]);
359 if (lines[i][len-1] == '\\') {
360 lines[i][len-1] = ' ';
362 char *p = &lines[i][len];
363 while (p < lines[i+1]) *p++ = ' ';
364 for (j = i+1; lines[j]; j++) lines[j] = lines[j+1];
373 save a lump of data into a file. Mostly used for debugging
375 BOOL file_save(const char *fname, void *packet, size_t length)
378 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
382 if (write(fd, packet, length) != (size_t)length) {