reran autoconf
[Samba.git] / source / lib / util_file.c
blob45f4f0a0b765c5154d7ae3e9812563c8cd044aa2
1 /*
2 * Unix SMB/Netbios implementation. Version 1.9. SMB parameters and setup
3 * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 675
17 * Mass Ave, Cambridge, MA 02139, USA.
20 #include "includes.h"
22 static int gotalarm;
24 /***************************************************************
25 Signal function to tell us we timed out.
26 ****************************************************************/
28 static void gotalarm_sig(void)
30 gotalarm = 1;
33 /***************************************************************
34 Lock or unlock a fd for a known lock type. Abandon after waitsecs
35 seconds.
36 ****************************************************************/
38 BOOL do_file_lock(int fd, int waitsecs, int type)
40 SMB_STRUCT_FLOCK lock;
41 int ret;
43 gotalarm = 0;
44 CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
46 lock.l_type = type;
47 lock.l_whence = SEEK_SET;
48 lock.l_start = 0;
49 lock.l_len = 1;
50 lock.l_pid = 0;
52 alarm(waitsecs);
53 /* Note we must *NOT* use sys_fcntl here ! JRA */
54 ret = fcntl(fd, SMB_F_SETLKW, &lock);
55 alarm(0);
56 CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN);
58 if (gotalarm) {
59 DEBUG(0, ("do_file_lock: failed to %s file.\n",
60 type == F_UNLCK ? "unlock" : "lock"));
61 return False;
64 return (ret == 0);
68 /***************************************************************
69 Lock an fd. Abandon after waitsecs seconds.
70 ****************************************************************/
72 BOOL file_lock(int fd, int type, int secs, int *plock_depth)
74 if (fd < 0)
75 return False;
77 (*plock_depth)++;
79 if ((*plock_depth) == 0)
81 if (!do_file_lock(fd, secs, type)) {
82 DEBUG(10,("file_lock: locking file failed, error = %s.\n",
83 strerror(errno)));
84 return False;
88 return True;
91 /***************************************************************
92 Unlock an fd. Abandon after waitsecs seconds.
93 ****************************************************************/
95 BOOL file_unlock(int fd, int *plock_depth)
97 BOOL ret=True;
99 if(*plock_depth == 1)
100 ret = do_file_lock(fd, 5, F_UNLCK);
102 (*plock_depth)--;
104 if(!ret)
105 DEBUG(10,("file_unlock: unlocking file failed, error = %s.\n",
106 strerror(errno)));
107 return ret;
110 /***************************************************************
111 locks a file for enumeration / modification.
112 update to be set = True if modification is required.
113 ****************************************************************/
115 void *startfilepwent(char *pfile, char *s_readbuf, int bufsize,
116 int *file_lock_depth, BOOL update)
118 FILE *fp = NULL;
120 if (!*pfile)
122 DEBUG(0, ("startfilepwent: No file set\n"));
123 return (NULL);
125 DEBUG(10, ("startfilepwent: opening file %s\n", pfile));
127 fp = sys_fopen(pfile, update ? "r+b" : "rb");
129 if (fp == NULL) {
130 DEBUG(0, ("startfilepwent: unable to open file %s\n", pfile));
131 return NULL;
134 /* Set a buffer to do more efficient reads */
135 setvbuf(fp, s_readbuf, _IOFBF, bufsize);
137 if (!file_lock(fileno(fp), (update ? F_WRLCK : F_RDLCK), 5, file_lock_depth))
139 DEBUG(0, ("startfilepwent: unable to lock file %s\n", pfile));
140 fclose(fp);
141 return NULL;
144 /* Make sure it is only rw by the owner */
145 chmod(pfile, 0600);
147 /* We have a lock on the file. */
148 return (void *)fp;
151 /***************************************************************
152 End enumeration of the file.
153 ****************************************************************/
154 void endfilepwent(void *vp, int *file_lock_depth)
156 FILE *fp = (FILE *)vp;
158 file_unlock(fileno(fp), file_lock_depth);
159 fclose(fp);
160 DEBUG(7, ("endfilepwent: closed file.\n"));
163 /*************************************************************************
164 Return the current position in the file list as an SMB_BIG_UINT.
165 This must be treated as an opaque token.
166 *************************************************************************/
167 SMB_BIG_UINT getfilepwpos(void *vp)
169 return (SMB_BIG_UINT)sys_ftell((FILE *)vp);
172 /*************************************************************************
173 Set the current position in the file list from an SMB_BIG_UINT.
174 This must be treated as an opaque token.
175 *************************************************************************/
176 BOOL setfilepwpos(void *vp, SMB_BIG_UINT tok)
178 return !sys_fseek((FILE *)vp, (SMB_OFF_T)tok, SEEK_SET);
181 /*************************************************************************
182 gets a line out of a file.
183 line is of format "xxxx:xxxxxx:xxxxx:".
184 lines with "#" at the front are ignored.
185 *************************************************************************/
186 int getfileline(void *vp, char *linebuf, int linebuf_size)
188 /* Static buffers we will return. */
189 FILE *fp = (FILE *)vp;
190 unsigned char c;
191 unsigned char *p;
192 size_t linebuf_len;
194 if (fp == NULL)
196 DEBUG(0,("getfileline: Bad file pointer.\n"));
197 return -1;
201 * Scan the file, a line at a time.
203 while (!feof(fp))
205 linebuf[0] = '\0';
207 fgets(linebuf, linebuf_size, fp);
208 if (ferror(fp))
210 return -1;
214 * Check if the string is terminated with a newline - if not
215 * then we must keep reading and discard until we get one.
218 linebuf_len = strlen(linebuf);
219 if (linebuf_len == 0)
221 linebuf[0] = '\0';
222 return 0;
225 if (linebuf[linebuf_len - 1] != '\n')
227 c = '\0';
228 while (!ferror(fp) && !feof(fp))
230 c = fgetc(fp);
231 if (c == '\n')
233 break;
237 else
239 linebuf[linebuf_len - 1] = '\0';
242 #ifdef DEBUG_PASSWORD
243 DEBUG(100, ("getfileline: got line |%s|\n", linebuf));
244 #endif
245 if ((linebuf[0] == 0) && feof(fp))
247 DEBUG(4, ("getfileline: end of file reached\n"));
248 return 0;
251 if (linebuf[0] == '#' || linebuf[0] == '\0')
253 DEBUG(6, ("getfileline: skipping comment or blank line\n"));
254 continue;
257 p = (unsigned char *) strchr(linebuf, ':');
258 if (p == NULL)
260 DEBUG(0, ("getfileline: malformed line entry (no :)\n"));
261 continue;
263 return linebuf_len;
265 return -1;
269 /****************************************************************************
270 read a line from a file with possible \ continuation chars.
271 Blanks at the start or end of a line are stripped.
272 The string will be allocated if s2 is NULL
273 ****************************************************************************/
274 char *fgets_slash(char *s2,int maxlen,FILE *f)
276 char *s=s2;
277 int len = 0;
278 int c;
279 BOOL start_of_line = True;
281 if (feof(f))
282 return(NULL);
284 if (maxlen <2) return(NULL);
286 if (!s2)
288 char *t;
290 maxlen = MIN(maxlen,8);
291 t = (char *)Realloc(s,maxlen);
292 if (!t) {
293 DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
294 SAFE_FREE(s);
295 return(NULL);
296 } else
297 s = t;
300 if (!s)
301 return(NULL);
303 *s = 0;
305 while (len < maxlen-1)
307 c = getc(f);
308 switch (c)
310 case '\r':
311 break;
312 case '\n':
313 while (len > 0 && s[len-1] == ' ')
315 s[--len] = 0;
317 if (len > 0 && s[len-1] == '\\')
319 s[--len] = 0;
320 start_of_line = True;
321 break;
323 return(s);
324 case EOF:
325 if (len <= 0 && !s2)
326 SAFE_FREE(s);
327 return(len>0?s:NULL);
328 case ' ':
329 if (start_of_line)
330 break;
331 default:
332 start_of_line = False;
333 s[len++] = c;
334 s[len] = 0;
336 if (!s2 && len > maxlen-3) {
337 char *t;
339 maxlen *= 2;
340 t = (char *)Realloc(s,maxlen);
341 if (!t) {
342 DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
343 SAFE_FREE(s);
344 return(NULL);
345 } else
346 s = t;
349 return(s);
353 /****************************************************************************
354 load from a pipe into memory
355 ****************************************************************************/
356 char *file_pload(const char *syscmd, size_t *size)
358 int fd, n;
359 char *p, *tp;
360 pstring buf;
361 size_t total;
363 fd = sys_popen(syscmd);
364 if (fd == -1) return NULL;
366 p = NULL;
367 total = 0;
369 while ((n = read(fd, buf, sizeof(buf))) > 0) {
370 tp = Realloc(p, total + n + 1);
371 if (!tp) {
372 DEBUG(0,("file_pload: failed to exand buffer!\n"));
373 close(fd);
374 SAFE_FREE(p);
375 return NULL;
376 } else
377 p = tp;
378 memcpy(p+total, buf, n);
379 total += n;
381 if (p) p[total] = 0;
383 sys_pclose(fd);
385 if (size) *size = total;
387 return p;
390 /****************************************************************************
391 load a file into memory from a fd.
392 ****************************************************************************/
394 char *fd_load(int fd, size_t *size)
396 SMB_STRUCT_STAT sbuf;
397 char *p;
399 if (sys_fstat(fd, &sbuf) != 0) return NULL;
401 p = (char *)malloc(sbuf.st_size+1);
402 if (!p) return NULL;
404 if (read(fd, p, sbuf.st_size) != sbuf.st_size) {
405 SAFE_FREE(p);
406 return NULL;
408 p[sbuf.st_size] = 0;
410 if (size) *size = sbuf.st_size;
412 return p;
415 /****************************************************************************
416 load a file into memory
417 ****************************************************************************/
418 char *file_load(const char *fname, size_t *size)
420 int fd;
421 char *p;
423 if (!fname || !*fname) return NULL;
425 fd = open(fname,O_RDONLY);
426 if (fd == -1) return NULL;
428 p = fd_load(fd, size);
430 close(fd);
432 return p;
436 /****************************************************************************
437 parse a buffer into lines
438 ****************************************************************************/
439 static char **file_lines_parse(char *p, size_t size, int *numlines, BOOL convert)
441 int i;
442 char *s, **ret;
444 if (!p) return NULL;
446 for (s = p, i=0; s < p+size; s++) {
447 if (s[0] == '\n') i++;
450 ret = (char **)malloc(sizeof(ret[0])*(i+2));
451 if (!ret) {
452 SAFE_FREE(p);
453 return NULL;
455 memset(ret, 0, sizeof(ret[0])*(i+2));
456 if (numlines) *numlines = i;
458 ret[0] = p;
459 for (s = p, i=0; s < p+size; s++) {
460 if (s[0] == '\n') {
461 s[0] = 0;
462 i++;
463 ret[i] = s+1;
465 if (s[0] == '\r') s[0] = 0;
468 if (convert) {
469 for (i = 0; ret[i]; i++)
470 unix_to_dos(ret[i]);
473 return ret;
477 /****************************************************************************
478 load a file into memory and return an array of pointers to lines in the file
479 must be freed with file_lines_free(). If convert is true calls unix_to_dos on
480 the list.
481 ****************************************************************************/
482 char **file_lines_load(const char *fname, int *numlines, BOOL convert)
484 char *p;
485 size_t size;
487 p = file_load(fname, &size);
488 if (!p) return NULL;
490 return file_lines_parse(p, size, numlines, convert);
493 /****************************************************************************
494 load a fd into memory and return an array of pointers to lines in the file
495 must be freed with file_lines_free(). If convert is true calls unix_to_dos on
496 the list.
497 ****************************************************************************/
498 char **fd_lines_load(int fd, int *numlines, BOOL convert)
500 char *p;
501 size_t size;
503 p = fd_load(fd, &size);
504 if (!p) return NULL;
506 return file_lines_parse(p, size, numlines, convert);
510 /****************************************************************************
511 load a pipe into memory and return an array of pointers to lines in the data
512 must be freed with file_lines_free(). If convert is true calls unix_to_dos on
513 the list.
514 ****************************************************************************/
515 char **file_lines_pload(const char *syscmd, int *numlines, BOOL convert)
517 char *p;
518 size_t size;
520 p = file_pload(syscmd, &size);
521 if (!p) return NULL;
523 return file_lines_parse(p, size, numlines, convert);
526 /****************************************************************************
527 free lines loaded with file_lines_load
528 ****************************************************************************/
529 void file_lines_free(char **lines)
531 if (!lines) return;
532 SAFE_FREE(lines[0]);
533 SAFE_FREE(lines);
537 /****************************************************************************
538 take a lislist of lines and modify them to produce a list where \ continues
539 a line
540 ****************************************************************************/
541 void file_lines_slashcont(char **lines)
543 int i, j;
545 for (i=0; lines[i];) {
546 int len = strlen(lines[i]);
547 if (lines[i][len-1] == '\\') {
548 lines[i][len-1] = ' ';
549 if (lines[i+1]) {
550 char *p = &lines[i][len];
551 while (p < lines[i+1]) *p++ = ' ';
552 for (j = i+1; lines[j]; j++) lines[j] = lines[j+1];
554 } else {
555 i++;