r23801: The FSF has moved around a lot. This fixes their Mass Ave address.
[sfrench/samba-autobuild/.git] / source / lib / util_file.c
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.
10  * 
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.
15  * 
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/>.
18  */
19
20 #include "includes.h"
21
22 #ifndef MAP_FAILED
23 #define MAP_FAILED ((void *)-1)
24 #endif
25
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 ****************************************************************************/
31
32 char *fgets_slash(char *s2,int maxlen,XFILE *f)
33 {
34         char *s=s2;
35         int len = 0;
36         int c;
37         BOOL start_of_line = True;
38
39         if (x_feof(f)) {
40                 return(NULL);
41         }
42
43         if (maxlen <2) {
44                 return(NULL);
45         }
46
47         if (!s2) {
48                 maxlen = MIN(maxlen,8);
49                 s = (char *)SMB_MALLOC(maxlen);
50         }
51
52         if (!s) {
53                 return(NULL);
54         }
55
56         *s = 0;
57
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;
66                                 }
67                                 if (len > 0 && s[len-1] == '\\') {
68                                         s[--len] = 0;
69                                         start_of_line = True;
70                                         break;
71                                 }
72                                 return(s);
73                         case EOF:
74                                 if (len <= 0 && !s2)  {
75                                         SAFE_FREE(s);
76                                 }
77                                 return(len>0?s:NULL);
78                         case ' ':
79                                 if (start_of_line) {
80                                         break;
81                                 }
82                         default:
83                                 start_of_line = False;
84                                 s[len++] = c;
85                                 s[len] = 0;
86                 }
87
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);
94                         }
95                 }
96         }
97         return(s);
98 }
99
100 /****************************************************************************
101  Load from a pipe into memory.
102 ****************************************************************************/
103
104 static char *file_pload(char *syscmd, size_t *size)
105 {
106         int fd, n;
107         char *p;
108         pstring buf;
109         size_t total;
110         
111         fd = sys_popen(syscmd);
112         if (fd == -1) {
113                 return NULL;
114         }
115
116         p = NULL;
117         total = 0;
118
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;
125                 }
126                 memcpy(p+total, buf, n);
127                 total += n;
128         }
129
130         if (p) {
131                 p[total] = 0;
132         }
133
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);
138
139         if (size) {
140                 *size = total;
141         }
142
143         return p;
144 }
145
146 /****************************************************************************
147  Load a file into memory from a fd.
148  Truncate at maxsize. If maxsize == 0 - no limit.
149 ****************************************************************************/ 
150
151 char *fd_load(int fd, size_t *psize, size_t maxsize)
152 {
153         SMB_STRUCT_STAT sbuf;
154         size_t size;
155         char *p;
156
157         if (sys_fstat(fd, &sbuf) != 0) {
158                 return NULL;
159         }
160
161         size = sbuf.st_size;
162         if (maxsize) {
163                 size = MIN(size, maxsize);
164         }
165
166         p = (char *)SMB_MALLOC(size+1);
167         if (!p) {
168                 return NULL;
169         }
170
171         if (read(fd, p, size) != size) {
172                 SAFE_FREE(p);
173                 return NULL;
174         }
175         p[size] = 0;
176
177         if (psize) {
178                 *psize = size;
179         }
180
181         return p;
182 }
183
184 /****************************************************************************
185  Load a file into memory.
186 ****************************************************************************/
187
188 char *file_load(const char *fname, size_t *size, size_t maxsize)
189 {
190         int fd;
191         char *p;
192
193         if (!fname || !*fname) {
194                 return NULL;
195         }
196         
197         fd = open(fname,O_RDONLY);
198         if (fd == -1) {
199                 return NULL;
200         }
201
202         p = fd_load(fd, size, maxsize);
203         close(fd);
204         return p;
205 }
206
207 /*******************************************************************
208  unmap or free memory
209 *******************************************************************/
210
211 BOOL unmap_file(void* start, size_t size)
212 {
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;
219         }
220         return True;
221 #else
222         SAFE_FREE( start );
223         return True;
224 #endif
225 }
226
227 /*******************************************************************
228  mmap (if possible) or read a file.
229 ********************************************************************/
230
231 void *map_file(char *fname, size_t size)
232 {
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;
241         }
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;
247         }
248 #endif
249         if (!p) {
250                 p = file_load(fname, &s2, 0);
251                 if (!p) {
252                         return NULL;
253                 }
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;
259                 }
260         }
261         return p;
262 }
263
264 /****************************************************************************
265  Parse a buffer into lines.
266 ****************************************************************************/
267
268 static char **file_lines_parse(char *p, size_t size, int *numlines)
269 {
270         int i;
271         char *s, **ret;
272
273         if (!p) {
274                 return NULL;
275         }
276
277         for (s = p, i=0; s < p+size; s++) {
278                 if (s[0] == '\n') i++;
279         }
280
281         ret = SMB_MALLOC_ARRAY(char *, i+2);
282         if (!ret) {
283                 SAFE_FREE(p);
284                 return NULL;
285         }       
286         memset(ret, 0, sizeof(ret[0])*(i+2));
287
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;
294                 }
295                 if (s[0] == '\r') {
296                         s[0] = 0;
297                 }
298         }
299
300         /* remove any blank lines at the end */
301         while (i > 0 && ret[i-1][0] == 0) {
302                 i--;
303         }
304
305         if (numlines) {
306                 *numlines = i;
307         }
308
309         return ret;
310 }
311
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 ****************************************************************************/
316
317 char **file_lines_load(const char *fname, int *numlines, size_t maxsize)
318 {
319         char *p;
320         size_t size = 0;
321
322         p = file_load(fname, &size, maxsize);
323         if (!p) {
324                 return NULL;
325         }
326
327         return file_lines_parse(p, size, numlines);
328 }
329
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 ****************************************************************************/
335
336 char **fd_lines_load(int fd, int *numlines, size_t maxsize)
337 {
338         char *p;
339         size_t size;
340
341         p = fd_load(fd, &size, maxsize);
342         if (!p) {
343                 return NULL;
344         }
345
346         return file_lines_parse(p, size, numlines);
347 }
348
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 ****************************************************************************/
353
354 char **file_lines_pload(char *syscmd, int *numlines)
355 {
356         char *p;
357         size_t size;
358
359         p = file_pload(syscmd, &size);
360         if (!p) {
361                 return NULL;
362         }
363
364         return file_lines_parse(p, size, numlines);
365 }
366
367 /****************************************************************************
368  Free lines loaded with file_lines_load.
369 ****************************************************************************/
370
371 void file_lines_free(char **lines)
372 {
373         if (!lines) {
374                 return;
375         }
376         SAFE_FREE(lines[0]);
377         SAFE_FREE(lines);
378 }
379
380 /****************************************************************************
381  Take a list of lines and modify them to produce a list where \ continues
382  a line.
383 ****************************************************************************/
384
385 void file_lines_slashcont(char **lines)
386 {
387         int i, j;
388
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++ = ' ';
397                                 }
398                                 for (j = i+1; lines[j]; j++) {
399                                         lines[j] = lines[j+1];
400                                 }
401                         }
402                 } else {
403                         i++;
404                 }
405         }
406 }
407
408 /****************************************************************************
409  Save a lump of data into a file. Mostly used for debugging.
410 ****************************************************************************/
411
412 BOOL file_save(const char *fname, void *packet, size_t length)
413 {
414         int fd;
415         fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
416         if (fd == -1) {
417                 return False;
418         }
419         if (write(fd, packet, length) != (size_t)length) {
420                 return False;
421         }
422         close(fd);
423         return True;
424 }