2 * Unix SMB/CIFS implementation.
3 * SMB parameters and setup
4 * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
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)
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
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.
25 /***************************************************************
26 Signal function to tell us we timed out.
27 ****************************************************************/
29 static void gotalarm_sig(void)
34 /***************************************************************
35 Lock or unlock a fd for a known lock type. Abandon after waitsecs
37 ****************************************************************/
39 BOOL do_file_lock(int fd, int waitsecs, int type)
41 SMB_STRUCT_FLOCK lock;
43 void (*oldsig_handler)(int);
46 oldsig_handler = CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
49 lock.l_whence = SEEK_SET;
55 /* Note we must *NOT* use sys_fcntl here ! JRA */
56 ret = fcntl(fd, SMB_F_SETLKW, &lock);
58 CatchSignal(SIGALRM, SIGNAL_CAST oldsig_handler);
61 DEBUG(0, ("do_file_lock: failed to %s file.\n",
62 type == F_UNLCK ? "unlock" : "lock"));
70 /***************************************************************
71 Lock an fd. Abandon after waitsecs seconds.
72 ****************************************************************/
74 BOOL file_lock(int fd, int type, int secs, int *plock_depth)
81 if ((*plock_depth) == 0)
83 if (!do_file_lock(fd, secs, type)) {
84 DEBUG(10,("file_lock: locking file failed, error = %s.\n",
93 /***************************************************************
94 Unlock an fd. Abandon after waitsecs seconds.
95 ****************************************************************/
97 BOOL file_unlock(int fd, int *plock_depth)
101 if(*plock_depth == 1)
102 ret = do_file_lock(fd, 5, F_UNLCK);
107 DEBUG(10,("file_unlock: unlocking file failed, error = %s.\n",
113 /*************************************************************************
114 gets a line out of a file.
115 line is of format "xxxx:xxxxxx:xxxxx:".
116 lines with "#" at the front are ignored.
117 *************************************************************************/
118 int getfileline(void *vp, char *linebuf, int linebuf_size)
120 /* Static buffers we will return. */
121 FILE *fp = (FILE *)vp;
128 DEBUG(0,("getfileline: Bad file pointer.\n"));
133 * Scan the file, a line at a time.
139 fgets(linebuf, linebuf_size, fp);
146 * Check if the string is terminated with a newline - if not
147 * then we must keep reading and discard until we get one.
150 linebuf_len = strlen(linebuf);
151 if (linebuf_len == 0)
157 if (linebuf[linebuf_len - 1] != '\n')
160 while (!ferror(fp) && !feof(fp))
171 linebuf[linebuf_len - 1] = '\0';
174 #ifdef DEBUG_PASSWORD
175 DEBUG(100, ("getfileline: got line |%s|\n", linebuf));
177 if ((linebuf[0] == 0) && feof(fp))
179 DEBUG(4, ("getfileline: end of file reached\n"));
183 if (linebuf[0] == '#' || linebuf[0] == '\0')
185 DEBUG(6, ("getfileline: skipping comment or blank line\n"));
189 p = (unsigned char *) strchr_m(linebuf, ':');
192 DEBUG(0, ("getfileline: malformed line entry (no :)\n"));
201 /****************************************************************************
202 read a line from a file with possible \ continuation chars.
203 Blanks at the start or end of a line are stripped.
204 The string will be allocated if s2 is NULL
205 ****************************************************************************/
206 char *fgets_slash(char *s2,int maxlen,XFILE *f)
211 BOOL start_of_line = True;
216 if (maxlen <2) return(NULL);
220 maxlen = MIN(maxlen,8);
221 s = (char *)malloc(maxlen);
224 if (!s) return(NULL);
228 while (len < maxlen-1)
236 while (len > 0 && s[len-1] == ' ')
240 if (len > 0 && s[len-1] == '\\')
243 start_of_line = True;
250 return(len>0?s:NULL);
255 start_of_line = False;
259 if (!s2 && len > maxlen-3)
264 t = (char *)Realloc(s,maxlen);
266 DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
276 /****************************************************************************
277 load from a pipe into memory
278 ****************************************************************************/
279 char *file_pload(char *syscmd, size_t *size)
286 fd = sys_popen(syscmd);
287 if (fd == -1) return NULL;
292 while ((n = read(fd, buf, sizeof(buf))) > 0) {
293 tp = Realloc(p, total + n + 1);
295 DEBUG(0,("file_pload: failed to expand buffer!\n"));
300 memcpy(p+total, buf, n);
305 /* FIXME: Perhaps ought to check that the command completed
306 * successfully (returned 0); if not the data may be
310 if (size) *size = total;
315 /****************************************************************************
316 load a file into memory from a fd.
317 ****************************************************************************/
319 char *fd_load(int fd, size_t *size)
321 SMB_STRUCT_STAT sbuf;
324 if (sys_fstat(fd, &sbuf) != 0) return NULL;
326 p = (char *)malloc(sbuf.st_size+1);
329 if (read(fd, p, sbuf.st_size) != sbuf.st_size) {
335 if (size) *size = sbuf.st_size;
340 /****************************************************************************
341 load a file into memory
342 ****************************************************************************/
343 char *file_load(const char *fname, size_t *size)
348 if (!fname || !*fname) return NULL;
350 fd = open(fname,O_RDONLY);
351 if (fd == -1) return NULL;
353 p = fd_load(fd, size);
361 /*******************************************************************
362 mmap (if possible) or read a file
363 ********************************************************************/
364 void *map_file(char *fname, size_t size)
371 fd = open(fname, O_RDONLY, 0);
373 DEBUG(2,("Failed to load %s - %s\n", fname, strerror(errno)));
376 p = mmap(NULL, size, PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
378 if (p == MAP_FAILED) {
379 DEBUG(1,("Failed to mmap %s - %s\n", fname, strerror(errno)));
385 p = file_load(fname, &s2);
388 DEBUG(1,("incorrect size for %s - got %d expected %d\n",
399 /****************************************************************************
400 parse a buffer into lines
401 ****************************************************************************/
402 static char **file_lines_parse(char *p, size_t size, int *numlines)
409 for (s = p, i=0; s < p+size; s++) {
410 if (s[0] == '\n') i++;
413 ret = (char **)malloc(sizeof(ret[0])*(i+2));
418 memset(ret, 0, sizeof(ret[0])*(i+2));
419 if (numlines) *numlines = i;
422 for (s = p, i=0; s < p+size; s++) {
428 if (s[0] == '\r') s[0] = 0;
435 /****************************************************************************
436 load a file into memory and return an array of pointers to lines in the file
437 must be freed with file_lines_free().
438 ****************************************************************************/
439 char **file_lines_load(const char *fname, int *numlines)
444 p = file_load(fname, &size);
447 return file_lines_parse(p, size, numlines);
450 /****************************************************************************
451 load a fd into memory and return an array of pointers to lines in the file
452 must be freed with file_lines_free(). If convert is true calls unix_to_dos on
454 ****************************************************************************/
455 char **fd_lines_load(int fd, int *numlines)
460 p = fd_load(fd, &size);
463 return file_lines_parse(p, size, numlines);
467 /****************************************************************************
468 load a pipe into memory and return an array of pointers to lines in the data
469 must be freed with file_lines_free().
470 ****************************************************************************/
471 char **file_lines_pload(char *syscmd, int *numlines)
476 p = file_pload(syscmd, &size);
479 return file_lines_parse(p, size, numlines);
482 /****************************************************************************
483 free lines loaded with file_lines_load
484 ****************************************************************************/
485 void file_lines_free(char **lines)
493 /****************************************************************************
494 take a lislist of lines and modify them to produce a list where \ continues
496 ****************************************************************************/
497 void file_lines_slashcont(char **lines)
501 for (i=0; lines[i];) {
502 int len = strlen(lines[i]);
503 if (lines[i][len-1] == '\\') {
504 lines[i][len-1] = ' ';
506 char *p = &lines[i][len];
507 while (p < lines[i+1]) *p++ = ' ';
508 for (j = i+1; lines[j]; j++) lines[j] = lines[j+1];
517 save a lump of data into a file. Mostly used for debugging
519 BOOL file_save(const char *fname, void *packet, size_t length)
522 fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
526 if (write(fd, packet, length) != (size_t)length) {