r7372: abartet's patch for BUG 2391 (segv caused by free a static pointer)
[Samba/gbeck.git] / source / lib / util_file.c
blob963d610beffb5737d84e031dbf20b956be112b84
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.
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, write to the Free Software Foundation, Inc., 675
18 * Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
23 #ifndef MAP_FAILED
24 #define MAP_FAILED ((void *)-1)
25 #endif
27 static int gotalarm;
29 /***************************************************************
30 Signal function to tell us we timed out.
31 ****************************************************************/
33 static void gotalarm_sig(void)
35 gotalarm = 1;
38 /***************************************************************
39 Lock or unlock a fd for a known lock type. Abandon after waitsecs
40 seconds.
41 ****************************************************************/
43 BOOL do_file_lock(int fd, int waitsecs, int type)
45 SMB_STRUCT_FLOCK lock;
46 int ret;
47 void (*oldsig_handler)(int);
49 gotalarm = 0;
50 oldsig_handler = CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
52 lock.l_type = type;
53 lock.l_whence = SEEK_SET;
54 lock.l_start = 0;
55 lock.l_len = 1;
56 lock.l_pid = 0;
58 alarm(waitsecs);
59 /* Note we must *NOT* use sys_fcntl here ! JRA */
60 ret = fcntl(fd, SMB_F_SETLKW, &lock);
61 alarm(0);
62 CatchSignal(SIGALRM, SIGNAL_CAST oldsig_handler);
64 if (gotalarm) {
65 DEBUG(0, ("do_file_lock: failed to %s file.\n",
66 type == F_UNLCK ? "unlock" : "lock"));
67 return False;
70 return (ret == 0);
73 /***************************************************************
74 Lock an fd. Abandon after waitsecs seconds.
75 ****************************************************************/
77 BOOL file_lock(int fd, int type, int secs, int *plock_depth)
79 if (fd < 0)
80 return False;
82 (*plock_depth)++;
84 if ((*plock_depth) == 0) {
85 if (!do_file_lock(fd, secs, type)) {
86 DEBUG(10,("file_lock: locking file failed, error = %s.\n", strerror(errno)));
87 return False;
91 return True;
94 /***************************************************************
95 Unlock an fd. Abandon after waitsecs seconds.
96 ****************************************************************/
98 BOOL file_unlock(int fd, int *plock_depth)
100 BOOL ret=True;
102 if(*plock_depth == 1) {
103 ret = do_file_lock(fd, 5, F_UNLCK);
106 (*plock_depth)--;
108 if(!ret) {
109 DEBUG(10,("file_unlock: unlocking file failed, error = %s.\n", strerror(errno)));
111 return ret;
114 /***************************************************************
115 Locks a file for enumeration / modification.
116 update to be set = True if modification is required.
117 ****************************************************************/
119 void *startfilepwent(char *pfile, char *s_readbuf, int bufsize,
120 int *file_lock_depth, BOOL update)
122 FILE *fp = NULL;
124 if (!*pfile) {
125 DEBUG(0, ("startfilepwent: No file set\n"));
126 return (NULL);
128 DEBUG(10, ("startfilepwent: opening file %s\n", pfile));
130 fp = sys_fopen(pfile, update ? "r+b" : "rb");
132 if (fp == NULL) {
133 DEBUG(0, ("startfilepwent: unable to open file %s\n", pfile));
134 return NULL;
137 /* Set a buffer to do more efficient reads */
138 setvbuf(fp, s_readbuf, _IOFBF, bufsize);
140 if (!file_lock(fileno(fp), (update ? F_WRLCK : F_RDLCK), 5, file_lock_depth)) {
141 DEBUG(0, ("startfilepwent: unable to lock file %s\n", pfile));
142 fclose(fp);
143 return NULL;
146 /* Make sure it is only rw by the owner */
147 chmod(pfile, 0600);
149 /* We have a lock on the file. */
150 return (void *)fp;
153 /***************************************************************
154 End enumeration of the file.
155 ****************************************************************/
157 void endfilepwent(void *vp, int *file_lock_depth)
159 FILE *fp = (FILE *)vp;
161 file_unlock(fileno(fp), file_lock_depth);
162 fclose(fp);
163 DEBUG(7, ("endfilepwent: closed file.\n"));
166 /*************************************************************************
167 Return the current position in the file list as an SMB_BIG_UINT.
168 This must be treated as an opaque token.
169 *************************************************************************/
171 SMB_BIG_UINT getfilepwpos(void *vp)
173 return (SMB_BIG_UINT)sys_ftell((FILE *)vp);
176 /*************************************************************************
177 Set the current position in the file list from an SMB_BIG_UINT.
178 This must be treated as an opaque token.
179 *************************************************************************/
181 BOOL setfilepwpos(void *vp, SMB_BIG_UINT tok)
183 return !sys_fseek((FILE *)vp, (SMB_OFF_T)tok, SEEK_SET);
186 /*************************************************************************
187 Gets a line out of a file.
188 line is of format "xxxx:xxxxxx:xxxxx:".
189 lines with "#" at the front are ignored.
190 *************************************************************************/
192 int getfileline(void *vp, char *linebuf, int linebuf_size)
194 /* Static buffers we will return. */
195 FILE *fp = (FILE *)vp;
196 unsigned char c;
197 unsigned char *p;
198 size_t linebuf_len;
200 if (fp == NULL) {
201 DEBUG(0,("getfileline: Bad file pointer.\n"));
202 return -1;
206 * Scan the file, a line at a time.
208 while (!feof(fp)) {
209 linebuf[0] = '\0';
211 fgets(linebuf, linebuf_size, fp);
212 if (ferror(fp)) {
213 return -1;
217 * Check if the string is terminated with a newline - if not
218 * then we must keep reading and discard until we get one.
221 linebuf_len = strlen(linebuf);
222 if (linebuf_len == 0) {
223 linebuf[0] = '\0';
224 return 0;
227 if (linebuf[linebuf_len - 1] != '\n') {
228 c = '\0';
229 while (!ferror(fp) && !feof(fp)) {
230 c = fgetc(fp);
231 if (c == '\n') {
232 break;
235 } else {
236 linebuf[linebuf_len - 1] = '\0';
239 #ifdef DEBUG_PASSWORD
240 DEBUG(100, ("getfileline: got line |%s|\n", linebuf));
241 #endif
242 if ((linebuf[0] == 0) && feof(fp)) {
243 DEBUG(4, ("getfileline: end of file reached\n"));
244 return 0;
247 if (linebuf[0] == '#' || linebuf[0] == '\0') {
248 DEBUG(6, ("getfileline: skipping comment or blank line\n"));
249 continue;
252 p = (unsigned char *) strchr_m(linebuf, ':');
253 if (p == NULL) {
254 DEBUG(0, ("getfileline: malformed line entry (no :)\n"));
255 continue;
257 return linebuf_len;
259 return -1;
262 /****************************************************************************
263 Read a line from a file with possible \ continuation chars.
264 Blanks at the start or end of a line are stripped.
265 The string will be allocated if s2 is NULL.
266 ****************************************************************************/
268 char *fgets_slash(char *s2,int maxlen,XFILE *f)
270 char *s=s2;
271 int len = 0;
272 int c;
273 BOOL start_of_line = True;
275 if (x_feof(f)) {
276 return(NULL);
279 if (maxlen <2) {
280 return(NULL);
283 if (!s2) {
284 maxlen = MIN(maxlen,8);
285 s = (char *)SMB_MALLOC(maxlen);
288 if (!s) {
289 return(NULL);
292 *s = 0;
294 while (len < maxlen-1) {
295 c = x_getc(f);
296 switch (c) {
297 case '\r':
298 break;
299 case '\n':
300 while (len > 0 && s[len-1] == ' ') {
301 s[--len] = 0;
303 if (len > 0 && s[len-1] == '\\') {
304 s[--len] = 0;
305 start_of_line = True;
306 break;
308 return(s);
309 case EOF:
310 if (len <= 0 && !s2) {
311 SAFE_FREE(s);
313 return(len>0?s:NULL);
314 case ' ':
315 if (start_of_line) {
316 break;
318 default:
319 start_of_line = False;
320 s[len++] = c;
321 s[len] = 0;
324 if (!s2 && len > maxlen-3) {
325 char *t;
327 maxlen *= 2;
328 t = (char *)SMB_REALLOC(s,maxlen);
329 if (!t) {
330 DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
331 SAFE_FREE(s);
332 return(NULL);
333 } else {
334 s = t;
338 return(s);
341 /****************************************************************************
342 Load from a pipe into memory.
343 ****************************************************************************/
345 char *file_pload(char *syscmd, size_t *size)
347 int fd, n;
348 char *p, *tp;
349 pstring buf;
350 size_t total;
352 fd = sys_popen(syscmd);
353 if (fd == -1) {
354 return NULL;
357 p = NULL;
358 total = 0;
360 while ((n = read(fd, buf, sizeof(buf))) > 0) {
361 tp = SMB_REALLOC(p, total + n + 1);
362 if (!tp) {
363 DEBUG(0,("file_pload: failed to expand buffer!\n"));
364 close(fd);
365 SAFE_FREE(p);
366 return NULL;
367 } else {
368 p = tp;
370 memcpy(p+total, buf, n);
371 total += n;
373 if (p) p[total] = 0;
375 /* FIXME: Perhaps ought to check that the command completed
376 * successfully (returned 0); if not the data may be
377 * truncated. */
378 sys_pclose(fd);
380 if (size) {
381 *size = total;
384 return p;
387 /****************************************************************************
388 Load a file into memory from a fd.
389 ****************************************************************************/
391 char *fd_load(int fd, size_t *size)
393 SMB_STRUCT_STAT sbuf;
394 char *p;
396 if (sys_fstat(fd, &sbuf) != 0) {
397 return NULL;
400 p = (char *)SMB_MALLOC(sbuf.st_size+1);
401 if (!p) {
402 return NULL;
405 if (read(fd, p, sbuf.st_size) != sbuf.st_size) {
406 SAFE_FREE(p);
407 return NULL;
409 p[sbuf.st_size] = 0;
411 if (size) {
412 *size = sbuf.st_size;
415 return p;
418 /****************************************************************************
419 Load a file into memory.
420 ****************************************************************************/
422 char *file_load(const char *fname, size_t *size)
424 int fd;
425 char *p;
427 if (!fname || !*fname) {
428 return NULL;
431 fd = open(fname,O_RDONLY);
432 if (fd == -1) {
433 return NULL;
436 p = fd_load(fd, size);
437 close(fd);
438 return p;
441 /*******************************************************************
442 mmap (if possible) or read a file.
443 ********************************************************************/
445 void *map_file(char *fname, size_t size)
447 size_t s2 = 0;
448 void *p = NULL;
449 #ifdef HAVE_MMAP
450 int fd;
451 fd = open(fname, O_RDONLY, 0);
452 if (fd == -1) {
453 DEBUG(2,("map_file: Failed to load %s - %s\n", fname, strerror(errno)));
454 return NULL;
456 p = mmap(NULL, size, PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
457 close(fd);
458 if (p == MAP_FAILED) {
459 DEBUG(1,("map_file: Failed to mmap %s - %s\n", fname, strerror(errno)));
460 return NULL;
462 #endif
463 if (!p) {
464 p = file_load(fname, &s2);
465 if (!p) {
466 return NULL;
468 if (s2 != size) {
469 DEBUG(1,("map_file: incorrect size for %s - got %lu expected %lu\n",
470 fname, (unsigned long)s2, (unsigned long)size));
471 SAFE_FREE(p);
472 return NULL;
475 return p;
478 /****************************************************************************
479 Parse a buffer into lines.
480 ****************************************************************************/
482 static char **file_lines_parse(char *p, size_t size, int *numlines)
484 int i;
485 char *s, **ret;
487 if (!p) {
488 return NULL;
491 for (s = p, i=0; s < p+size; s++) {
492 if (s[0] == '\n') i++;
495 ret = SMB_MALLOC_ARRAY(char *, i+2);
496 if (!ret) {
497 SAFE_FREE(p);
498 return NULL;
500 memset(ret, 0, sizeof(ret[0])*(i+2));
501 if (numlines) {
502 *numlines = i;
505 ret[0] = p;
506 for (s = p, i=0; s < p+size; s++) {
507 if (s[0] == '\n') {
508 s[0] = 0;
509 i++;
510 ret[i] = s+1;
512 if (s[0] == '\r') {
513 s[0] = 0;
517 return ret;
520 /****************************************************************************
521 Load a file into memory and return an array of pointers to lines in the file
522 must be freed with file_lines_free().
523 ****************************************************************************/
525 char **file_lines_load(const char *fname, int *numlines)
527 char *p;
528 size_t size;
530 p = file_load(fname, &size);
531 if (!p) {
532 return NULL;
535 return file_lines_parse(p, size, numlines);
538 /****************************************************************************
539 Load a fd into memory and return an array of pointers to lines in the file
540 must be freed with file_lines_free(). If convert is true calls unix_to_dos on
541 the list.
542 ****************************************************************************/
544 char **fd_lines_load(int fd, int *numlines)
546 char *p;
547 size_t size;
549 p = fd_load(fd, &size);
550 if (!p) {
551 return NULL;
554 return file_lines_parse(p, size, numlines);
557 /****************************************************************************
558 Load a pipe into memory and return an array of pointers to lines in the data
559 must be freed with file_lines_free().
560 ****************************************************************************/
562 char **file_lines_pload(char *syscmd, int *numlines)
564 char *p;
565 size_t size;
567 p = file_pload(syscmd, &size);
568 if (!p) {
569 return NULL;
572 return file_lines_parse(p, size, numlines);
575 /****************************************************************************
576 Free lines loaded with file_lines_load.
577 ****************************************************************************/
579 void file_lines_free(char **lines)
581 if (!lines) {
582 return;
584 SAFE_FREE(lines[0]);
585 SAFE_FREE(lines);
588 /****************************************************************************
589 Take a list of lines and modify them to produce a list where \ continues
590 a line.
591 ****************************************************************************/
593 void file_lines_slashcont(char **lines)
595 int i, j;
597 for (i=0; lines[i];) {
598 int len = strlen(lines[i]);
599 if (lines[i][len-1] == '\\') {
600 lines[i][len-1] = ' ';
601 if (lines[i+1]) {
602 char *p = &lines[i][len];
603 while (p < lines[i+1]) {
604 *p++ = ' ';
606 for (j = i+1; lines[j]; j++) {
607 lines[j] = lines[j+1];
610 } else {
611 i++;
616 /****************************************************************************
617 Save a lump of data into a file. Mostly used for debugging.
618 ****************************************************************************/
620 BOOL file_save(const char *fname, void *packet, size_t length)
622 int fd;
623 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
624 if (fd == -1) {
625 return False;
627 if (write(fd, packet, length) != (size_t)length) {
628 return False;
630 close(fd);
631 return True;