/*
- * Unix SMB/Netbios implementation. Version 1.9. SMB parameters and setup
+ * Unix SMB/CIFS implementation.
+ * SMB parameters and setup
* Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
*
* This program is free software; you can redistribute it and/or modify it under
#include "includes.h"
-extern int DEBUGLEVEL;
+#ifndef MAP_FAILED
+#define MAP_FAILED ((void *)-1)
+#endif
+
static int gotalarm;
{
SMB_STRUCT_FLOCK lock;
int ret;
+ void (*oldsig_handler)(int);
gotalarm = 0;
- CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
+ oldsig_handler = CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
lock.l_type = type;
lock.l_whence = SEEK_SET;
lock.l_pid = 0;
alarm(waitsecs);
+ /* Note we must *NOT* use sys_fcntl here ! JRA */
ret = fcntl(fd, SMB_F_SETLKW, &lock);
alarm(0);
- CatchSignal(SIGALRM, SIGNAL_CAST SIG_DFL);
+ CatchSignal(SIGALRM, SIGNAL_CAST oldsig_handler);
if (gotalarm) {
DEBUG(0, ("do_file_lock: failed to %s file.\n",
*/
linebuf_len = strlen(linebuf);
+ if (linebuf_len == 0)
+ {
+ linebuf[0] = '\0';
+ return 0;
+ }
+
if (linebuf[linebuf_len - 1] != '\n')
{
c = '\0';
continue;
}
- p = (unsigned char *) strchr(linebuf, ':');
+ p = (unsigned char *) strchr_m(linebuf, ':');
if (p == NULL)
{
DEBUG(0, ("getfileline: malformed line entry (no :)\n"));
Blanks at the start or end of a line are stripped.
The string will be allocated if s2 is NULL
****************************************************************************/
-char *fgets_slash(char *s2,int maxlen,FILE *f)
+char *fgets_slash(char *s2,int maxlen,XFILE *f)
{
char *s=s2;
int len = 0;
int c;
BOOL start_of_line = True;
- if (feof(f))
+ if (x_feof(f))
return(NULL);
+ if (maxlen <2) return(NULL);
+
if (!s2)
{
maxlen = MIN(maxlen,8);
- s = (char *)Realloc(s,maxlen);
+ s = (char *)malloc(maxlen);
}
- if (!s || maxlen < 2) return(NULL);
+ if (!s) return(NULL);
*s = 0;
while (len < maxlen-1)
{
- c = getc(f);
+ c = x_getc(f);
switch (c)
{
case '\r':
return(s);
case EOF:
if (len <= 0 && !s2)
- free(s);
+ SAFE_FREE(s);
return(len>0?s:NULL);
case ' ':
if (start_of_line)
}
if (!s2 && len > maxlen-3)
{
+ char *t;
+
maxlen *= 2;
- s = (char *)Realloc(s,maxlen);
- if (!s) return(NULL);
+ t = (char *)Realloc(s,maxlen);
+ if (!t) {
+ DEBUG(0,("fgets_slash: failed to expand buffer!\n"));
+ SAFE_FREE(s);
+ return(NULL);
+ } else s = t;
}
}
return(s);
char *file_pload(char *syscmd, size_t *size)
{
int fd, n;
- char *p;
+ char *p, *tp;
pstring buf;
size_t total;
total = 0;
while ((n = read(fd, buf, sizeof(buf))) > 0) {
- p = Realloc(p, total + n + 1);
- if (!p) {
+ tp = Realloc(p, total + n + 1);
+ if (!tp) {
+ DEBUG(0,("file_pload: failed to expand buffer!\n"));
close(fd);
+ SAFE_FREE(p);
return NULL;
- }
+ } else p = tp;
memcpy(p+total, buf, n);
total += n;
}
- p[total] = 0;
+ if (p) p[total] = 0;
+ /* FIXME: Perhaps ought to check that the command completed
+ * successfully (returned 0); if not the data may be
+ * truncated. */
sys_pclose(fd);
if (size) *size = total;
return p;
}
-
/****************************************************************************
-load a file into memory
-****************************************************************************/
-char *file_load(char *fname, size_t *size)
+load a file into memory from a fd.
+****************************************************************************/
+
+char *fd_load(int fd, size_t *size)
{
- int fd;
SMB_STRUCT_STAT sbuf;
char *p;
- if (!fname || !*fname) return NULL;
-
- fd = open(fname,O_RDONLY);
- if (fd == -1) return NULL;
-
if (sys_fstat(fd, &sbuf) != 0) return NULL;
- if (sbuf.st_size == 0) return NULL;
-
p = (char *)malloc(sbuf.st_size+1);
if (!p) return NULL;
if (read(fd, p, sbuf.st_size) != sbuf.st_size) {
- free(p);
+ SAFE_FREE(p);
return NULL;
}
p[sbuf.st_size] = 0;
+ if (size) *size = sbuf.st_size;
+
+ return p;
+}
+
+/****************************************************************************
+load a file into memory
+****************************************************************************/
+char *file_load(const char *fname, size_t *size)
+{
+ int fd;
+ char *p;
+
+ if (!fname || !*fname) return NULL;
+
+ fd = open(fname,O_RDONLY);
+ if (fd == -1) return NULL;
+
+ p = fd_load(fd, size);
+
close(fd);
- if (size) *size = sbuf.st_size;
+ return p;
+}
+
+
+/*******************************************************************
+mmap (if possible) or read a file
+********************************************************************/
+void *map_file(char *fname, size_t size)
+{
+ size_t s2 = 0;
+ void *p = NULL;
+#ifdef HAVE_MMAP
+ if (lp_use_mmap()) {
+ int fd;
+ fd = open(fname, O_RDONLY, 0);
+ if (fd == -1) {
+ DEBUG(2,("Failed to load %s - %s\n", fname, strerror(errno)));
+ return NULL;
+ }
+ p = mmap(NULL, size, PROT_READ, MAP_SHARED|MAP_FILE, fd, 0);
+ close(fd);
+ if (p == MAP_FAILED) {
+ DEBUG(1,("Failed to mmap %s - %s\n", fname, strerror(errno)));
+ return NULL;
+ }
+ }
+#endif
+ if (!p) {
+ p = file_load(fname, &s2);
+ if (!p) return NULL;
+ if (s2 != size) {
+ DEBUG(1,("incorrect size for %s - got %lu expected %lu\n",
+ fname, (unsigned long)s2, (unsigned long)size));
+ if (p) free(p);
+ return NULL;
+ }
+ }
return p;
}
ret = (char **)malloc(sizeof(ret[0])*(i+2));
if (!ret) {
- free(p);
+ SAFE_FREE(p);
return NULL;
}
memset(ret, 0, sizeof(ret[0])*(i+2));
/****************************************************************************
load a file into memory and return an array of pointers to lines in the file
-must be freed with file_lines_free()
+must be freed with file_lines_free().
****************************************************************************/
-char **file_lines_load(char *fname, int *numlines)
+char **file_lines_load(const char *fname, int *numlines)
{
char *p;
size_t size;
return file_lines_parse(p, size, numlines);
}
+/****************************************************************************
+load a fd into memory and return an array of pointers to lines in the file
+must be freed with file_lines_free(). If convert is true calls unix_to_dos on
+the list.
+****************************************************************************/
+char **fd_lines_load(int fd, int *numlines)
+{
+ char *p;
+ size_t size;
+
+ p = fd_load(fd, &size);
+ if (!p) return NULL;
+
+ return file_lines_parse(p, size, numlines);
+}
+
/****************************************************************************
load a pipe into memory and return an array of pointers to lines in the data
-must be freed with file_lines_free()
+must be freed with file_lines_free().
****************************************************************************/
char **file_lines_pload(char *syscmd, int *numlines)
{
void file_lines_free(char **lines)
{
if (!lines) return;
- free(lines[0]);
- free(lines);
+ SAFE_FREE(lines[0]);
+ SAFE_FREE(lines);
}
}
}
}
+
+/*
+ save a lump of data into a file. Mostly used for debugging
+*/
+BOOL file_save(const char *fname, void *packet, size_t length)
+{
+ int fd;
+ fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
+ if (fd == -1) {
+ return False;
+ }
+ if (write(fd, packet, length) != (size_t)length) {
+ return False;
+ }
+ close(fd);
+ return True;
+}