More warning fixes for Solaris.
[Samba.git] / source / lib / util_file.c
blobf9ffa44b03b759e9e3870d93454c68e3237ad732
1 /*
2 * Unix SMB/CIFS implementation.
3 * SMB parameters and setup
4 * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
5 *
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 3 of the License, or (at your option)
9 * any later version.
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
14 * more details.
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
22 #ifndef MAP_FAILED
23 #define MAP_FAILED ((void *)-1)
24 #endif
26 /****************************************************************************
27 Read a line from a file with possible \ continuation chars.
28 Blanks at the start or end of a line are stripped.
29 The string will be allocated if s2 is NULL.
30 ****************************************************************************/
32 char *fgets_slash(char *s2,int maxlen,XFILE *f)
34 char *s=s2;
35 int len = 0;
36 int c;
37 bool start_of_line = True;
39 if (x_feof(f)) {
40 return(NULL);
43 if (maxlen <2) {
44 return(NULL);
47 if (!s2) {
48 maxlen = MIN(maxlen,8);
49 s = (char *)SMB_MALLOC(maxlen);
52 if (!s) {
53 return(NULL);
56 *s = 0;
58 while (len < maxlen-1) {
59 c = x_getc(f);
60 switch (c) {
61 case '\r':
62 break;
63 case '\n':
64 while (len > 0 && s[len-1] == ' ') {
65 s[--len] = 0;
67 if (len > 0 && s[len-1] == '\\') {
68 s[--len] = 0;
69 start_of_line = True;
70 break;
72 return(s);
73 case EOF:
74 if (len <= 0 && !s2) {
75 SAFE_FREE(s);
77 return(len>0?s:NULL);
78 case ' ':
79 if (start_of_line) {
80 break;
82 default:
83 start_of_line = False;
84 s[len++] = c;
85 s[len] = 0;
88 if (!s2 && len > maxlen-3) {
89 maxlen *= 2;
90 s = (char *)SMB_REALLOC(s,maxlen);
91 if (!s) {
92 DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
93 return(NULL);
97 return(s);
100 /****************************************************************************
101 Load from a pipe into memory.
102 ****************************************************************************/
104 static char *file_pload(const char *syscmd, size_t *size)
106 int fd, n;
107 char *p;
108 char buf[1024];
109 size_t total;
111 fd = sys_popen(syscmd);
112 if (fd == -1) {
113 return NULL;
116 p = NULL;
117 total = 0;
119 while ((n = read(fd, buf, sizeof(buf))) > 0) {
120 p = (char *)SMB_REALLOC(p, total + n + 1);
121 if (!p) {
122 DEBUG(0,("file_pload: failed to expand buffer!\n"));
123 close(fd);
124 return NULL;
126 memcpy(p+total, buf, n);
127 total += n;
130 if (p) {
131 p[total] = 0;
134 /* FIXME: Perhaps ought to check that the command completed
135 * successfully (returned 0); if not the data may be
136 * truncated. */
137 sys_pclose(fd);
139 if (size) {
140 *size = total;
143 return p;
146 /****************************************************************************
147 Load a file into memory from a fd.
148 Truncate at maxsize. If maxsize == 0 - no limit.
149 ****************************************************************************/
151 char *fd_load(int fd, size_t *psize, size_t maxsize)
153 SMB_STRUCT_STAT sbuf;
154 size_t size;
155 char *p;
157 if (sys_fstat(fd, &sbuf) != 0) {
158 return NULL;
161 size = sbuf.st_size;
162 if (maxsize) {
163 size = MIN(size, maxsize);
166 p = (char *)SMB_MALLOC(size+1);
167 if (!p) {
168 return NULL;
171 if (read(fd, p, size) != size) {
172 SAFE_FREE(p);
173 return NULL;
175 p[size] = 0;
177 if (psize) {
178 *psize = size;
181 return p;
184 /****************************************************************************
185 Load a file into memory.
186 ****************************************************************************/
188 char *file_load(const char *fname, size_t *size, size_t maxsize)
190 int fd;
191 char *p;
193 if (!fname || !*fname) {
194 return NULL;
197 fd = open(fname,O_RDONLY);
198 if (fd == -1) {
199 return NULL;
202 p = fd_load(fd, size, maxsize);
203 close(fd);
204 return p;
207 /*******************************************************************
208 unmap or free memory
209 *******************************************************************/
211 bool unmap_file(void* start, size_t size)
213 #ifdef HAVE_MMAP
214 if ( munmap( start, size ) != 0 ) {
215 DEBUG( 1, ("map_file: Failed to unmap address %p "
216 "of size %u - %s\n",
217 start, (unsigned int)size, strerror(errno) ));
218 return False;
220 return True;
221 #else
222 SAFE_FREE( start );
223 return True;
224 #endif
227 /*******************************************************************
228 mmap (if possible) or read a file.
229 ********************************************************************/
231 void *map_file(char *fname, size_t size)
233 size_t s2 = 0;
234 void *p = NULL;
235 #ifdef HAVE_MMAP
236 int fd;
237 fd = open(fname, O_RDONLY, 0);
238 if (fd == -1) {
239 DEBUG(2,("map_file: Failed to load %s - %s\n", fname, strerror(errno)));
240 return NULL;
242 p = mmap(NULL, size, PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
243 close(fd);
244 if (p == MAP_FAILED) {
245 DEBUG(1,("map_file: Failed to mmap %s - %s\n", fname, strerror(errno)));
246 return NULL;
248 #endif
249 if (!p) {
250 p = file_load(fname, &s2, 0);
251 if (!p) {
252 return NULL;
254 if (s2 != size) {
255 DEBUG(1,("map_file: incorrect size for %s - got %lu expected %lu\n",
256 fname, (unsigned long)s2, (unsigned long)size));
257 SAFE_FREE(p);
258 return NULL;
261 return p;
264 /****************************************************************************
265 Parse a buffer into lines.
266 ****************************************************************************/
268 static char **file_lines_parse(char *p, size_t size, int *numlines)
270 int i;
271 char *s, **ret;
273 if (!p) {
274 return NULL;
277 for (s = p, i=0; s < p+size; s++) {
278 if (s[0] == '\n') i++;
281 ret = SMB_MALLOC_ARRAY(char *, i+2);
282 if (!ret) {
283 SAFE_FREE(p);
284 return NULL;
286 memset(ret, 0, sizeof(ret[0])*(i+2));
288 ret[0] = p;
289 for (s = p, i=0; s < p+size; s++) {
290 if (s[0] == '\n') {
291 s[0] = 0;
292 i++;
293 ret[i] = s+1;
295 if (s[0] == '\r') {
296 s[0] = 0;
300 /* remove any blank lines at the end */
301 while (i > 0 && ret[i-1][0] == 0) {
302 i--;
305 if (numlines) {
306 *numlines = i;
309 return ret;
312 /****************************************************************************
313 Load a file into memory and return an array of pointers to lines in the file
314 must be freed with file_lines_free().
315 ****************************************************************************/
317 char **file_lines_load(const char *fname, int *numlines, size_t maxsize)
319 char *p;
320 size_t size = 0;
322 p = file_load(fname, &size, maxsize);
323 if (!p) {
324 return NULL;
327 return file_lines_parse(p, size, numlines);
330 /****************************************************************************
331 Load a fd into memory and return an array of pointers to lines in the file
332 must be freed with file_lines_free(). If convert is true calls unix_to_dos on
333 the list.
334 ****************************************************************************/
336 char **fd_lines_load(int fd, int *numlines, size_t maxsize)
338 char *p;
339 size_t size;
341 p = fd_load(fd, &size, maxsize);
342 if (!p) {
343 return NULL;
346 return file_lines_parse(p, size, numlines);
349 /****************************************************************************
350 Load a pipe into memory and return an array of pointers to lines in the data
351 must be freed with file_lines_free().
352 ****************************************************************************/
354 char **file_lines_pload(const char *syscmd, int *numlines)
356 char *p;
357 size_t size;
359 p = file_pload(syscmd, &size);
360 if (!p) {
361 return NULL;
364 return file_lines_parse(p, size, numlines);
367 /****************************************************************************
368 Free lines loaded with file_lines_load.
369 ****************************************************************************/
371 void file_lines_free(char **lines)
373 if (!lines) {
374 return;
376 SAFE_FREE(lines[0]);
377 SAFE_FREE(lines);
380 /****************************************************************************
381 Take a list of lines and modify them to produce a list where \ continues
382 a line.
383 ****************************************************************************/
385 void file_lines_slashcont(char **lines)
387 int i, j;
389 for (i=0; lines[i];) {
390 int len = strlen(lines[i]);
391 if (lines[i][len-1] == '\\') {
392 lines[i][len-1] = ' ';
393 if (lines[i+1]) {
394 char *p = &lines[i][len];
395 while (p < lines[i+1]) {
396 *p++ = ' ';
398 for (j = i+1; lines[j]; j++) {
399 lines[j] = lines[j+1];
402 } else {
403 i++;
408 /****************************************************************************
409 Save a lump of data into a file. Mostly used for debugging.
410 ****************************************************************************/
412 bool file_save(const char *fname, void *packet, size_t length)
414 int fd;
415 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
416 if (fd == -1) {
417 return False;
419 if (write(fd, packet, length) != (size_t)length) {
420 return False;
422 close(fd);
423 return True;