r9746: Add "staff" as possible alternative to wheel
[kai/samba.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 2 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, write to the Free Software Foundation, Inc., 675
18  * Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 #include "includes.h"
22 #include "system/shmem.h"
23 #include "system/filesys.h"
24
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)
31 {
32         /* Static buffers we will return. */
33         FILE *fp = (FILE *)vp;
34         uint8_t   c;
35         uint8_t  *p;
36         size_t            linebuf_len;
37
38         if (fp == NULL)
39         {
40                 DEBUG(0,("getfileline: Bad file pointer.\n"));
41                 return -1;
42         }
43
44         /*
45          * Scan the file, a line at a time.
46          */
47         while (!feof(fp))
48         {
49                 linebuf[0] = '\0';
50
51                 fgets(linebuf, linebuf_size, fp);
52                 if (ferror(fp))
53                 {
54                         return -1;
55                 }
56
57                 /*
58                  * Check if the string is terminated with a newline - if not
59                  * then we must keep reading and discard until we get one.
60                  */
61
62                 linebuf_len = strlen(linebuf);
63                 if (linebuf_len == 0)
64                 {
65                         linebuf[0] = '\0';
66                         return 0;
67                 }
68
69                 if (linebuf[linebuf_len - 1] != '\n')
70                 {
71                         c = '\0';
72                         while (!ferror(fp) && !feof(fp))
73                         {
74                                 c = fgetc(fp);
75                                 if (c == '\n')
76                                 {
77                                         break;
78                                 }
79                         }
80                 }
81                 else
82                 {
83                         linebuf[linebuf_len - 1] = '\0';
84                 }
85
86 #ifdef DEBUG_PASSWORD
87                 DEBUG(100, ("getfileline: got line |%s|\n", linebuf));
88 #endif
89                 if ((linebuf[0] == 0) && feof(fp))
90                 {
91                         DEBUG(4, ("getfileline: end of file reached\n"));
92                         return 0;
93                 }
94
95                 if (linebuf[0] == '#' || linebuf[0] == '\0')
96                 {
97                         DEBUG(6, ("getfileline: skipping comment or blank line\n"));
98                         continue;
99                 }
100
101                 p = (uint8_t *) strchr_m(linebuf, ':');
102                 if (p == NULL)
103                 {
104                         DEBUG(0, ("getfileline: malformed line entry (no :)\n"));
105                         continue;
106                 }
107                 return linebuf_len;
108         }
109         return -1;
110 }
111
112
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)
119 {
120   char *s=s2;
121   int len = 0;
122   int c;
123   BOOL start_of_line = True;
124
125   if (x_feof(f))
126     return(NULL);
127
128   if (maxlen <2) return(NULL);
129
130   if (!s2)
131     {
132       maxlen = MIN(maxlen,8);
133       s = (char *)malloc(maxlen);
134     }
135
136   if (!s) return(NULL);
137
138   *s = 0;
139
140   while (len < maxlen-1)
141     {
142       c = x_getc(f);
143       switch (c)
144         {
145         case '\r':
146           break;
147         case '\n':
148           while (len > 0 && s[len-1] == ' ')
149             {
150               s[--len] = 0;
151             }
152           if (len > 0 && s[len-1] == '\\')
153             {
154               s[--len] = 0;
155               start_of_line = True;
156               break;
157             }
158           return(s);
159         case EOF:
160           if (len <= 0 && !s2) 
161             SAFE_FREE(s);
162           return(len>0?s:NULL);
163         case ' ':
164           if (start_of_line)
165             break;
166         default:
167           start_of_line = False;
168           s[len++] = c;
169           s[len] = 0;
170         }
171       if (!s2 && len > maxlen-3)
172         {
173           char *t;
174           
175           maxlen *= 2;
176           t = realloc_p(s, char, maxlen);
177           if (!t) {
178             DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
179             SAFE_FREE(s);
180             return(NULL);
181           } else s = t;
182         }
183     }
184   return(s);
185 }
186
187
188
189 /****************************************************************************
190 load a file into memory from a fd.
191 ****************************************************************************/ 
192
193 char *fd_load(int fd, size_t *size, TALLOC_CTX *mem_ctx)
194 {
195         struct stat sbuf;
196         char *p;
197
198         if (fstat(fd, &sbuf) != 0) return NULL;
199
200         p = (char *)talloc_size(mem_ctx, sbuf.st_size+1);
201         if (!p) return NULL;
202
203         if (read(fd, p, sbuf.st_size) != sbuf.st_size) {
204                 talloc_free(p);
205                 return NULL;
206         }
207         p[sbuf.st_size] = 0;
208
209         if (size) *size = sbuf.st_size;
210
211         return p;
212 }
213
214 /****************************************************************************
215 load a file into memory
216 ****************************************************************************/
217 char *file_load(const char *fname, size_t *size, TALLOC_CTX *mem_ctx)
218 {
219         int fd;
220         char *p;
221
222         if (!fname || !*fname) return NULL;
223         
224         fd = open(fname,O_RDONLY);
225         if (fd == -1) return NULL;
226
227         p = fd_load(fd, size, mem_ctx);
228
229         close(fd);
230
231         return p;
232 }
233
234
235 /*******************************************************************
236 mmap (if possible) or read a file
237 ********************************************************************/
238 void *map_file(char *fname, size_t size)
239 {
240         size_t s2 = 0;
241         void *p = NULL;
242 #ifdef HAVE_MMAP
243         int fd;
244         fd = open(fname, O_RDONLY, 0);
245         if (fd == -1) {
246                 DEBUG(2,("Failed to load %s - %s\n", fname, strerror(errno)));
247                 return NULL;
248         }
249         p = mmap(NULL, size, PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
250         close(fd);
251         if (p == MAP_FAILED) {
252                 DEBUG(1,("Failed to mmap %s - %s\n", fname, strerror(errno)));
253                 return NULL;
254         }
255 #endif
256         if (!p) {
257                 p = file_load(fname, &s2, talloc_autofree_context());
258                 if (!p) return NULL;
259                 if (s2 != size) {
260                         DEBUG(1,("incorrect size for %s - got %d expected %d\n",
261                                  fname, (int)s2, (int)size));
262                         talloc_free(p);
263                         return NULL;
264                 }
265         }
266
267         return p;
268 }
269
270
271 /****************************************************************************
272 parse a buffer into lines
273 ****************************************************************************/
274 static char **file_lines_parse(char *p, size_t size, int *numlines)
275 {
276         int i;
277         char *s, **ret;
278
279         if (!p) return NULL;
280
281         for (s = p, i=0; s < p+size; s++) {
282                 if (s[0] == '\n') i++;
283         }
284
285         ret = malloc_array_p(char *, i+2);
286         if (!ret) {
287                 SAFE_FREE(p);
288                 return NULL;
289         }       
290         memset(ret, 0, sizeof(ret[0])*(i+2));
291         if (numlines) *numlines = i;
292
293         ret[0] = p;
294         for (s = p, i=0; s < p+size; s++) {
295                 if (s[0] == '\n') {
296                         s[0] = 0;
297                         i++;
298                         ret[i] = s+1;
299                 }
300                 if (s[0] == '\r') s[0] = 0;
301         }
302
303         return ret;
304 }
305
306
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)
312 {
313         char *p;
314         size_t size;
315
316         p = file_load(fname, &size, mem_ctx);
317         if (!p) return NULL;
318
319         return file_lines_parse(p, size, numlines);
320 }
321
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
325 the list.
326 ****************************************************************************/
327 char **fd_lines_load(int fd, int *numlines, TALLOC_CTX *mem_ctx)
328 {
329         char *p;
330         size_t size;
331
332         p = fd_load(fd, &size, mem_ctx);
333         if (!p) return NULL;
334
335         return file_lines_parse(p, size, numlines);
336 }
337
338
339 /****************************************************************************
340 take a lislist of lines and modify them to produce a list where \ continues
341 a line
342 ****************************************************************************/
343 void file_lines_slashcont(char **lines)
344 {
345         int i, j;
346
347         for (i=0; lines[i];) {
348                 int len = strlen(lines[i]);
349                 if (lines[i][len-1] == '\\') {
350                         lines[i][len-1] = ' ';
351                         if (lines[i+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];
355                         }
356                 } else {
357                         i++;
358                 }
359         }
360 }
361
362 /*
363   save a lump of data into a file. Mostly used for debugging 
364 */
365 BOOL file_save(const char *fname, void *packet, size_t length)
366 {
367         int fd;
368         fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
369         if (fd == -1) {
370                 return False;
371         }
372         if (write(fd, packet, length) != (size_t)length) {
373                 return False;
374         }
375         close(fd);
376         return True;
377 }
378
379 /*
380   see if a file exists
381 */
382 BOOL file_exists(const char *path)
383 {
384         struct stat st;
385         return (stat(path, &st) == 0);
386 }