r13752: Add doxyfile and fix formatting of comments. Current output is available...
[jelmer/samba4-debian.git] / source / lib / util / 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  * Added afdgets() Jelmer Vernooij 2005
7  * 
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)
11  * any later version.
12  * 
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
16  * more details.
17  * 
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.
21  */
22
23 #include "includes.h"
24 #include "system/shmem.h"
25 #include "system/filesys.h"
26
27 /**
28  * @file
29  * @brief File-related utility functions
30  */
31
32 /**
33 read a line from a file with possible \ continuation chars. 
34 Blanks at the start or end of a line are stripped.
35 The string will be allocated if s2 is NULL
36 **/
37 char *fgets_slash(char *s2,int maxlen,XFILE *f)
38 {
39   char *s=s2;
40   int len = 0;
41   int c;
42   BOOL start_of_line = True;
43
44   if (x_feof(f))
45     return(NULL);
46
47   if (maxlen <2) return(NULL);
48
49   if (!s2)
50     {
51       maxlen = MIN(maxlen,8);
52       s = (char *)malloc(maxlen);
53     }
54
55   if (!s) return(NULL);
56
57   *s = 0;
58
59   while (len < maxlen-1)
60     {
61       c = x_getc(f);
62       switch (c)
63         {
64         case '\r':
65           break;
66         case '\n':
67           while (len > 0 && s[len-1] == ' ')
68             {
69               s[--len] = 0;
70             }
71           if (len > 0 && s[len-1] == '\\')
72             {
73               s[--len] = 0;
74               start_of_line = True;
75               break;
76             }
77           return(s);
78         case EOF:
79           if (len <= 0 && !s2) 
80             SAFE_FREE(s);
81           return(len>0?s:NULL);
82         case ' ':
83           if (start_of_line)
84             break;
85         default:
86           start_of_line = False;
87           s[len++] = c;
88           s[len] = 0;
89         }
90       if (!s2 && len > maxlen-3)
91         {
92           char *t;
93           
94           maxlen *= 2;
95           t = realloc_p(s, char, maxlen);
96           if (!t) {
97             DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
98             SAFE_FREE(s);
99             return(NULL);
100           } else s = t;
101         }
102     }
103   return(s);
104 }
105
106 /**
107  * Read one line (data until next newline or eof) and allocate it 
108  */
109 char *afdgets(int fd, TALLOC_CTX *mem_ctx, size_t hint)
110 {
111         char *data = NULL;
112         ssize_t alloc_size = 0, offset = 0, ret;
113         int p;
114
115         if (hint <= 0) hint = 0x100;
116
117         do {
118                 alloc_size += hint;
119
120                 data = talloc_realloc(mem_ctx, data, char, alloc_size);
121
122                 if (!data)
123                         return NULL;
124
125                 ret = read(fd, data + offset, hint);
126
127                 if (ret == -1) {
128                         talloc_free(data);
129                         return NULL;
130                 }
131
132                 /* Find newline */
133                 for (p = 0; p < ret; p++) {
134                         if (data[offset + p] == '\n')
135                                 break;
136                 }
137
138                 if (p < ret) {
139                         data[offset + p] = '\0';
140
141                         /* Go back to position of newline */
142                         lseek(fd, p - ret + 1, SEEK_CUR);
143                         return data;
144                 }
145
146                 offset += ret;
147
148         } while (ret == hint);
149
150         data[offset] = '\0';
151
152         return data;
153 }
154
155
156 /**
157 load a file into memory from a fd.
158 **/
159
160 char *fd_load(int fd, size_t *size, TALLOC_CTX *mem_ctx)
161 {
162         struct stat sbuf;
163         char *p;
164
165         if (fstat(fd, &sbuf) != 0) return NULL;
166
167         p = (char *)talloc_size(mem_ctx, sbuf.st_size+1);
168         if (!p) return NULL;
169
170         if (read(fd, p, sbuf.st_size) != sbuf.st_size) {
171                 talloc_free(p);
172                 return NULL;
173         }
174         p[sbuf.st_size] = 0;
175
176         if (size) *size = sbuf.st_size;
177
178         return p;
179 }
180
181 /**
182 load a file into memory
183 **/
184 char *file_load(const char *fname, size_t *size, TALLOC_CTX *mem_ctx)
185 {
186         int fd;
187         char *p;
188
189         if (!fname || !*fname) return NULL;
190         
191         fd = open(fname,O_RDONLY);
192         if (fd == -1) return NULL;
193
194         p = fd_load(fd, size, mem_ctx);
195
196         close(fd);
197
198         return p;
199 }
200
201
202 /**
203 mmap (if possible) or read a file
204 **/
205 void *map_file(const char *fname, size_t size)
206 {
207         size_t s2 = 0;
208         void *p = NULL;
209 #ifdef HAVE_MMAP
210         int fd;
211         fd = open(fname, O_RDONLY, 0);
212         if (fd == -1) {
213                 DEBUG(2,("Failed to load %s - %s\n", fname, strerror(errno)));
214                 return NULL;
215         }
216         p = mmap(NULL, size, PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
217         close(fd);
218         if (p == MAP_FAILED) {
219                 DEBUG(1,("Failed to mmap %s - %s\n", fname, strerror(errno)));
220                 return NULL;
221         }
222 #endif
223         if (!p) {
224                 p = file_load(fname, &s2, talloc_autofree_context());
225                 if (!p) return NULL;
226                 if (s2 != size) {
227                         DEBUG(1,("incorrect size for %s - got %d expected %d\n",
228                                  fname, (int)s2, (int)size));
229                         talloc_free(p);
230                         return NULL;
231                 }
232         }
233
234         return p;
235 }
236
237
238 /**
239 parse a buffer into lines
240 **/
241 static char **file_lines_parse(char *p, size_t size, int *numlines, TALLOC_CTX *mem_ctx)
242 {
243         int i;
244         char *s, **ret;
245
246         if (!p) return NULL;
247
248         for (s = p, i=0; s < p+size; s++) {
249                 if (s[0] == '\n') i++;
250         }
251
252         ret = talloc_array(mem_ctx, char *, i+2);
253         if (!ret) {
254                 talloc_free(p);
255                 return NULL;
256         }       
257         
258         talloc_reference(ret, p);
259         
260         memset(ret, 0, sizeof(ret[0])*(i+2));
261         if (numlines) *numlines = i;
262
263         ret[0] = p;
264         for (s = p, i=0; s < p+size; s++) {
265                 if (s[0] == '\n') {
266                         s[0] = 0;
267                         i++;
268                         ret[i] = s+1;
269                 }
270                 if (s[0] == '\r') s[0] = 0;
271         }
272
273         return ret;
274 }
275
276
277 /**
278 load a file into memory and return an array of pointers to lines in the file
279 must be freed with talloc_free(). 
280 **/
281 char **file_lines_load(const char *fname, int *numlines, TALLOC_CTX *mem_ctx)
282 {
283         char *p;
284         char **lines;
285         size_t size;
286
287         p = file_load(fname, &size, mem_ctx);
288         if (!p) return NULL;
289
290         lines = file_lines_parse(p, size, numlines, mem_ctx);
291
292         talloc_free(p);
293
294         return lines;
295 }
296
297 /**
298 load a fd into memory and return an array of pointers to lines in the file
299 must be freed with talloc_free(). If convert is true calls unix_to_dos on
300 the list.
301 **/
302 char **fd_lines_load(int fd, int *numlines, TALLOC_CTX *mem_ctx)
303 {
304         char *p;
305         char **lines;
306         size_t size;
307
308         p = fd_load(fd, &size, mem_ctx);
309         if (!p) return NULL;
310
311         lines = file_lines_parse(p, size, numlines, mem_ctx);
312
313         talloc_free(p);
314
315         return lines;
316 }
317
318
319 /**
320 take a list of lines and modify them to produce a list where \ continues
321 a line
322 **/
323 void file_lines_slashcont(char **lines)
324 {
325         int i, j;
326
327         for (i=0; lines[i];) {
328                 int len = strlen(lines[i]);
329                 if (lines[i][len-1] == '\\') {
330                         lines[i][len-1] = ' ';
331                         if (lines[i+1]) {
332                                 char *p = &lines[i][len];
333                                 while (p < lines[i+1]) *p++ = ' ';
334                                 for (j = i+1; lines[j]; j++) lines[j] = lines[j+1];
335                         }
336                 } else {
337                         i++;
338                 }
339         }
340 }
341
342 /**
343   save a lump of data into a file. Mostly used for debugging 
344 */
345 BOOL file_save(const char *fname, const void *packet, size_t length)
346 {
347         int fd;
348         fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
349         if (fd == -1) {
350                 return False;
351         }
352         if (write(fd, packet, length) != (size_t)length) {
353                 return False;
354         }
355         close(fd);
356         return True;
357 }
358
359 /**
360   see if a file exists
361 */
362 BOOL file_exists(const char *path)
363 {
364         struct stat st;
365         return (stat(path, &st) == 0);
366 }
367
368 int vfdprintf(int fd, const char *format, va_list ap) _PRINTF_ATTRIBUTE(2,0)
369 {
370         char *p;
371         int len, ret;
372         va_list ap2;
373
374         VA_COPY(ap2, ap);
375
376         len = vasprintf(&p, format, ap2);
377         if (len <= 0) return len;
378         ret = write(fd, p, len);
379         SAFE_FREE(p);
380         return ret;
381 }
382
383 int fdprintf(int fd, const char *format, ...) _PRINTF_ATTRIBUTE(2,3)
384 {
385         va_list ap;
386         int ret;
387
388         va_start(ap, format);
389         ret = vfdprintf(fd, format, ap);
390         va_end(ap);
391         return ret;
392 }