From 613860a3aa3523642b01b8eaa62db7e08612a584 Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Thu, 5 Nov 1998 16:51:34 +0000 Subject: util_file.c: split some routines out of various places (e.g smbpass.c) because they now get used in more than one location. util_sid.c: need sid_copy, compare, split rid, append rid etc etc... (This used to be commit 71dfaa307ec954041c09ed157594a46503fb6db8) --- source3/lib/util_file.c | 329 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 329 insertions(+) create mode 100644 source3/lib/util_file.c (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c new file mode 100644 index 0000000000..0d6e77b010 --- /dev/null +++ b/source3/lib/util_file.c @@ -0,0 +1,329 @@ +/* + * Unix SMB/Netbios implementation. Version 1.9. 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 + * the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., 675 + * Mass Ave, Cambridge, MA 02139, USA. + */ + +#include "includes.h" + +extern int DEBUGLEVEL; + +static int gotalarm; + +/*************************************************************** + Signal function to tell us we timed out. +****************************************************************/ + +static void gotalarm_sig(void) +{ + gotalarm = 1; +} + +/*************************************************************** + Lock or unlock a fd for a known lock type. Abandon after waitsecs + seconds. +****************************************************************/ + +BOOL do_file_lock(int fd, int waitsecs, int type) +{ + SMB_STRUCT_FLOCK lock; + int ret; + + gotalarm = 0; + CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig); + + lock.l_type = type; + lock.l_whence = SEEK_SET; + lock.l_start = 0; + lock.l_len = 1; + lock.l_pid = 0; + + alarm(5); + ret = fcntl(fd, SMB_F_SETLKW, &lock); + alarm(0); + CatchSignal(SIGALRM, SIGNAL_CAST SIG_DFL); + + if (gotalarm) { + DEBUG(0, ("do_file_lock: failed to %s file.\n", + type == F_UNLCK ? "unlock" : "lock")); + return False; + } + + return (ret == 0); +} + + +/*************************************************************** + Lock an fd. Abandon after waitsecs seconds. +****************************************************************/ + +BOOL file_lock(int fd, int type, int secs, int *plock_depth) +{ + if (fd < 0) + return False; + + (*plock_depth)++; + + if ((*plock_depth) == 0) + { + if (!do_file_lock(fd, secs, type)) { + DEBUG(10,("file_lock: locking file failed, error = %s.\n", + strerror(errno))); + return False; + } + } + + return True; +} + +/*************************************************************** + Unlock an fd. Abandon after waitsecs seconds. +****************************************************************/ + +BOOL file_unlock(int fd, int *plock_depth) +{ + BOOL ret=True; + + if(*plock_depth == 1) + ret = do_file_lock(fd, 5, F_UNLCK); + + (*plock_depth)--; + + if(!ret) + DEBUG(10,("file_unlock: unlocking file failed, error = %s.\n", + strerror(errno))); + return ret; +} + +/*************************************************************** + locks a file for enumeration / modification. + update to be set = True if modification is required. +****************************************************************/ + +void *startfilepwent(char *pfile, char *s_readbuf, int bufsize, + int *file_lock_depth, BOOL update) +{ + FILE *fp = NULL; + + if (!*pfile) + { + DEBUG(0, ("startfilepwent: No file set\n")); + return (NULL); + } + DEBUG(10, ("startfilepwent: opening file %s\n", pfile)); + + fp = fopen(pfile, update ? "r+b" : "rb"); + + if (fp == NULL) { + DEBUG(0, ("startfilepwent: unable to open file %s\n", pfile)); + return NULL; + } + + /* Set a buffer to do more efficient reads */ + setvbuf(fp, s_readbuf, _IOFBF, bufsize); + + if (!file_lock(fileno(fp), (update ? F_WRLCK : F_RDLCK), 5, file_lock_depth)) + { + DEBUG(0, ("startfilepwent: unable to lock file %s\n", pfile)); + fclose(fp); + return NULL; + } + + /* Make sure it is only rw by the owner */ + chmod(pfile, 0600); + + /* We have a lock on the file. */ + return (void *)fp; +} + +/*************************************************************** + End enumeration of the file. +****************************************************************/ +void endfilepwent(void *vp, int *file_lock_depth) +{ + FILE *fp = (FILE *)vp; + + file_unlock(fileno(fp), file_lock_depth); + fclose(fp); + DEBUG(7, ("endfilepwent: closed file.\n")); +} + +/************************************************************************* + Return the current position in the file list as an SMB_BIG_UINT. + This must be treated as an opaque token. +*************************************************************************/ +SMB_BIG_UINT getfilepwpos(void *vp) +{ + return (SMB_BIG_UINT)sys_ftell((FILE *)vp); +} + +/************************************************************************* + Set the current position in the file list from an SMB_BIG_UINT. + This must be treated as an opaque token. +*************************************************************************/ +BOOL setfilepwpos(void *vp, SMB_BIG_UINT tok) +{ + return !sys_fseek((FILE *)vp, (SMB_OFF_T)tok, SEEK_SET); +} + +/************************************************************************* + gets a line out of a file. + line is of format "xxxx:xxxxxx:xxxxx:". + lines with "#" at the front are ignored. +*************************************************************************/ +int getfileline(void *vp, char *linebuf, int linebuf_size) +{ + /* Static buffers we will return. */ + FILE *fp = (FILE *)vp; + unsigned char c; + unsigned char *p; + size_t linebuf_len; + + if (fp == NULL) + { + DEBUG(0,("getfileline: Bad file pointer.\n")); + return -1; + } + + /* + * Scan the file, a line at a time. + */ + while (!feof(fp)) + { + linebuf[0] = '\0'; + + fgets(linebuf, linebuf_size, fp); + if (ferror(fp)) + { + return -1; + } + + /* + * Check if the string is terminated with a newline - if not + * then we must keep reading and discard until we get one. + */ + + linebuf_len = strlen(linebuf); + if (linebuf[linebuf_len - 1] != '\n') + { + c = '\0'; + while (!ferror(fp) && !feof(fp)) + { + c = fgetc(fp); + if (c == '\n') + { + break; + } + } + } + else + { + linebuf[linebuf_len - 1] = '\0'; + } + +#ifdef DEBUG_PASSWORD + DEBUG(100, ("getfileline: got line |%s|\n", linebuf)); +#endif + if ((linebuf[0] == 0) && feof(fp)) + { + DEBUG(4, ("getfileline: end of file reached\n")); + return 0; + } + + if (linebuf[0] == '#' || linebuf[0] == '\0') + { + DEBUG(6, ("getfileline: skipping comment or blank line\n")); + continue; + } + + p = (unsigned char *) strchr(linebuf, ':'); + if (p == NULL) + { + DEBUG(0, ("getfileline: malformed line entry (no :)\n")); + continue; + } + return linebuf_len; + } + return -1; +} + + +/**************************************************************************** +read a line from a file with possible \ continuation chars. +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 *s=s2; + int len = 0; + int c; + BOOL start_of_line = True; + + if (feof(f)) + return(NULL); + + if (!s2) + { + maxlen = MIN(maxlen,8); + s = (char *)Realloc(s,maxlen); + } + + if (!s || maxlen < 2) return(NULL); + + *s = 0; + + while (len < maxlen-1) + { + c = getc(f); + switch (c) + { + case '\r': + break; + case '\n': + while (len > 0 && s[len-1] == ' ') + { + s[--len] = 0; + } + if (len > 0 && s[len-1] == '\\') + { + s[--len] = 0; + start_of_line = True; + break; + } + return(s); + case EOF: + if (len <= 0 && !s2) + free(s); + return(len>0?s:NULL); + case ' ': + if (start_of_line) + break; + default: + start_of_line = False; + s[len++] = c; + s[len] = 0; + } + if (!s2 && len > maxlen-3) + { + maxlen *= 2; + s = (char *)Realloc(s,maxlen); + if (!s) return(NULL); + } + } + return(s); +} + -- cgit From 768761820e8d7481c586c4e0ab4ac7cb36d18c4b Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 17 Nov 1998 20:50:07 +0000 Subject: Added the same open()/fopen()/creat()/mmap() -> sys_XXX calls. Tidied up some of the mess (no other word for it). Still doesn't compile cleanly. There are calls with incorrect parameters that don't seem to be doing the right thing. This code still needs surgery :-(. Jeremy. (This used to be commit 18ff93a9abbf68ee8c59c0af3e57c63e4a015dac) --- source3/lib/util_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 0d6e77b010..faceed1dbd 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -125,7 +125,7 @@ void *startfilepwent(char *pfile, char *s_readbuf, int bufsize, } DEBUG(10, ("startfilepwent: opening file %s\n", pfile)); - fp = fopen(pfile, update ? "r+b" : "rb"); + fp = sys_fopen(pfile, update ? "r+b" : "rb"); if (fp == NULL) { DEBUG(0, ("startfilepwent: unable to open file %s\n", pfile)); -- cgit From 0ad513f42c10266bf4ab61089eb212efd05366f6 Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Tue, 13 Jul 1999 19:54:40 +0000 Subject: renamed getfilepwent() and endfilepwent() to getfileent() and endfileent() as they are generic "file line-by-line" reading routines. lines with "#" at the front are ignored (as comments). this code started out as the password file reading code. (This used to be commit ef6df590fdf65a6d94b343998bac3a4d48ae07e0) --- source3/lib/util_file.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index faceed1dbd..37489086d8 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -113,22 +113,22 @@ BOOL file_unlock(int fd, int *plock_depth) update to be set = True if modification is required. ****************************************************************/ -void *startfilepwent(char *pfile, char *s_readbuf, int bufsize, +void *startfileent(char *pfile, char *s_readbuf, int bufsize, int *file_lock_depth, BOOL update) { FILE *fp = NULL; if (!*pfile) { - DEBUG(0, ("startfilepwent: No file set\n")); + DEBUG(0, ("startfileent: No file set\n")); return (NULL); } - DEBUG(10, ("startfilepwent: opening file %s\n", pfile)); + DEBUG(10, ("startfileent: opening file %s\n", pfile)); fp = sys_fopen(pfile, update ? "r+b" : "rb"); if (fp == NULL) { - DEBUG(0, ("startfilepwent: unable to open file %s\n", pfile)); + DEBUG(0, ("startfileent: unable to open file %s\n", pfile)); return NULL; } @@ -137,7 +137,7 @@ void *startfilepwent(char *pfile, char *s_readbuf, int bufsize, if (!file_lock(fileno(fp), (update ? F_WRLCK : F_RDLCK), 5, file_lock_depth)) { - DEBUG(0, ("startfilepwent: unable to lock file %s\n", pfile)); + DEBUG(0, ("startfileent: unable to lock file %s\n", pfile)); fclose(fp); return NULL; } @@ -152,13 +152,13 @@ void *startfilepwent(char *pfile, char *s_readbuf, int bufsize, /*************************************************************** End enumeration of the file. ****************************************************************/ -void endfilepwent(void *vp, int *file_lock_depth) +void endfileent(void *vp, int *file_lock_depth) { FILE *fp = (FILE *)vp; file_unlock(fileno(fp), file_lock_depth); fclose(fp); - DEBUG(7, ("endfilepwent: closed file.\n")); + DEBUG(7, ("endfileent: closed file.\n")); } /************************************************************************* @@ -181,7 +181,6 @@ BOOL setfilepwpos(void *vp, SMB_BIG_UINT tok) /************************************************************************* gets a line out of a file. - line is of format "xxxx:xxxxxx:xxxxx:". lines with "#" at the front are ignored. *************************************************************************/ int getfileline(void *vp, char *linebuf, int linebuf_size) -- cgit From 1f33d5a8aaa6b9a94dde21529fe2aa407fb5ffa1 Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Wed, 14 Jul 1999 19:21:44 +0000 Subject: code from bertl to allow remap of default built-in names to anything. parameter is "builtin rid file". Copyright 1999 Bertl (This used to be commit 80d36778432d42eb265ed9428f27a27250ba5e08) --- source3/lib/util_file.c | 53 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 46 insertions(+), 7 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 37489086d8..5861314c02 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -188,7 +188,6 @@ int getfileline(void *vp, char *linebuf, int linebuf_size) /* Static buffers we will return. */ FILE *fp = (FILE *)vp; unsigned char c; - unsigned char *p; size_t linebuf_len; if (fp == NULL) @@ -248,12 +247,6 @@ int getfileline(void *vp, char *linebuf, int linebuf_size) continue; } - p = (unsigned char *) strchr(linebuf, ':'); - if (p == NULL) - { - DEBUG(0, ("getfileline: malformed line entry (no :)\n")); - continue; - } return linebuf_len; } return -1; @@ -326,3 +319,49 @@ char *fgets_slash(char *s2,int maxlen,FILE *f) return(s); } +/**************************************************************************** +checks if a file has changed since last read +****************************************************************************/ +BOOL file_modified(const char *filename, time_t *lastmodified) +{ + SMB_STRUCT_STAT st; + + if (sys_stat(filename, &st) != 0) + { + DEBUG(0, ("file_changed: Unable to stat file %s. Error was %s\n", + filename, strerror(errno) )); + return False; + } + + if(st.st_mtime <= *lastmodified) + { + DEBUG(20, ("file_modified: %s not modified\n", filename)); + return False; + } + + DEBUG(20, ("file_modified: %s modified\n", filename)); + *lastmodified = st.st_mtime; + return True; +} + +/*************************************************************************** +opens a file if modified otherwise returns NULL +***************************************************************************/ +void *open_file_if_modified(const char *filename, char *mode, time_t *lastmodified) +{ + FILE *f; + + if (file_modified(filename, lastmodified)) + { + return NULL; + } + + if( (f = fopen(filename, mode)) == NULL) + { + DEBUG(0, ("open_file_if_modified: can't open file %s. Error was %s\n", + filename, strerror(errno))); + return NULL; + } + + return (void *)f; +} -- cgit From 73a0c9f04673499d7248f1b49d59f859649be11b Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Mon, 26 Jul 1999 18:35:46 +0000 Subject: Jean-Francois spotted bug in use of file_modified() routine submitted recently. (This used to be commit 717af2d55d4f964c0449a1e502b6e77d1c3f3f30) --- source3/lib/util_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 5861314c02..f4325b813b 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -351,7 +351,7 @@ void *open_file_if_modified(const char *filename, char *mode, time_t *lastmodifi { FILE *f; - if (file_modified(filename, lastmodified)) + if (!file_modified(filename, lastmodified)) { return NULL; } -- cgit From 3db52feb1f3b2c07ce0b06ad4a7099fa6efe3fc7 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 13 Dec 1999 13:27:58 +0000 Subject: first pass at updating head branch to be to be the same as the SAMBA_2_0 branch (This used to be commit 453a822a76780063dff23526c35408866d0c0154) --- source3/lib/util_file.c | 70 +++++++++++-------------------------------------- 1 file changed, 16 insertions(+), 54 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index f4325b813b..9eb38460d9 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -51,7 +51,7 @@ BOOL do_file_lock(int fd, int waitsecs, int type) lock.l_len = 1; lock.l_pid = 0; - alarm(5); + alarm(waitsecs); ret = fcntl(fd, SMB_F_SETLKW, &lock); alarm(0); CatchSignal(SIGALRM, SIGNAL_CAST SIG_DFL); @@ -113,22 +113,22 @@ BOOL file_unlock(int fd, int *plock_depth) update to be set = True if modification is required. ****************************************************************/ -void *startfileent(char *pfile, char *s_readbuf, int bufsize, +void *startfilepwent(char *pfile, char *s_readbuf, int bufsize, int *file_lock_depth, BOOL update) { FILE *fp = NULL; if (!*pfile) { - DEBUG(0, ("startfileent: No file set\n")); + DEBUG(0, ("startfilepwent: No file set\n")); return (NULL); } - DEBUG(10, ("startfileent: opening file %s\n", pfile)); + DEBUG(10, ("startfilepwent: opening file %s\n", pfile)); fp = sys_fopen(pfile, update ? "r+b" : "rb"); if (fp == NULL) { - DEBUG(0, ("startfileent: unable to open file %s\n", pfile)); + DEBUG(0, ("startfilepwent: unable to open file %s\n", pfile)); return NULL; } @@ -137,7 +137,7 @@ void *startfileent(char *pfile, char *s_readbuf, int bufsize, if (!file_lock(fileno(fp), (update ? F_WRLCK : F_RDLCK), 5, file_lock_depth)) { - DEBUG(0, ("startfileent: unable to lock file %s\n", pfile)); + DEBUG(0, ("startfilepwent: unable to lock file %s\n", pfile)); fclose(fp); return NULL; } @@ -152,13 +152,13 @@ void *startfileent(char *pfile, char *s_readbuf, int bufsize, /*************************************************************** End enumeration of the file. ****************************************************************/ -void endfileent(void *vp, int *file_lock_depth) +void endfilepwent(void *vp, int *file_lock_depth) { FILE *fp = (FILE *)vp; file_unlock(fileno(fp), file_lock_depth); fclose(fp); - DEBUG(7, ("endfileent: closed file.\n")); + DEBUG(7, ("endfilepwent: closed file.\n")); } /************************************************************************* @@ -181,6 +181,7 @@ BOOL setfilepwpos(void *vp, SMB_BIG_UINT tok) /************************************************************************* gets a line out of a file. + line is of format "xxxx:xxxxxx:xxxxx:". lines with "#" at the front are ignored. *************************************************************************/ int getfileline(void *vp, char *linebuf, int linebuf_size) @@ -188,6 +189,7 @@ int getfileline(void *vp, char *linebuf, int linebuf_size) /* Static buffers we will return. */ FILE *fp = (FILE *)vp; unsigned char c; + unsigned char *p; size_t linebuf_len; if (fp == NULL) @@ -247,6 +249,12 @@ int getfileline(void *vp, char *linebuf, int linebuf_size) continue; } + p = (unsigned char *) strchr(linebuf, ':'); + if (p == NULL) + { + DEBUG(0, ("getfileline: malformed line entry (no :)\n")); + continue; + } return linebuf_len; } return -1; @@ -319,49 +327,3 @@ char *fgets_slash(char *s2,int maxlen,FILE *f) return(s); } -/**************************************************************************** -checks if a file has changed since last read -****************************************************************************/ -BOOL file_modified(const char *filename, time_t *lastmodified) -{ - SMB_STRUCT_STAT st; - - if (sys_stat(filename, &st) != 0) - { - DEBUG(0, ("file_changed: Unable to stat file %s. Error was %s\n", - filename, strerror(errno) )); - return False; - } - - if(st.st_mtime <= *lastmodified) - { - DEBUG(20, ("file_modified: %s not modified\n", filename)); - return False; - } - - DEBUG(20, ("file_modified: %s modified\n", filename)); - *lastmodified = st.st_mtime; - return True; -} - -/*************************************************************************** -opens a file if modified otherwise returns NULL -***************************************************************************/ -void *open_file_if_modified(const char *filename, char *mode, time_t *lastmodified) -{ - FILE *f; - - if (!file_modified(filename, lastmodified)) - { - return NULL; - } - - if( (f = fopen(filename, mode)) == NULL) - { - DEBUG(0, ("open_file_if_modified: can't open file %s. Error was %s\n", - filename, strerror(errno))); - return NULL; - } - - return (void *)f; -} -- cgit From 95ddbb8aaf1631711299b0710820ef9b0545224c Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 16 Apr 2000 06:19:11 +0000 Subject: the new file_lines_load() and file_lines_free() routines. Very useful! ------------ The following series of commits are for the new tdb based printing backend. This completely replaces our old printing backend. Major changes include: - all print ops are now done in printing/*.c rather than scattered all over the place - system job ids are decoupled from SMB job ids - the lpq parsers don't need to be nearly so smart, they only need to parse the filename, the status and system job id - we can store lots more info about a job, including the full job name - the queue cache control is much better I also added a new utility routine file_lines_load() that loads a text file and parses it into lines. This is used in out lpq parsing and I also want to use it to replace all of our fgets() based code in other places. (This used to be commit be1e98b3f7a6007d2c9ac8e079724cb264e0dd3a) --- source3/lib/util_file.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 9eb38460d9..39c97d5c3f 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -327,3 +327,67 @@ char *fgets_slash(char *s2,int maxlen,FILE *f) return(s); } + +/**************************************************************************** +load a file into memory and return an array of pointers to lines in the file +must be freed with file_lines_free() +****************************************************************************/ +char **file_lines_load(char *fname, int *numlines) +{ + int fd, i; + SMB_STRUCT_STAT sbuf; + char *p, *s, **ret; + size_t size; + + fd = open(fname,O_RDONLY); + if (fd == -1) return NULL; + + if (sys_fstat(fd, &sbuf) != 0) return NULL; + size = sbuf.st_size; + + if (size == 0) return NULL; + + p = (char *)malloc(size+1); + if (!p) return NULL; + + if (read(fd, p, size) != size) { + free(p); + return NULL; + } + p[size] = 0; + + close(fd); + + for (s = p, i=0; s < p+size; s++) { + if (s[0] == '\n') i++; + } + + ret = (char **)malloc(sizeof(ret[0])*(i+1)); + if (!ret) { + free(p); + return NULL; + } + *numlines = i; + + ret[0] = p; + for (s = p, i=0; s < p+size; s++) { + if (s[0] == '\n') { + s[0] = 0; + i++; + ret[i] = s+1; + } + if (s[0] == '\r') s[0] = 0; + } + + return ret; +} + +/**************************************************************************** +free lines loaded with file_lines_load +****************************************************************************/ +void file_lines_free(char **lines) +{ + if (!lines) return; + free(lines[0]); + free(lines); +} -- cgit From 37c0312def0b0c1b3c4248bd8d797db15d88d772 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 16 Apr 2000 09:40:02 +0000 Subject: added fdprintf() this is like fprintf() but operates on a file descriptor combined with file_load_lines() this makes it really easy to get rid of the use of fopen() in Samba. (This used to be commit bd5cd502bf52164b95d7bfc026189e04988171db) --- source3/lib/util_file.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 39c97d5c3f..d533e6428f 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -362,12 +362,13 @@ char **file_lines_load(char *fname, int *numlines) if (s[0] == '\n') i++; } - ret = (char **)malloc(sizeof(ret[0])*(i+1)); + ret = (char **)malloc(sizeof(ret[0])*(i+2)); if (!ret) { free(p); return NULL; } - *numlines = i; + memset(ret, 0, sizeof(ret[0])*(i+2)); + if (numlines) *numlines = i; ret[0] = p; for (s = p, i=0; s < p+size; s++) { @@ -391,3 +392,4 @@ void file_lines_free(char **lines) free(lines[0]); free(lines); } + -- cgit From 19f946ba6fe442544ac9b0f71bcd33112fc79995 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 16 Apr 2000 11:00:21 +0000 Subject: converted a bunch more functions to use a fd instead of a FILE* to support some of this I added the following functions in util_file.c file_lines_pload : load lines from a pipe file_pload : load a pipe into memory (This used to be commit a09470817c5b21dba42f9ef4ce5e8b768a254c0b) --- source3/lib/util_file.c | 102 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 91 insertions(+), 11 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index d533e6428f..1ef713b105 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -329,35 +329,83 @@ char *fgets_slash(char *s2,int maxlen,FILE *f) /**************************************************************************** -load a file into memory and return an array of pointers to lines in the file -must be freed with file_lines_free() +load from a pipe into memory ****************************************************************************/ -char **file_lines_load(char *fname, int *numlines) +char *file_pload(char *syscmd, size_t *size) { - int fd, i; + int fd, n; + char *p; + pstring buf; + size_t total; + + fd = sys_popen(syscmd); + if (fd == -1) return NULL; + + p = NULL; + total = 0; + + while ((n = read(fd, buf, sizeof(buf))) > 0) { + p = Realloc(p, total + n + 1); + if (!p) { + close(fd); + return NULL; + } + memcpy(p+total, buf, n); + total += n; + } + p[total] = 0; + + sys_pclose(fd); + + if (size) *size = total; + + return p; +} + + +/**************************************************************************** +load a file into memory +****************************************************************************/ +char *file_load(char *fname, size_t *size) +{ + int fd; SMB_STRUCT_STAT sbuf; - char *p, *s, **ret; - size_t size; + char *p; fd = open(fname,O_RDONLY); if (fd == -1) return NULL; if (sys_fstat(fd, &sbuf) != 0) return NULL; - size = sbuf.st_size; - if (size == 0) return NULL; + if (sbuf.st_size == 0) return NULL; - p = (char *)malloc(size+1); + p = (char *)malloc(sbuf.st_size+1); if (!p) return NULL; - if (read(fd, p, size) != size) { + if (read(fd, p, sbuf.st_size) != sbuf.st_size) { free(p); return NULL; } - p[size] = 0; + p[sbuf.st_size] = 0; close(fd); + if (size) *size = sbuf.st_size; + + return p; +} + + +/**************************************************************************** +parse a buffer into lines +****************************************************************************/ +static char **file_lines_parse(char *p, size_t size, int *numlines) +{ + int i; + char *s, **ret; + + if (!p) return NULL; + for (s = p, i=0; s < p+size; s++) { if (s[0] == '\n') i++; } @@ -383,6 +431,38 @@ char **file_lines_load(char *fname, int *numlines) return ret; } + +/**************************************************************************** +load a file into memory and return an array of pointers to lines in the file +must be freed with file_lines_free() +****************************************************************************/ +char **file_lines_load(char *fname, int *numlines) +{ + char *p; + size_t size; + + p = file_load(fname, &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() +****************************************************************************/ +char **file_lines_pload(char *syscmd, int *numlines) +{ + char *p; + size_t size; + + p = file_pload(syscmd, &size); + if (!p) return NULL; + + return file_lines_parse(p, size, numlines); +} + /**************************************************************************** free lines loaded with file_lines_load ****************************************************************************/ -- cgit From 8d7e498db160f9a366400fc1c55dacbcffaf4196 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 16 Apr 2000 11:17:19 +0000 Subject: converted a couple more functions to use a fd instead of a FILE* added a new utility fn file_lines_slashcont() which is used to handle files that treat a \ followed by a newline as a blank (This used to be commit 384ecd9d66ccd31ee85000c0ca55d413d8f2cc53) --- source3/lib/util_file.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 1ef713b105..f3e2879587 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -473,3 +473,26 @@ void file_lines_free(char **lines) free(lines); } + +/**************************************************************************** +take a lislist of lines and modify them to produce a list where \ continues +a line +****************************************************************************/ +void file_lines_slashcont(char **lines) +{ + int i, j; + + for (i=0; lines[i];) { + int len = strlen(lines[i]); + if (lines[i][len-1] == '\\') { + lines[i][len-1] = ' '; + if (lines[i+1]) { + char *p = &lines[i][len]; + while (p < lines[i+1]) *p++ = ' '; + for (j = i+1; lines[j]; j++) lines[j] = lines[j+1]; + } + } else { + i++; + } + } +} -- cgit From 75731f8c23ee20f5d345615b1398be1991fff0e4 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 18 Apr 2000 02:17:46 +0000 Subject: fixed some crash bugs in the nt forms parsing (This used to be commit e505a6ddf3df37ca485cae117c53fa96d736f897) --- source3/lib/util_file.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index f3e2879587..c3b444ffa1 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -371,6 +371,8 @@ char *file_load(char *fname, 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; -- cgit From 05cb3464f972d336dcb82ca332bf9b2617646070 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 30 Apr 2000 15:13:15 +0000 Subject: - added some error checking - removed the VTP hook in smbd (This used to be commit 09355fcd50e6c9c0c81e5f70ab9b7ff88aa897bf) --- source3/lib/util_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index c3b444ffa1..194fb7ae49 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -353,7 +353,7 @@ char *file_pload(char *syscmd, size_t *size) memcpy(p+total, buf, n); total += n; } - p[total] = 0; + if (p) p[total] = 0; sys_pclose(fd); -- cgit From 40cc8e0df2282e8fc8d20735ee351add1ae23644 Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Fri, 18 Aug 2000 05:56:19 +0000 Subject: getfileline() - line with length of zero -> filebuf[strlen(filebuf)-1] is NOT ok. (This used to be commit 24e0c8ef70dc59bfaaa113c3d44befbccbcba15f) --- source3/lib/util_file.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 194fb7ae49..50bd85e6e3 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -217,21 +217,24 @@ int getfileline(void *vp, char *linebuf, int linebuf_size) */ linebuf_len = strlen(linebuf); - if (linebuf[linebuf_len - 1] != '\n') + if (linebuf_len > 0) { - c = '\0'; - while (!ferror(fp) && !feof(fp)) + if (linebuf[linebuf_len - 1] != '\n') { - c = fgetc(fp); - if (c == '\n') + c = '\0'; + while (!ferror(fp) && !feof(fp)) { - break; + c = fgetc(fp); + if (c == '\n') + { + break; + } } } - } - else - { - linebuf[linebuf_len - 1] = '\0'; + else + { + linebuf[linebuf_len - 1] = '\0'; + } } #ifdef DEBUG_PASSWORD -- cgit From 7e5fd8fc2c255b5388a634b18280bb497f82e3ed Mon Sep 17 00:00:00 2001 From: Luke Leighton Date: Fri, 18 Aug 2000 06:27:24 +0000 Subject: oops. must return "" string and length zero when strlen(filebuf) == 0 (This used to be commit d3bc7cca99e47ce89035a03022d7c3ec69e01636) --- source3/lib/util_file.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 50bd85e6e3..e21aafa009 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -217,24 +217,27 @@ int getfileline(void *vp, char *linebuf, int linebuf_size) */ linebuf_len = strlen(linebuf); - if (linebuf_len > 0) + if (linebuf_len == 0) { - if (linebuf[linebuf_len - 1] != '\n') + linebuf[0] = '\0'; + return 0; + } + + if (linebuf[linebuf_len - 1] != '\n') + { + c = '\0'; + while (!ferror(fp) && !feof(fp)) { - c = '\0'; - while (!ferror(fp) && !feof(fp)) + c = fgetc(fp); + if (c == '\n') { - c = fgetc(fp); - if (c == '\n') - { - break; - } + break; } } - else - { - linebuf[linebuf_len - 1] = '\0'; - } + } + else + { + linebuf[linebuf_len - 1] = '\0'; } #ifdef DEBUG_PASSWORD -- cgit From 38a585c99d6373e846de19fdd75b4df259806fc7 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Fri, 1 Dec 2000 04:10:54 +0000 Subject: Allow zero length smb.conf files. (This used to be commit 46007a541cd2497c14659a10ba24a6d0a375ac5a) --- source3/lib/util_file.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index e21aafa009..33a687950d 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -385,8 +385,6 @@ char *file_load(char *fname, size_t *size) 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; -- cgit From cf5b71994d6cdb2f81c390579f4a0e676926c6b9 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 7 Dec 2000 19:26:04 +0000 Subject: file_lines_load/file_lines_pload can now optionally convert unix_to_dos() on read. Jeremy. (This used to be commit 76b8dd376d13eb4469417be217c966d54d333367) --- source3/lib/util_file.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 33a687950d..1184dd0634 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -405,7 +405,7 @@ char *file_load(char *fname, size_t *size) /**************************************************************************** parse a buffer into lines ****************************************************************************/ -static char **file_lines_parse(char *p, size_t size, int *numlines) +static char **file_lines_parse(char *p, size_t size, int *numlines, BOOL convert) { int i; char *s, **ret; @@ -434,15 +434,21 @@ static char **file_lines_parse(char *p, size_t size, int *numlines) if (s[0] == '\r') s[0] = 0; } + if (convert) { + for (i = 0; i < *numlines; i++) + unix_to_dos(ret[i], True); + } + return ret; } /**************************************************************************** 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(). If convert is true calls unix_to_dos on +the list. ****************************************************************************/ -char **file_lines_load(char *fname, int *numlines) +char **file_lines_load(char *fname, int *numlines, BOOL convert) { char *p; size_t size; @@ -450,15 +456,16 @@ char **file_lines_load(char *fname, int *numlines) p = file_load(fname, &size); if (!p) return NULL; - return file_lines_parse(p, size, numlines); + return file_lines_parse(p, size, numlines, convert); } /**************************************************************************** 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(). If convert is true calls unix_to_dos on +the list. ****************************************************************************/ -char **file_lines_pload(char *syscmd, int *numlines) +char **file_lines_pload(char *syscmd, int *numlines, BOOL convert) { char *p; size_t size; @@ -466,7 +473,7 @@ char **file_lines_pload(char *syscmd, int *numlines) p = file_pload(syscmd, &size); if (!p) return NULL; - return file_lines_parse(p, size, numlines); + return file_lines_parse(p, size, numlines, convert); } /**************************************************************************** -- cgit From 407b48d8a1f74b48eb64c4c94c0aa696f70172ff Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 9 Dec 2000 06:34:59 +0000 Subject: Fixed typo causing coredump in file_lines_parse. Jeremy. (This used to be commit f575f4d67a5b45e47c29de30f02901c55cef4621) --- source3/lib/util_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 1184dd0634..023f3e131c 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -435,7 +435,7 @@ static char **file_lines_parse(char *p, size_t size, int *numlines, BOOL convert } if (convert) { - for (i = 0; i < *numlines; i++) + for (i = 0; ret[i]; i++) unix_to_dos(ret[i], True); } -- cgit From 50e78a9ac8cf0949c2471fafde844c674f97d73d Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 13 Apr 2001 00:37:00 +0000 Subject: As Andrew suggested, make smbrun return a fd for a deleted file which can then be read. Jeremy. (This used to be commit e7d59d6de89a5fdd201e4b5c6072dab08b1519db) --- source3/lib/util_file.c | 52 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 12 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 023f3e131c..4e2adc97bc 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -368,21 +368,15 @@ char *file_pload(char *syscmd, size_t *size) 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; p = (char *)malloc(sbuf.st_size+1); @@ -394,13 +388,31 @@ char *file_load(char *fname, size_t *size) } p[sbuf.st_size] = 0; - close(fd); - if (size) *size = sbuf.st_size; return p; } +/**************************************************************************** +load a file into memory +****************************************************************************/ +char *file_load(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); + + return p; +} + /**************************************************************************** parse a buffer into lines @@ -459,6 +471,22 @@ char **file_lines_load(char *fname, int *numlines, BOOL convert) return file_lines_parse(p, size, numlines, convert); } +/**************************************************************************** +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, BOOL convert) +{ + char *p; + size_t size; + + p = fd_load(fd, &size); + if (!p) return NULL; + + return file_lines_parse(p, size, numlines, convert); +} + /**************************************************************************** load a pipe into memory and return an array of pointers to lines in the data -- cgit From 87fbb7092b8f8b2f0db0f361c3d625e19de57cd9 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 4 Jul 2001 07:15:53 +0000 Subject: The big character set handling changeover! This commit gets rid of all our old codepage handling and replaces it with iconv. All internal strings in Samba are now in "unix" charset, which may be multi-byte. See internals.doc and my posting to samba-technical for a more complete explanation. (This used to be commit debb471267960e56005a741817ebd227ecfc512a) --- source3/lib/util_file.c | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 4e2adc97bc..7dc25a8dae 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -417,7 +417,7 @@ char *file_load(char *fname, size_t *size) /**************************************************************************** parse a buffer into lines ****************************************************************************/ -static char **file_lines_parse(char *p, size_t size, int *numlines, BOOL convert) +static char **file_lines_parse(char *p, size_t size, int *numlines) { int i; char *s, **ret; @@ -446,21 +446,15 @@ static char **file_lines_parse(char *p, size_t size, int *numlines, BOOL convert if (s[0] == '\r') s[0] = 0; } - if (convert) { - for (i = 0; ret[i]; i++) - unix_to_dos(ret[i], True); - } - return ret; } /**************************************************************************** load a file 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. +must be freed with file_lines_free(). ****************************************************************************/ -char **file_lines_load(char *fname, int *numlines, BOOL convert) +char **file_lines_load(char *fname, int *numlines) { char *p; size_t size; @@ -468,7 +462,7 @@ char **file_lines_load(char *fname, int *numlines, BOOL convert) p = file_load(fname, &size); if (!p) return NULL; - return file_lines_parse(p, size, numlines, convert); + return file_lines_parse(p, size, numlines); } /**************************************************************************** @@ -476,7 +470,7 @@ 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, BOOL convert) +char **fd_lines_load(int fd, int *numlines) { char *p; size_t size; @@ -484,16 +478,15 @@ char **fd_lines_load(int fd, int *numlines, BOOL convert) p = fd_load(fd, &size); if (!p) return NULL; - return file_lines_parse(p, size, numlines, convert); + 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(). If convert is true calls unix_to_dos on -the list. +must be freed with file_lines_free(). ****************************************************************************/ -char **file_lines_pload(char *syscmd, int *numlines, BOOL convert) +char **file_lines_pload(char *syscmd, int *numlines) { char *p; size_t size; @@ -501,7 +494,7 @@ char **file_lines_pload(char *syscmd, int *numlines, BOOL convert) p = file_pload(syscmd, &size); if (!p) return NULL; - return file_lines_parse(p, size, numlines, convert); + return file_lines_parse(p, size, numlines); } /**************************************************************************** -- cgit From 527e824293ee934ca5da0ef5424efe5ab7757248 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 4 Jul 2001 07:36:09 +0000 Subject: strchr and strrchr are macros when compiling with optimisation in gcc, so we can't redefine them. damn. (This used to be commit c41fc06376d1a2b83690612304e85010b5e5f3cf) --- source3/lib/util_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 7dc25a8dae..01a8b1c333 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -255,7 +255,7 @@ int getfileline(void *vp, char *linebuf, int linebuf_size) continue; } - p = (unsigned char *) strchr(linebuf, ':'); + p = (unsigned char *) strchr_m(linebuf, ':'); if (p == NULL) { DEBUG(0, ("getfileline: malformed line entry (no :)\n")); -- cgit From e485a1a4986c9328754b9a8b3054b8a6738b54f0 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Sun, 5 Aug 2001 10:10:16 +0000 Subject: Some fixes about malloc/Realloc and mem leak thanks to andreas moroder (This used to be commit b29a549cdd85d42a1697041ab04f0ae4eddd23ca) --- source3/lib/util_file.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 01a8b1c333..a92eb15333 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -282,13 +282,15 @@ char *fgets_slash(char *s2,int maxlen,FILE *f) if (feof(f)) return(NULL); + if (maxlen <2) return(NULL); + if (!s2) { maxlen = MIN(maxlen,8); s = (char *)Realloc(s,maxlen); } - if (!s || maxlen < 2) return(NULL); + if (!s) return(NULL); *s = 0; -- cgit From 2e783a47076bd0994b6ce86df7ec967bc1c2da63 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Sun, 12 Aug 2001 17:30:01 +0000 Subject: this is a big global fix for the ptr = Realloc(ptr, size) bug. many possible mem leaks, and segfaults fixed. someone should port this fix to 2.2 also. (This used to be commit fa8e55b8b465114ce209344965c1ca0333b84db9) --- source3/lib/util_file.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index a92eb15333..d80c09666b 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -287,7 +287,7 @@ char *fgets_slash(char *s2,int maxlen,FILE *f) if (!s2) { maxlen = MIN(maxlen,8); - s = (char *)Realloc(s,maxlen); + s = (char *)malloc(maxlen); } if (!s) return(NULL); @@ -327,9 +327,15 @@ char *fgets_slash(char *s2,int maxlen,FILE *f) } 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")); + if (s) free(s); + return(NULL); + } else s = t; } } return(s); @@ -342,7 +348,7 @@ load from a pipe into memory char *file_pload(char *syscmd, size_t *size) { int fd, n; - char *p; + char *p, *tp; pstring buf; size_t total; @@ -353,11 +359,13 @@ char *file_pload(char *syscmd, size_t *size) 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 exand buffer!\n")); close(fd); + if (p) free(p); return NULL; - } + } else p = tp; memcpy(p+total, buf, n); total += n; } -- cgit From 79139fe8d882c39620b0d52ef081f639d1294917 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 10 Sep 2001 12:46:42 +0000 Subject: convert more code to use XFILE (This used to be commit fe6679dffba9a92bb35933ad52172c9be0e9ef90) --- source3/lib/util_file.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index d80c09666b..8eeb3475e3 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -272,14 +272,14 @@ read a line from a file with possible \ continuation chars. 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); @@ -296,7 +296,7 @@ char *fgets_slash(char *s2,int maxlen,FILE *f) while (len < maxlen-1) { - c = getc(f); + c = x_getc(f); switch (c) { case '\r': -- cgit From 484a7c0341fe033fe26fe1e6b597ed1c456c39d4 Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Mon, 17 Sep 2001 02:19:44 +0000 Subject: move to SAFE_FREE() (This used to be commit 60e907b7e8e1c008463a88ed2b076344278986ef) --- source3/lib/util_file.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 8eeb3475e3..35b47a2cbb 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -315,7 +315,7 @@ char *fgets_slash(char *s2,int maxlen,XFILE *f) return(s); case EOF: if (len <= 0 && !s2) - free(s); + SAFE_FREE(s); return(len>0?s:NULL); case ' ': if (start_of_line) @@ -333,7 +333,7 @@ char *fgets_slash(char *s2,int maxlen,XFILE *f) t = (char *)Realloc(s,maxlen); if (!t) { DEBUG(0,("fgets_slash: failed to expand buffer!\n")); - if (s) free(s); + SAFE_FREE(s); return(NULL); } else s = t; } @@ -363,7 +363,7 @@ char *file_pload(char *syscmd, size_t *size) if (!tp) { DEBUG(0,("file_pload: failed to exand buffer!\n")); close(fd); - if (p) free(p); + SAFE_FREE(p); return NULL; } else p = tp; memcpy(p+total, buf, n); @@ -393,7 +393,7 @@ char *fd_load(int fd, size_t *size) 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; @@ -440,7 +440,7 @@ static char **file_lines_parse(char *p, size_t size, int *numlines) 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)); @@ -513,8 +513,8 @@ free lines loaded with file_lines_load void file_lines_free(char **lines) { if (!lines) return; - free(lines[0]); - free(lines); + SAFE_FREE(lines[0]); + SAFE_FREE(lines); } -- cgit From d25c2b146b1581ef2a141ecbc04579b792b4c4dc Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Sat, 29 Sep 2001 11:51:40 +0000 Subject: Add a few const statements to various odd bits of the tree. (Fixes some warnings) (This used to be commit b648cc669d16eb40b477c8dc51efeab485a15de5) --- source3/lib/util_file.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 35b47a2cbb..1d7b85f75c 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -406,7 +406,7 @@ char *fd_load(int fd, size_t *size) /**************************************************************************** load a file into memory ****************************************************************************/ -char *file_load(char *fname, size_t *size) +char *file_load(const char *fname, size_t *size) { int fd; char *p; @@ -464,7 +464,7 @@ static char **file_lines_parse(char *p, size_t size, int *numlines) load a file into memory and return an array of pointers to lines in the file 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; -- cgit From dc1fc3ee8ec2199bc73bb5d7ec711c6800f61d65 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Tue, 2 Oct 2001 04:29:50 +0000 Subject: Removed 'extern int DEBUGLEVEL' as it is now in the smb.h header. (This used to be commit 2d0922b0eabfdc0aaf1d0797482fef47ed7fde8e) --- source3/lib/util_file.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 1d7b85f75c..3d072bb170 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -19,8 +19,6 @@ #include "includes.h" -extern int DEBUGLEVEL; - static int gotalarm; /*************************************************************** -- cgit From 9bcd133e9e7b0cfe974f273fb23409d660af8358 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 3 Oct 2001 12:18:20 +0000 Subject: switched over to a new method of handling uppercase/lowercase mappings for unicode strings. The new method relies on 3 files that are mmap'd at startup to provide the mapping tables. The upcase.dat and lowcase.dat tables should be the same on all systems. The valid.dat table says what characters are valid in 8.3 names, and differs between systems. I'm committing the japanese valid.dat here, in future we need some way of automatically installing and choosing a appropriate table. This commit also adds my mini tdb based gettext replacement in intl/lang_tdb.c. I have not enabled this yet and have not removed the old gettext code as the new code is still being looked at by Monyo. Right now the code assumes that the upcase.dat, lowcase.dat and valid.dat files are installed in the Samba lib directory. That is not a good choice, but I'll leave them there until we work out the new install directory structure for Samba 3.0. simo - please look at the isvalid_w() function and think about using it in your new mangling code. That should be the final step to correctly passing the chargen test code from monyo. (This used to be commit 1c221994f118dd542a158b2db51e07d04d0e9314) --- source3/lib/util_file.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 3d072bb170..77c0d7888e 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -422,6 +422,41 @@ char *file_load(const char *fname, size_t *size) } +/******************************************************************* +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 + int fd; + fd = open(fname, O_RDONLY, 0); + if (fd == -1) { + DEBUG(1,("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 || s2 != size) { + DEBUG(1,("incorrect size for %s - got %d expected %d\n", + fname, s2, size)); + if (p) free(p); + return NULL; + } + } + + return p; +} + + /**************************************************************************** parse a buffer into lines ****************************************************************************/ -- cgit From 81f56139b6964ddbe2c03232475f87f474136490 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 11 Oct 2001 07:42:52 +0000 Subject: initial kerberos/ADS/SPNEGO support in libsmb and smbclient. To activate you need to: - install krb5 libraries - run configure - build smbclient - run kinit to get a TGT - run smbclient with the -k option to choose kerberos auth (This used to be commit d33057585644e1337bac743e25ed7653bfb39eef) --- source3/lib/util_file.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 77c0d7888e..5ecf526280 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -573,3 +573,20 @@ void file_lines_slashcont(char **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) != length) { + return False; + } + close(fd); + return True; +} -- cgit From cf5a038adf0bd609e00039b44e197ea589eeb9cb Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 17 Dec 2001 11:13:57 +0000 Subject: obey "use mmap" on case tables (This used to be commit 505a1bdd15313698a6024a847f3771ea30a51f89) --- source3/lib/util_file.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 5ecf526280..0cd60fed26 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -430,17 +430,19 @@ void *map_file(char *fname, size_t size) size_t s2 = 0; void *p = NULL; #ifdef HAVE_MMAP - int fd; - fd = open(fname, O_RDONLY, 0); - if (fd == -1) { - DEBUG(1,("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; + if (lp_use_mmap()) { + int fd; + fd = open(fname, O_RDONLY, 0); + if (fd == -1) { + DEBUG(1,("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) { -- cgit From cd68afe31256ad60748b34f7318a180cfc2127cc Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Wed, 30 Jan 2002 06:08:46 +0000 Subject: Removed version number from file header. Changed "SMB/Netbios" to "SMB/CIFS" in file header. (This used to be commit 6a58c9bd06d0d7502a24bf5ce5a2faf0a146edfa) --- source3/lib/util_file.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 0cd60fed26..549766b137 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -1,5 +1,6 @@ /* - * 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 -- cgit From 079334a431bd214f327e8273f49a41acd740b71f Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Fri, 15 Mar 2002 09:19:07 +0000 Subject: lower the debug level of failing to map a file (This used to be commit ad9965414d4d1fd8a031e3169b8f19d66cdad8be) --- source3/lib/util_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 549766b137..88d03e7472 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -435,7 +435,7 @@ void *map_file(char *fname, size_t size) int fd; fd = open(fname, O_RDONLY, 0); if (fd == -1) { - DEBUG(1,("Failed to load %s - %s\n", fname, strerror(errno))); + 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); -- cgit From 64d20453d97f08e412a2dc51d8d131d630f63999 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 26 Mar 2002 22:36:27 +0000 Subject: Don't hold the mutex for more than 20 seconds. Jeremy. (This used to be commit 1b9f1a368f2f37700cef357ab4bbc0389ec06378) --- source3/lib/util_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 88d03e7472..e80267f84b 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -53,7 +53,7 @@ BOOL do_file_lock(int fd, int waitsecs, int type) alarm(waitsecs); ret = fcntl(fd, SMB_F_SETLKW, &lock); alarm(0); - CatchSignal(SIGALRM, SIGNAL_CAST SIG_DFL); + CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN); if (gotalarm) { DEBUG(0, ("do_file_lock: failed to %s file.\n", -- cgit From e90b65284812aaa5ff9e9935ce9bbad7791cbbcd Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Mon, 15 Jul 2002 10:35:28 +0000 Subject: updated the 3.0 branch from the head branch - ready for alpha18 (This used to be commit 03ac082dcb375b6f3ca3d810a6a6367542bc23ce) --- source3/lib/util_file.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index e80267f84b..611e0e40be 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -40,9 +40,10 @@ BOOL do_file_lock(int fd, int waitsecs, int type) { 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; @@ -51,9 +52,10 @@ BOOL do_file_lock(int fd, int waitsecs, int type) 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_IGN); + CatchSignal(SIGALRM, SIGNAL_CAST oldsig_handler); if (gotalarm) { DEBUG(0, ("do_file_lock: failed to %s file.\n", @@ -448,7 +450,8 @@ void *map_file(char *fname, size_t size) #endif if (!p) { p = file_load(fname, &s2); - if (!p || s2 != size) { + if (!p) return NULL; + if (s2 != size) { DEBUG(1,("incorrect size for %s - got %d expected %d\n", fname, s2, size)); if (p) free(p); -- cgit From 266ec4aac04cb8666234f18baa38ff6387f40cb3 Mon Sep 17 00:00:00 2001 From: Andrew Bartlett Date: Mon, 24 Feb 2003 03:09:08 +0000 Subject: Merge doxygen, signed/unsigned, const and other small fixes from HEAD to 3.0. Andrew Bartlett (This used to be commit 9ef0d40c3f8aef52ab321dc065264c42065bc876) --- source3/lib/util_file.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 611e0e40be..02acbd4d7e 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -362,7 +362,7 @@ char *file_pload(char *syscmd, size_t *size) while ((n = read(fd, buf, sizeof(buf))) > 0) { tp = Realloc(p, total + n + 1); if (!tp) { - DEBUG(0,("file_pload: failed to exand buffer!\n")); + DEBUG(0,("file_pload: failed to expand buffer!\n")); close(fd); SAFE_FREE(p); return NULL; @@ -372,6 +372,9 @@ char *file_pload(char *syscmd, size_t *size) } 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; @@ -590,7 +593,7 @@ BOOL file_save(const char *fname, void *packet, size_t length) if (fd == -1) { return False; } - if (write(fd, packet, length) != length) { + if (write(fd, packet, length) != (size_t)length) { return False; } close(fd); -- cgit From 77373f1f8e3b2f61e9bbcd9fadfb83257d390cf2 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Thu, 24 Jul 2003 23:46:27 +0000 Subject: More printf fixes - size_t is long on some architectures. (This used to be commit ba4d334b822248d8ab929c9568533431603d967e) --- source3/lib/util_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 02acbd4d7e..5e9732ce52 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -455,7 +455,7 @@ void *map_file(char *fname, size_t size) p = file_load(fname, &s2); if (!p) return NULL; if (s2 != size) { - DEBUG(1,("incorrect size for %s - got %d expected %d\n", + DEBUG(1,("incorrect size for %s - got %l expected %l\n", fname, s2, size)); if (p) free(p); return NULL; -- cgit From 7d833de662b83f026b54a236588da27dd8899630 Mon Sep 17 00:00:00 2001 From: Tim Potter Date: Fri, 25 Jul 2003 04:24:40 +0000 Subject: More printf portability fixes. Got caught out by some gcc'isms last time. )-: (This used to be commit 59dae1da66a5eb7e128263bd578f167d8746e9f0) --- source3/lib/util_file.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 5e9732ce52..638a6ca342 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -455,8 +455,8 @@ void *map_file(char *fname, size_t size) p = file_load(fname, &s2); if (!p) return NULL; if (s2 != size) { - DEBUG(1,("incorrect size for %s - got %l expected %l\n", - fname, 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; } -- cgit From 7a0b58a012590009a0d05430ea0c75ac4509830c Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Mon, 8 Sep 2003 14:00:51 +0000 Subject: fix compile error on HP-UX 10.20 (This used to be commit 15d53fd53c622de316d0ff41f1b60f0e2c69f908) --- source3/lib/util_file.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 638a6ca342..bd505ac921 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -20,6 +20,11 @@ #include "includes.h" +#ifndef MAP_FAILED +#define MAP_FAILED ((void *)-1) +#endif + + static int gotalarm; /*************************************************************** -- cgit From b0b317edbf444cfb4cc0006bbffb4b10a0b08f99 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Wed, 1 Sep 2004 01:33:55 +0000 Subject: r2155: Reformat, plus steal from Samba4 :-). tridge: the lp_use_mmap() in map_file() is inappropriate for 2 reasons, so I have removed it. - lp_use_mmap() is really meant to cope with systems that have broken mmap coherence, but map_file() doesn't need coherence, as its maps read only - map_file() is used to map the charset files before loadparm has loaded, so lp_use_mmap() is always returning false for the major use of map_file() Jeremy. (This used to be commit 3716dbc0cb9a8ca4027217b24dbf62a62f44e9f6) --- source3/lib/util_file.c | 492 +++++++++++++++++++++++++----------------------- 1 file changed, 259 insertions(+), 233 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index bd505ac921..303d961df5 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -24,7 +24,6 @@ #define MAP_FAILED ((void *)-1) #endif - static int gotalarm; /*************************************************************** @@ -33,7 +32,7 @@ static int gotalarm; static void gotalarm_sig(void) { - gotalarm = 1; + gotalarm = 1; } /*************************************************************** @@ -43,34 +42,33 @@ static void gotalarm_sig(void) BOOL do_file_lock(int fd, int waitsecs, int type) { - SMB_STRUCT_FLOCK lock; - int ret; - void (*oldsig_handler)(int); - - gotalarm = 0; - oldsig_handler = CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig); - - lock.l_type = type; - lock.l_whence = SEEK_SET; - lock.l_start = 0; - lock.l_len = 1; - 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 oldsig_handler); - - if (gotalarm) { - DEBUG(0, ("do_file_lock: failed to %s file.\n", - type == F_UNLCK ? "unlock" : "lock")); - return False; - } - - return (ret == 0); -} + SMB_STRUCT_FLOCK lock; + int ret; + void (*oldsig_handler)(int); + + gotalarm = 0; + oldsig_handler = CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig); + + lock.l_type = type; + lock.l_whence = SEEK_SET; + lock.l_start = 0; + lock.l_len = 1; + 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 oldsig_handler); + + if (gotalarm) { + DEBUG(0, ("do_file_lock: failed to %s file.\n", + type == F_UNLCK ? "unlock" : "lock")); + return False; + } + return (ret == 0); +} /*************************************************************** Lock an fd. Abandon after waitsecs seconds. @@ -78,21 +76,19 @@ BOOL do_file_lock(int fd, int waitsecs, int type) BOOL file_lock(int fd, int type, int secs, int *plock_depth) { - if (fd < 0) - return False; + if (fd < 0) + return False; - (*plock_depth)++; + (*plock_depth)++; - if ((*plock_depth) == 0) - { - if (!do_file_lock(fd, secs, type)) { - DEBUG(10,("file_lock: locking file failed, error = %s.\n", - strerror(errno))); - return False; - } - } + if ((*plock_depth) == 0) { + if (!do_file_lock(fd, secs, type)) { + DEBUG(10,("file_lock: locking file failed, error = %s.\n", strerror(errno))); + return False; + } + } - return True; + return True; } /*************************************************************** @@ -101,105 +97,107 @@ BOOL file_lock(int fd, int type, int secs, int *plock_depth) BOOL file_unlock(int fd, int *plock_depth) { - BOOL ret=True; + BOOL ret=True; - if(*plock_depth == 1) - ret = do_file_lock(fd, 5, F_UNLCK); + if(*plock_depth == 1) { + ret = do_file_lock(fd, 5, F_UNLCK); + } - (*plock_depth)--; + (*plock_depth)--; - if(!ret) - DEBUG(10,("file_unlock: unlocking file failed, error = %s.\n", - strerror(errno))); - return ret; + if(!ret) { + DEBUG(10,("file_unlock: unlocking file failed, error = %s.\n", strerror(errno))); + } + return ret; } /*************************************************************** - locks a file for enumeration / modification. + Locks a file for enumeration / modification. update to be set = True if modification is required. ****************************************************************/ void *startfilepwent(char *pfile, char *s_readbuf, int bufsize, int *file_lock_depth, BOOL update) { - FILE *fp = NULL; + FILE *fp = NULL; - if (!*pfile) - { - DEBUG(0, ("startfilepwent: No file set\n")); - return (NULL); - } - DEBUG(10, ("startfilepwent: opening file %s\n", pfile)); + if (!*pfile) { + DEBUG(0, ("startfilepwent: No file set\n")); + return (NULL); + } + DEBUG(10, ("startfilepwent: opening file %s\n", pfile)); - fp = sys_fopen(pfile, update ? "r+b" : "rb"); + fp = sys_fopen(pfile, update ? "r+b" : "rb"); - if (fp == NULL) { - DEBUG(0, ("startfilepwent: unable to open file %s\n", pfile)); - return NULL; - } + if (fp == NULL) { + DEBUG(0, ("startfilepwent: unable to open file %s\n", pfile)); + return NULL; + } - /* Set a buffer to do more efficient reads */ - setvbuf(fp, s_readbuf, _IOFBF, bufsize); + /* Set a buffer to do more efficient reads */ + setvbuf(fp, s_readbuf, _IOFBF, bufsize); - if (!file_lock(fileno(fp), (update ? F_WRLCK : F_RDLCK), 5, file_lock_depth)) - { - DEBUG(0, ("startfilepwent: unable to lock file %s\n", pfile)); - fclose(fp); - return NULL; - } + if (!file_lock(fileno(fp), (update ? F_WRLCK : F_RDLCK), 5, file_lock_depth)) { + DEBUG(0, ("startfilepwent: unable to lock file %s\n", pfile)); + fclose(fp); + return NULL; + } - /* Make sure it is only rw by the owner */ - chmod(pfile, 0600); + /* Make sure it is only rw by the owner */ + chmod(pfile, 0600); - /* We have a lock on the file. */ - return (void *)fp; + /* We have a lock on the file. */ + return (void *)fp; } /*************************************************************** End enumeration of the file. ****************************************************************/ + void endfilepwent(void *vp, int *file_lock_depth) { - FILE *fp = (FILE *)vp; + FILE *fp = (FILE *)vp; - file_unlock(fileno(fp), file_lock_depth); - fclose(fp); - DEBUG(7, ("endfilepwent: closed file.\n")); + file_unlock(fileno(fp), file_lock_depth); + fclose(fp); + DEBUG(7, ("endfilepwent: closed file.\n")); } /************************************************************************* Return the current position in the file list as an SMB_BIG_UINT. This must be treated as an opaque token. *************************************************************************/ + SMB_BIG_UINT getfilepwpos(void *vp) { - return (SMB_BIG_UINT)sys_ftell((FILE *)vp); + return (SMB_BIG_UINT)sys_ftell((FILE *)vp); } /************************************************************************* Set the current position in the file list from an SMB_BIG_UINT. This must be treated as an opaque token. *************************************************************************/ + BOOL setfilepwpos(void *vp, SMB_BIG_UINT tok) { - return !sys_fseek((FILE *)vp, (SMB_OFF_T)tok, SEEK_SET); + return !sys_fseek((FILE *)vp, (SMB_OFF_T)tok, SEEK_SET); } /************************************************************************* - gets a line out of a file. + Gets a line out of a file. line is of format "xxxx:xxxxxx:xxxxx:". lines with "#" at the front are ignored. *************************************************************************/ + int getfileline(void *vp, char *linebuf, int linebuf_size) { /* Static buffers we will return. */ FILE *fp = (FILE *)vp; unsigned char c; unsigned char *p; - size_t linebuf_len; + size_t linebuf_len; - if (fp == NULL) - { + if (fp == NULL) { DEBUG(0,("getfileline: Bad file pointer.\n")); return -1; } @@ -207,13 +205,11 @@ int getfileline(void *vp, char *linebuf, int linebuf_size) /* * Scan the file, a line at a time. */ - while (!feof(fp)) - { + while (!feof(fp)) { linebuf[0] = '\0'; fgets(linebuf, linebuf_size, fp); - if (ferror(fp)) - { + if (ferror(fp)) { return -1; } @@ -223,47 +219,38 @@ int getfileline(void *vp, char *linebuf, int linebuf_size) */ linebuf_len = strlen(linebuf); - if (linebuf_len == 0) - { + if (linebuf_len == 0) { linebuf[0] = '\0'; return 0; } - if (linebuf[linebuf_len - 1] != '\n') - { + if (linebuf[linebuf_len - 1] != '\n') { c = '\0'; - while (!ferror(fp) && !feof(fp)) - { + while (!ferror(fp) && !feof(fp)) { c = fgetc(fp); - if (c == '\n') - { + if (c == '\n') { break; } } - } - else - { + } else { linebuf[linebuf_len - 1] = '\0'; } #ifdef DEBUG_PASSWORD DEBUG(100, ("getfileline: got line |%s|\n", linebuf)); #endif - if ((linebuf[0] == 0) && feof(fp)) - { + if ((linebuf[0] == 0) && feof(fp)) { DEBUG(4, ("getfileline: end of file reached\n")); return 0; } - if (linebuf[0] == '#' || linebuf[0] == '\0') - { + if (linebuf[0] == '#' || linebuf[0] == '\0') { DEBUG(6, ("getfileline: skipping comment or blank line\n")); continue; } p = (unsigned char *) strchr_m(linebuf, ':'); - if (p == NULL) - { + if (p == NULL) { DEBUG(0, ("getfileline: malformed line entry (no :)\n")); continue; } @@ -272,85 +259,89 @@ int getfileline(void *vp, char *linebuf, int linebuf_size) return -1; } - /**************************************************************************** -read a line from a file with possible \ continuation chars. -Blanks at the start or end of a line are stripped. -The string will be allocated if s2 is NULL + Read a line from a file with possible \ continuation chars. + 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,XFILE *f) { - char *s=s2; - int len = 0; - int c; - BOOL start_of_line = True; - - if (x_feof(f)) - return(NULL); - - if (maxlen <2) return(NULL); - - if (!s2) - { - maxlen = MIN(maxlen,8); - s = (char *)malloc(maxlen); - } - - if (!s) return(NULL); - - *s = 0; - - while (len < maxlen-1) - { - c = x_getc(f); - switch (c) - { - case '\r': - break; - case '\n': - while (len > 0 && s[len-1] == ' ') - { - s[--len] = 0; - } - if (len > 0 && s[len-1] == '\\') - { - s[--len] = 0; - start_of_line = True; - break; - } - return(s); - case EOF: - if (len <= 0 && !s2) - SAFE_FREE(s); - return(len>0?s:NULL); - case ' ': - if (start_of_line) - break; - default: - start_of_line = False; - s[len++] = c; - s[len] = 0; - } - if (!s2 && len > maxlen-3) - { - char *t; + char *s=s2; + int len = 0; + int c; + BOOL start_of_line = True; + + if (x_feof(f)) { + return(NULL); + } + + if (maxlen <2) { + return(NULL); + } + + if (!s2) { + maxlen = MIN(maxlen,8); + s = (char *)malloc(maxlen); + } + + if (!s) { + return(NULL); + } + + *s = 0; + + while (len < maxlen-1) { + c = x_getc(f); + switch (c) { + case '\r': + break; + case '\n': + while (len > 0 && s[len-1] == ' ') { + s[--len] = 0; + } + if (len > 0 && s[len-1] == '\\') { + s[--len] = 0; + start_of_line = True; + break; + } + return(s); + case EOF: + if (len <= 0 && !s2) { + SAFE_FREE(s); + } + return(len>0?s:NULL); + case ' ': + if (start_of_line) { + break; + } + default: + start_of_line = False; + s[len++] = c; + s[len] = 0; + } + + if (!s2 && len > maxlen-3) { + char *t; - maxlen *= 2; - 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); + maxlen *= 2; + 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); } - /**************************************************************************** -load from a pipe into memory + Load from a pipe into memory. ****************************************************************************/ + char *file_pload(char *syscmd, size_t *size) { int fd, n; @@ -359,7 +350,9 @@ char *file_pload(char *syscmd, size_t *size) size_t total; fd = sys_popen(syscmd); - if (fd == -1) return NULL; + if (fd == -1) { + return NULL; + } p = NULL; total = 0; @@ -371,7 +364,9 @@ char *file_pload(char *syscmd, size_t *size) close(fd); SAFE_FREE(p); return NULL; - } else p = tp; + } else { + p = tp; + } memcpy(p+total, buf, n); total += n; } @@ -382,13 +377,15 @@ char *file_pload(char *syscmd, size_t *size) * truncated. */ sys_pclose(fd); - if (size) *size = total; + if (size) { + *size = total; + } return p; } /**************************************************************************** -load a file into memory from a fd. + Load a file into memory from a fd. ****************************************************************************/ char *fd_load(int fd, size_t *size) @@ -396,10 +393,14 @@ char *fd_load(int fd, size_t *size) SMB_STRUCT_STAT sbuf; char *p; - if (sys_fstat(fd, &sbuf) != 0) return NULL; + if (sys_fstat(fd, &sbuf) != 0) { + return NULL; + } p = (char *)malloc(sbuf.st_size+1); - if (!p) return NULL; + if (!p) { + return NULL; + } if (read(fd, p, sbuf.st_size) != sbuf.st_size) { SAFE_FREE(p); @@ -407,79 +408,85 @@ char *fd_load(int fd, size_t *size) } p[sbuf.st_size] = 0; - if (size) *size = sbuf.st_size; + if (size) { + *size = sbuf.st_size; + } return p; } /**************************************************************************** -load a file into memory + Load a file into memory. ****************************************************************************/ + char *file_load(const char *fname, size_t *size) { int fd; char *p; - if (!fname || !*fname) return NULL; + if (!fname || !*fname) { + return NULL; + } fd = open(fname,O_RDONLY); - if (fd == -1) return NULL; + if (fd == -1) { + return NULL; + } p = fd_load(fd, size); - close(fd); - return p; } - /******************************************************************* -mmap (if possible) or read a file + 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; - } + int fd; + fd = open(fname, O_RDONLY, 0); + if (fd == -1) { + DEBUG(2,("map_file: 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,("map_file: Failed to mmap %s - %s\n", fname, strerror(errno))); + return NULL; } #endif if (!p) { p = file_load(fname, &s2); - if (!p) return NULL; + if (!p) { + return NULL; + } if (s2 != size) { - DEBUG(1,("incorrect size for %s - got %lu expected %lu\n", + DEBUG(1,("map_file: incorrect size for %s - got %lu expected %lu\n", fname, (unsigned long)s2, (unsigned long)size)); - if (p) free(p); + SAFE_FREE(p); return NULL; } } - return p; } - /**************************************************************************** -parse a buffer into lines + Parse a buffer into lines. ****************************************************************************/ + static char **file_lines_parse(char *p, size_t size, int *numlines) { int i; char *s, **ret; - if (!p) return NULL; + if (!p) { + return NULL; + } for (s = p, i=0; s < p+size; s++) { if (s[0] == '\n') i++; @@ -491,7 +498,9 @@ static char **file_lines_parse(char *p, size_t size, int *numlines) return NULL; } memset(ret, 0, sizeof(ret[0])*(i+2)); - if (numlines) *numlines = i; + if (numlines) { + *numlines = i; + } ret[0] = p; for (s = p, i=0; s < p+size; s++) { @@ -500,75 +509,87 @@ static char **file_lines_parse(char *p, size_t size, int *numlines) i++; ret[i] = s+1; } - if (s[0] == '\r') s[0] = 0; + if (s[0] == '\r') { + s[0] = 0; + } } return ret; } - /**************************************************************************** -load a file into memory and return an array of pointers to lines in the file -must be freed with file_lines_free(). + Load a file into memory and return an array of pointers to lines in the file + must be freed with file_lines_free(). ****************************************************************************/ + char **file_lines_load(const char *fname, int *numlines) { char *p; size_t size; p = file_load(fname, &size); - if (!p) return NULL; + if (!p) { + return NULL; + } 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. + 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; + 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(). + Load a pipe into memory and return an array of pointers to lines in the data + must be freed with file_lines_free(). ****************************************************************************/ + char **file_lines_pload(char *syscmd, int *numlines) { char *p; size_t size; p = file_pload(syscmd, &size); - if (!p) return NULL; + if (!p) { + return NULL; + } return file_lines_parse(p, size, numlines); } /**************************************************************************** -free lines loaded with file_lines_load + Free lines loaded with file_lines_load. ****************************************************************************/ + void file_lines_free(char **lines) { - if (!lines) return; + if (!lines) { + return; + } SAFE_FREE(lines[0]); SAFE_FREE(lines); } - /**************************************************************************** -take a lislist of lines and modify them to produce a list where \ continues -a line + Take a list of lines and modify them to produce a list where \ continues + a line. ****************************************************************************/ + void file_lines_slashcont(char **lines) { int i, j; @@ -579,8 +600,12 @@ void file_lines_slashcont(char **lines) lines[i][len-1] = ' '; if (lines[i+1]) { char *p = &lines[i][len]; - while (p < lines[i+1]) *p++ = ' '; - for (j = i+1; lines[j]; j++) lines[j] = lines[j+1]; + while (p < lines[i+1]) { + *p++ = ' '; + } + for (j = i+1; lines[j]; j++) { + lines[j] = lines[j+1]; + } } } else { i++; @@ -588,9 +613,10 @@ void file_lines_slashcont(char **lines) } } -/* - save a lump of data into a file. Mostly used for debugging -*/ +/**************************************************************************** + 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; -- cgit From acf9d61421faa6c0055d57fdee7db300dc5431aa Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 7 Dec 2004 18:25:53 +0000 Subject: r4088: Get medieval on our ass about malloc.... :-). Take control of all our allocation functions so we can funnel through some well known functions. Should help greatly with malloc checking. HEAD patch to follow. Jeremy. (This used to be commit 620f2e608f70ba92f032720c031283d295c5c06a) --- source3/lib/util_file.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 303d961df5..963d610bef 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -282,7 +282,7 @@ char *fgets_slash(char *s2,int maxlen,XFILE *f) if (!s2) { maxlen = MIN(maxlen,8); - s = (char *)malloc(maxlen); + s = (char *)SMB_MALLOC(maxlen); } if (!s) { @@ -325,7 +325,7 @@ char *fgets_slash(char *s2,int maxlen,XFILE *f) char *t; maxlen *= 2; - t = (char *)Realloc(s,maxlen); + t = (char *)SMB_REALLOC(s,maxlen); if (!t) { DEBUG(0,("fgets_slash: failed to expand buffer!\n")); SAFE_FREE(s); @@ -358,7 +358,7 @@ char *file_pload(char *syscmd, size_t *size) total = 0; while ((n = read(fd, buf, sizeof(buf))) > 0) { - tp = Realloc(p, total + n + 1); + tp = SMB_REALLOC(p, total + n + 1); if (!tp) { DEBUG(0,("file_pload: failed to expand buffer!\n")); close(fd); @@ -397,7 +397,7 @@ char *fd_load(int fd, size_t *size) return NULL; } - p = (char *)malloc(sbuf.st_size+1); + p = (char *)SMB_MALLOC(sbuf.st_size+1); if (!p) { return NULL; } @@ -492,7 +492,7 @@ static char **file_lines_parse(char *p, size_t size, int *numlines) if (s[0] == '\n') i++; } - ret = (char **)malloc(sizeof(ret[0])*(i+2)); + ret = SMB_MALLOC_ARRAY(char *, i+2); if (!ret) { SAFE_FREE(p); return NULL; -- cgit From d1f91f7c723733113b4e9792042101c80dfc064c Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sat, 3 Dec 2005 06:46:46 +0000 Subject: r12043: It's amazing the warnings you find when compiling on a 64-bit box with gcc4 and -O6... Fix a bunch of C99 dereferencing type-punned pointer will break strict-aliasing rules errors. Also added prs_int32 (not uint32...) as it's needed in one place. Find places where prs_uint32 was being used to marshall/unmarshall a time_t (a big no no on 64-bits). More warning fixes to come. Thanks to Volker for nudging me to compile like this. Jeremy. (This used to be commit c65b752604f8f58abc4e7ae8514dc2c7f086271c) --- source3/lib/util_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 963d610bef..407a8b24fc 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -525,7 +525,7 @@ static char **file_lines_parse(char *p, size_t size, int *numlines) char **file_lines_load(const char *fname, int *numlines) { char *p; - size_t size; + size_t size = 0; p = file_load(fname, &size); if (!p) { -- cgit From 0af1500fc0bafe61019f1b2ab1d9e1d369221240 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Fri, 3 Feb 2006 22:19:41 +0000 Subject: r13316: Let the carnage begin.... Sync with trunk as off r13315 (This used to be commit 17e63ac4ed8325c0d44fe62b2442449f3298559f) --- source3/lib/util_file.c | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 407a8b24fc..53a9bc9b41 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -386,30 +386,37 @@ char *file_pload(char *syscmd, size_t *size) /**************************************************************************** Load a file into memory from a fd. + Truncate at maxsize. If maxsize == 0 - no limit. ****************************************************************************/ -char *fd_load(int fd, size_t *size) +char *fd_load(int fd, size_t *psize, size_t maxsize) { SMB_STRUCT_STAT sbuf; + size_t size; char *p; if (sys_fstat(fd, &sbuf) != 0) { return NULL; } - p = (char *)SMB_MALLOC(sbuf.st_size+1); + size = sbuf.st_size; + if (maxsize) { + size = MIN(size, maxsize); + } + + p = (char *)SMB_MALLOC(size+1); if (!p) { return NULL; } - if (read(fd, p, sbuf.st_size) != sbuf.st_size) { + if (read(fd, p, size) != size) { SAFE_FREE(p); return NULL; } - p[sbuf.st_size] = 0; + p[size] = 0; - if (size) { - *size = sbuf.st_size; + if (psize) { + *psize = size; } return p; @@ -419,7 +426,7 @@ char *fd_load(int fd, size_t *size) Load a file into memory. ****************************************************************************/ -char *file_load(const char *fname, size_t *size) +char *file_load(const char *fname, size_t *size, size_t maxsize) { int fd; char *p; @@ -433,7 +440,7 @@ char *file_load(const char *fname, size_t *size) return NULL; } - p = fd_load(fd, size); + p = fd_load(fd, size, maxsize); close(fd); return p; } @@ -461,7 +468,7 @@ void *map_file(char *fname, size_t size) } #endif if (!p) { - p = file_load(fname, &s2); + p = file_load(fname, &s2, 0); if (!p) { return NULL; } @@ -522,12 +529,12 @@ static char **file_lines_parse(char *p, size_t size, int *numlines) must be freed with file_lines_free(). ****************************************************************************/ -char **file_lines_load(const char *fname, int *numlines) +char **file_lines_load(const char *fname, int *numlines, size_t maxsize) { char *p; size_t size = 0; - p = file_load(fname, &size); + p = file_load(fname, &size, maxsize); if (!p) { return NULL; } @@ -541,12 +548,12 @@ char **file_lines_load(const char *fname, int *numlines) the list. ****************************************************************************/ -char **fd_lines_load(int fd, int *numlines) +char **fd_lines_load(int fd, int *numlines, size_t maxsize) { char *p; size_t size; - p = fd_load(fd, &size); + p = fd_load(fd, &size, maxsize); if (!p) { return NULL; } -- cgit From 894358a8f3e338b339b6c37233edef794b312087 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Tue, 7 Mar 2006 06:31:04 +0000 Subject: r13915: Fixed a very interesting class of realloc() bugs found by Coverity. realloc can return NULL in one of two cases - (1) the realloc failed, (2) realloc succeeded but the new size requested was zero, in which case this is identical to a free() call. The error paths dealing with these two cases should be different, but mostly weren't. Secondly the standard idiom for dealing with realloc when you know the new size is non-zero is the following : tmp = realloc(p, size); if (!tmp) { SAFE_FREE(p); return error; } else { p = tmp; } However, there were *many* *many* places in Samba where we were using the old (broken) idiom of : p = realloc(p, size) if (!p) { return error; } which will leak the memory pointed to by p on realloc fail. This commit (hopefully) fixes all these cases by moving to a standard idiom of : p = SMB_REALLOC(p, size) if (!p) { return error; } Where if the realloc returns null due to the realloc failing or size == 0 we *guarentee* that the storage pointed to by p has been freed. This allows me to remove a lot of code that was dealing with the standard (more verbose) method that required a tmp pointer. This is almost always what you want. When a realloc fails you never usually want the old memory, you want to free it and get into your error processing asap. For the 11 remaining cases where we really do need to keep the old pointer I have invented the new macro SMB_REALLOC_KEEP_OLD_ON_ERROR, which can be used as follows : tmp = SMB_REALLOC_KEEP_OLD_ON_ERROR(p, size); if (!tmp) { SAFE_FREE(p); return error; } else { p = tmp; } SMB_REALLOC_KEEP_OLD_ON_ERROR guarentees never to free the pointer p, even on size == 0 or realloc fail. All this is done by a hidden extra argument to Realloc(), BOOL free_old_on_error which is set appropriately by the SMB_REALLOC and SMB_REALLOC_KEEP_OLD_ON_ERROR macros (and their array counterparts). It remains to be seen what this will do to our Coverity bug count :-). Jeremy. (This used to be commit 1d710d06a214f3f1740e80e0bffd6aab44aac2b0) --- source3/lib/util_file.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 53a9bc9b41..06008886c0 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -322,16 +322,11 @@ char *fgets_slash(char *s2,int maxlen,XFILE *f) } if (!s2 && len > maxlen-3) { - char *t; - maxlen *= 2; - t = (char *)SMB_REALLOC(s,maxlen); - if (!t) { + s = (char *)SMB_REALLOC(s,maxlen); + if (!s) { DEBUG(0,("fgets_slash: failed to expand buffer!\n")); - SAFE_FREE(s); return(NULL); - } else { - s = t; } } } @@ -345,7 +340,7 @@ char *fgets_slash(char *s2,int maxlen,XFILE *f) char *file_pload(char *syscmd, size_t *size) { int fd, n; - char *p, *tp; + char *p; pstring buf; size_t total; @@ -358,19 +353,19 @@ char *file_pload(char *syscmd, size_t *size) total = 0; while ((n = read(fd, buf, sizeof(buf))) > 0) { - tp = SMB_REALLOC(p, total + n + 1); - if (!tp) { + p = SMB_REALLOC(p, total + n + 1); + if (!p) { 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; } - if (p) 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 -- cgit From bbf666e447132a5f6b206ddf9ca918298b756392 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Sat, 8 Apr 2006 17:25:31 +0000 Subject: r15003: patch based on code from Arkady Glabek to ensure that global memory is freed when unloading pam_winbind.so (needs more testing on non-linux platforms) (This used to be commit 1e0b79e591d70352a96e0a0487d8f394dc7b36ba) --- source3/lib/util_file.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 06008886c0..fff564aeb0 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -440,6 +440,26 @@ char *file_load(const char *fname, size_t *size, size_t maxsize) return p; } +/******************************************************************* + unmap or free memory +*******************************************************************/ + +BOOL unmap_file(void* start, size_t size) +{ +#ifdef HAVE_MMAP + if ( munmap( start, size ) != 0 ) { + DEBUG( 1, ("map_file: Failed to unmap address %X " + "of size %d - %s\n", + start, size, strerror(errno) )); + return False; + } + return True; +#else + SAFE_FREE( start ); + return True; +#endif +} + /******************************************************************* mmap (if possible) or read a file. ********************************************************************/ -- cgit From d8dfc29c9e4f1f1b7790d49c06f8bb5c301e509b Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Sun, 9 Apr 2006 01:20:26 +0000 Subject: r15005: Fix printf args to remove warnings. Jeremy. (This used to be commit 68d100830c5a6fa24b863071e8ca77ab264175a0) --- source3/lib/util_file.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index fff564aeb0..ed7be3f6c1 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -448,9 +448,9 @@ BOOL unmap_file(void* start, size_t size) { #ifdef HAVE_MMAP if ( munmap( start, size ) != 0 ) { - DEBUG( 1, ("map_file: Failed to unmap address %X " - "of size %d - %s\n", - start, size, strerror(errno) )); + DEBUG( 1, ("map_file: Failed to unmap address %p " + "of size %u - %s\n", + start, (unsigned int)size, strerror(errno) )); return False; } return True; -- cgit From e23781b3b304d1e69ad80af5ae9c0ed8d02cf996 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Sun, 30 Jul 2006 16:36:56 +0000 Subject: r17316: More C++ warnings -- 456 left (This used to be commit 1e4ee728df7eeafc1b4d533240acb032f73b4f5c) --- source3/lib/util_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index ed7be3f6c1..77ee95cc38 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -353,7 +353,7 @@ char *file_pload(char *syscmd, size_t *size) total = 0; while ((n = read(fd, buf, sizeof(buf))) > 0) { - p = SMB_REALLOC(p, total + n + 1); + p = (char *)SMB_REALLOC(p, total + n + 1); if (!p) { DEBUG(0,("file_pload: failed to expand buffer!\n")); close(fd); -- cgit From d0301937ede878a378677faa64ee2c06fec73681 Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 17 Aug 2006 15:04:53 +0000 Subject: r17592: Remove some unused functions pointed out by John E. Malmberg, make do_file_lock static to pdb_smbpasswd.c, the only user of it. Volker (This used to be commit 543f77a45f0a75ede48b0f2c674a0abdd386fed5) --- source3/lib/util_file.c | 235 ------------------------------------------------ 1 file changed, 235 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 77ee95cc38..1e0d880e83 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -24,241 +24,6 @@ #define MAP_FAILED ((void *)-1) #endif -static int gotalarm; - -/*************************************************************** - Signal function to tell us we timed out. -****************************************************************/ - -static void gotalarm_sig(void) -{ - gotalarm = 1; -} - -/*************************************************************** - Lock or unlock a fd for a known lock type. Abandon after waitsecs - seconds. -****************************************************************/ - -BOOL do_file_lock(int fd, int waitsecs, int type) -{ - SMB_STRUCT_FLOCK lock; - int ret; - void (*oldsig_handler)(int); - - gotalarm = 0; - oldsig_handler = CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig); - - lock.l_type = type; - lock.l_whence = SEEK_SET; - lock.l_start = 0; - lock.l_len = 1; - 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 oldsig_handler); - - if (gotalarm) { - DEBUG(0, ("do_file_lock: failed to %s file.\n", - type == F_UNLCK ? "unlock" : "lock")); - return False; - } - - return (ret == 0); -} - -/*************************************************************** - Lock an fd. Abandon after waitsecs seconds. -****************************************************************/ - -BOOL file_lock(int fd, int type, int secs, int *plock_depth) -{ - if (fd < 0) - return False; - - (*plock_depth)++; - - if ((*plock_depth) == 0) { - if (!do_file_lock(fd, secs, type)) { - DEBUG(10,("file_lock: locking file failed, error = %s.\n", strerror(errno))); - return False; - } - } - - return True; -} - -/*************************************************************** - Unlock an fd. Abandon after waitsecs seconds. -****************************************************************/ - -BOOL file_unlock(int fd, int *plock_depth) -{ - BOOL ret=True; - - if(*plock_depth == 1) { - ret = do_file_lock(fd, 5, F_UNLCK); - } - - (*plock_depth)--; - - if(!ret) { - DEBUG(10,("file_unlock: unlocking file failed, error = %s.\n", strerror(errno))); - } - return ret; -} - -/*************************************************************** - Locks a file for enumeration / modification. - update to be set = True if modification is required. -****************************************************************/ - -void *startfilepwent(char *pfile, char *s_readbuf, int bufsize, - int *file_lock_depth, BOOL update) -{ - FILE *fp = NULL; - - if (!*pfile) { - DEBUG(0, ("startfilepwent: No file set\n")); - return (NULL); - } - DEBUG(10, ("startfilepwent: opening file %s\n", pfile)); - - fp = sys_fopen(pfile, update ? "r+b" : "rb"); - - if (fp == NULL) { - DEBUG(0, ("startfilepwent: unable to open file %s\n", pfile)); - return NULL; - } - - /* Set a buffer to do more efficient reads */ - setvbuf(fp, s_readbuf, _IOFBF, bufsize); - - if (!file_lock(fileno(fp), (update ? F_WRLCK : F_RDLCK), 5, file_lock_depth)) { - DEBUG(0, ("startfilepwent: unable to lock file %s\n", pfile)); - fclose(fp); - return NULL; - } - - /* Make sure it is only rw by the owner */ - chmod(pfile, 0600); - - /* We have a lock on the file. */ - return (void *)fp; -} - -/*************************************************************** - End enumeration of the file. -****************************************************************/ - -void endfilepwent(void *vp, int *file_lock_depth) -{ - FILE *fp = (FILE *)vp; - - file_unlock(fileno(fp), file_lock_depth); - fclose(fp); - DEBUG(7, ("endfilepwent: closed file.\n")); -} - -/************************************************************************* - Return the current position in the file list as an SMB_BIG_UINT. - This must be treated as an opaque token. -*************************************************************************/ - -SMB_BIG_UINT getfilepwpos(void *vp) -{ - return (SMB_BIG_UINT)sys_ftell((FILE *)vp); -} - -/************************************************************************* - Set the current position in the file list from an SMB_BIG_UINT. - This must be treated as an opaque token. -*************************************************************************/ - -BOOL setfilepwpos(void *vp, SMB_BIG_UINT tok) -{ - return !sys_fseek((FILE *)vp, (SMB_OFF_T)tok, SEEK_SET); -} - -/************************************************************************* - Gets a line out of a file. - line is of format "xxxx:xxxxxx:xxxxx:". - lines with "#" at the front are ignored. -*************************************************************************/ - -int getfileline(void *vp, char *linebuf, int linebuf_size) -{ - /* Static buffers we will return. */ - FILE *fp = (FILE *)vp; - unsigned char c; - unsigned char *p; - size_t linebuf_len; - - if (fp == NULL) { - DEBUG(0,("getfileline: Bad file pointer.\n")); - return -1; - } - - /* - * Scan the file, a line at a time. - */ - while (!feof(fp)) { - linebuf[0] = '\0'; - - fgets(linebuf, linebuf_size, fp); - if (ferror(fp)) { - return -1; - } - - /* - * Check if the string is terminated with a newline - if not - * then we must keep reading and discard until we get one. - */ - - linebuf_len = strlen(linebuf); - if (linebuf_len == 0) { - linebuf[0] = '\0'; - return 0; - } - - if (linebuf[linebuf_len - 1] != '\n') { - c = '\0'; - while (!ferror(fp) && !feof(fp)) { - c = fgetc(fp); - if (c == '\n') { - break; - } - } - } else { - linebuf[linebuf_len - 1] = '\0'; - } - -#ifdef DEBUG_PASSWORD - DEBUG(100, ("getfileline: got line |%s|\n", linebuf)); -#endif - if ((linebuf[0] == 0) && feof(fp)) { - DEBUG(4, ("getfileline: end of file reached\n")); - return 0; - } - - if (linebuf[0] == '#' || linebuf[0] == '\0') { - DEBUG(6, ("getfileline: skipping comment or blank line\n")); - continue; - } - - p = (unsigned char *) strchr_m(linebuf, ':'); - if (p == NULL) { - DEBUG(0, ("getfileline: malformed line entry (no :)\n")); - continue; - } - return linebuf_len; - } - return -1; -} - /**************************************************************************** Read a line from a file with possible \ continuation chars. Blanks at the start or end of a line are stripped. -- cgit From 728bee7ff3672e7f658912179cd77d5dc4842e86 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Fri, 15 Dec 2006 01:50:04 +0000 Subject: r20179: Sync up with Samba4 - remove blank lines at the end parsing a file. Jeremy. (This used to be commit ea8215935e5f3952a4480c1f7dafafa9458f66a9) --- source3/lib/util_file.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 1e0d880e83..03246cad8e 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -285,9 +285,6 @@ static char **file_lines_parse(char *p, size_t size, int *numlines) return NULL; } memset(ret, 0, sizeof(ret[0])*(i+2)); - if (numlines) { - *numlines = i; - } ret[0] = p; for (s = p, i=0; s < p+size; s++) { @@ -301,6 +298,15 @@ static char **file_lines_parse(char *p, size_t size, int *numlines) } } + /* remove any blank lines at the end */ + while (i > 0 && ret[i-1][0] == 0) { + i--; + } + + if (numlines) { + *numlines = i; + } + return ret; } -- cgit From 8689b1ce99d7941c6548e75e8e8b8a2751de4c2c Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Fri, 29 Jun 2007 17:09:39 +0000 Subject: r23659: file_pload is not used outside of util_file.c (This used to be commit 3ec43e18a4ffc46700de484251ea0bb2a18cde78) --- source3/lib/util_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 03246cad8e..9c54c50b39 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -102,7 +102,7 @@ char *fgets_slash(char *s2,int maxlen,XFILE *f) Load from a pipe into memory. ****************************************************************************/ -char *file_pload(char *syscmd, size_t *size) +static char *file_pload(char *syscmd, size_t *size) { int fd, n; char *p; -- cgit From d824b98f80ba186030cbb70b3a1e5daf80469ecd Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Mon, 9 Jul 2007 19:25:36 +0000 Subject: r23779: Change from v2 or later to v3 or later. Jeremy. (This used to be commit 407e6e695b8366369b7c76af1ff76869b45347b3) --- source3/lib/util_file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 9c54c50b39..4ab37aadce 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -5,7 +5,7 @@ * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free - * Software Foundation; either version 2 of the License, or (at your option) + * Software Foundation; either version 3 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT -- cgit From 153cfb9c83534b09f15cc16205d7adb19b394928 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Tue, 10 Jul 2007 05:23:25 +0000 Subject: r23801: The FSF has moved around a lot. This fixes their Mass Ave address. (This used to be commit 87c91e4362c51819032bfbebbb273c52e203b227) --- source3/lib/util_file.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 4ab37aadce..0eb95e8284 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -14,8 +14,7 @@ * more details. * * You should have received a copy of the GNU General Public License along with - * this program; if not, write to the Free Software Foundation, Inc., 675 - * Mass Ave, Cambridge, MA 02139, USA. + * this program; if not, see . */ #include "includes.h" -- cgit From 30191d1a5704ad2b158386b511558972d539ce47 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 18 Oct 2007 17:40:25 -0700 Subject: RIP BOOL. Convert BOOL -> bool. I found a few interesting bugs in various places whilst doing this (places that assumed BOOL == int). I also need to fix the Samba4 pidl generation (next checkin). Jeremy. (This used to be commit f35a266b3cbb3e5fa6a86be60f34fe340a3ca71f) --- source3/lib/util_file.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 0eb95e8284..673a15df38 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -34,7 +34,7 @@ char *fgets_slash(char *s2,int maxlen,XFILE *f) char *s=s2; int len = 0; int c; - BOOL start_of_line = True; + bool start_of_line = True; if (x_feof(f)) { return(NULL); @@ -208,7 +208,7 @@ char *file_load(const char *fname, size_t *size, size_t maxsize) unmap or free memory *******************************************************************/ -BOOL unmap_file(void* start, size_t size) +bool unmap_file(void* start, size_t size) { #ifdef HAVE_MMAP if ( munmap( start, size ) != 0 ) { @@ -409,7 +409,7 @@ void file_lines_slashcont(char **lines) Save a lump of data into a file. Mostly used for debugging. ****************************************************************************/ -BOOL file_save(const char *fname, void *packet, size_t length) +bool file_save(const char *fname, void *packet, size_t length) { int fd; fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644); -- cgit From 68be9a820059ee96dd26c527efd7c14e679d3f2c Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 15 Nov 2007 14:19:52 -0800 Subject: More pstring removal. This one was tricky. I had to add one horror (pstring_clean_name()) which will have to remain until I've removed all pstrings from the client code. Jeremy. (This used to be commit 1ea3ac80146b83c2522b69e7747c823366a2b47d) --- source3/lib/util_file.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'source3/lib/util_file.c') diff --git a/source3/lib/util_file.c b/source3/lib/util_file.c index 673a15df38..b628b06cc6 100644 --- a/source3/lib/util_file.c +++ b/source3/lib/util_file.c @@ -105,9 +105,9 @@ static char *file_pload(char *syscmd, size_t *size) { int fd, n; char *p; - pstring buf; + char buf[1024]; size_t total; - + fd = sys_popen(syscmd); if (fd == -1) { return NULL; -- cgit