1998-11-05 19:51:34 +03:00
/*
2002-01-30 09:08:46 +03:00
* Unix SMB / CIFS implementation .
* SMB parameters and setup
1998-11-05 19:51:34 +03:00
* 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
2007-07-09 23:25:36 +04:00
* Software Foundation ; either version 3 of the License , or ( at your option )
1998-11-05 19:51:34 +03:00
* 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
2007-07-10 09:23:25 +04:00
* this program ; if not , see < http : //www.gnu.org/licenses/>.
1998-11-05 19:51:34 +03:00
*/
# include "includes.h"
2003-09-08 18:00:51 +04:00
# ifndef MAP_FAILED
# define MAP_FAILED ((void *)-1)
# endif
1998-11-05 19:51:34 +03:00
/****************************************************************************
2004-09-01 05:33:55 +04:00
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 .
1998-11-05 19:51:34 +03:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-09-01 05:33:55 +04:00
2001-09-10 16:46:42 +04:00
char * fgets_slash ( char * s2 , int maxlen , XFILE * f )
1998-11-05 19:51:34 +03:00
{
2004-09-01 05:33:55 +04:00
char * s = s2 ;
int len = 0 ;
int c ;
2007-10-19 04:40:25 +04:00
bool start_of_line = True ;
2004-09-01 05:33:55 +04:00
if ( x_feof ( f ) ) {
return ( NULL ) ;
}
if ( maxlen < 2 ) {
return ( NULL ) ;
}
if ( ! s2 ) {
maxlen = MIN ( maxlen , 8 ) ;
2004-12-07 21:25:53 +03:00
s = ( char * ) SMB_MALLOC ( maxlen ) ;
2004-09-01 05:33:55 +04:00
}
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 ) {
maxlen * = 2 ;
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)
2006-03-07 09:31:04 +03:00
s = ( char * ) SMB_REALLOC ( s , maxlen ) ;
if ( ! s ) {
2004-09-01 05:33:55 +04:00
DEBUG ( 0 , ( " fgets_slash: failed to expand buffer! \n " ) ) ;
return ( NULL ) ;
}
}
}
return ( s ) ;
1998-11-05 19:51:34 +03:00
}
2000-04-16 10:19:11 +04:00
/****************************************************************************
2004-09-01 05:33:55 +04:00
Load from a pipe into memory .
2000-04-16 10:19:11 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-09-01 05:33:55 +04:00
2007-06-29 21:09:39 +04:00
static char * file_pload ( char * syscmd , size_t * size )
2000-04-16 10:19:11 +04:00
{
2000-04-16 15:00:21 +04:00
int fd , n ;
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)
2006-03-07 09:31:04 +03:00
char * p ;
2007-11-16 01:19:52 +03:00
char buf [ 1024 ] ;
2000-04-16 15:00:21 +04:00
size_t total ;
2007-11-16 01:19:52 +03:00
2000-04-16 15:00:21 +04:00
fd = sys_popen ( syscmd ) ;
2004-09-01 05:33:55 +04:00
if ( fd = = - 1 ) {
return NULL ;
}
2000-04-16 15:00:21 +04:00
p = NULL ;
total = 0 ;
while ( ( n = read ( fd , buf , sizeof ( buf ) ) ) > 0 ) {
2006-07-30 20:36:56 +04:00
p = ( char * ) SMB_REALLOC ( p , total + n + 1 ) ;
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)
2006-03-07 09:31:04 +03:00
if ( ! p ) {
2003-02-24 06:09:08 +03:00
DEBUG ( 0 , ( " file_pload: failed to expand buffer! \n " ) ) ;
2000-04-16 15:00:21 +04:00
close ( fd ) ;
return NULL ;
2004-09-01 05:33:55 +04:00
}
2000-04-16 15:00:21 +04:00
memcpy ( p + total , buf , n ) ;
total + = n ;
}
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)
2006-03-07 09:31:04 +03:00
if ( p ) {
p [ total ] = 0 ;
}
2000-04-16 15:00:21 +04:00
2003-02-24 06:09:08 +03:00
/* FIXME: Perhaps ought to check that the command completed
* successfully ( returned 0 ) ; if not the data may be
* truncated . */
2000-04-16 15:00:21 +04:00
sys_pclose ( fd ) ;
2004-09-01 05:33:55 +04:00
if ( size ) {
* size = total ;
}
2000-04-16 15:00:21 +04:00
return p ;
}
/****************************************************************************
2004-09-01 05:33:55 +04:00
Load a file into memory from a fd .
2006-02-04 01:19:41 +03:00
Truncate at maxsize . If maxsize = = 0 - no limit .
2001-04-13 04:37:00 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2006-02-04 01:19:41 +03:00
char * fd_load ( int fd , size_t * psize , size_t maxsize )
2000-04-16 15:00:21 +04:00
{
2000-04-16 10:19:11 +04:00
SMB_STRUCT_STAT sbuf ;
2006-02-04 01:19:41 +03:00
size_t size ;
2000-04-16 15:00:21 +04:00
char * p ;
2000-04-18 06:17:46 +04:00
2004-09-01 05:33:55 +04:00
if ( sys_fstat ( fd , & sbuf ) ! = 0 ) {
return NULL ;
}
2000-04-16 10:19:11 +04:00
2006-02-04 01:19:41 +03:00
size = sbuf . st_size ;
if ( maxsize ) {
size = MIN ( size , maxsize ) ;
}
p = ( char * ) SMB_MALLOC ( size + 1 ) ;
2004-09-01 05:33:55 +04:00
if ( ! p ) {
return NULL ;
}
2000-04-16 10:19:11 +04:00
2006-02-04 01:19:41 +03:00
if ( read ( fd , p , size ) ! = size ) {
2001-09-17 06:19:44 +04:00
SAFE_FREE ( p ) ;
2000-04-16 10:19:11 +04:00
return NULL ;
}
2006-02-04 01:19:41 +03:00
p [ size ] = 0 ;
2000-04-16 10:19:11 +04:00
2006-02-04 01:19:41 +03:00
if ( psize ) {
* psize = size ;
2004-09-01 05:33:55 +04:00
}
2000-04-16 15:00:21 +04:00
return p ;
}
2001-04-13 04:37:00 +04:00
/****************************************************************************
2004-09-01 05:33:55 +04:00
Load a file into memory .
2001-04-13 04:37:00 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-09-01 05:33:55 +04:00
2006-02-04 01:19:41 +03:00
char * file_load ( const char * fname , size_t * size , size_t maxsize )
2001-04-13 04:37:00 +04:00
{
int fd ;
char * p ;
2004-09-01 05:33:55 +04:00
if ( ! fname | | ! * fname ) {
return NULL ;
}
2001-04-13 04:37:00 +04:00
fd = open ( fname , O_RDONLY ) ;
2004-09-01 05:33:55 +04:00
if ( fd = = - 1 ) {
return NULL ;
}
2001-04-13 04:37:00 +04:00
2006-02-04 01:19:41 +03:00
p = fd_load ( fd , size , maxsize ) ;
2001-04-13 04:37:00 +04:00
close ( fd ) ;
return p ;
}
2006-04-08 21:25:31 +04:00
/*******************************************************************
unmap or free memory
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool unmap_file ( void * start , size_t size )
2006-04-08 21:25:31 +04:00
{
# ifdef HAVE_MMAP
if ( munmap ( start , size ) ! = 0 ) {
2006-04-09 05:20:26 +04:00
DEBUG ( 1 , ( " map_file: Failed to unmap address %p "
" of size %u - %s \n " ,
start , ( unsigned int ) size , strerror ( errno ) ) ) ;
2006-04-08 21:25:31 +04:00
return False ;
}
return True ;
# else
SAFE_FREE ( start ) ;
return True ;
# endif
}
2001-10-03 16:18:20 +04:00
/*******************************************************************
2004-09-01 05:33:55 +04:00
mmap ( if possible ) or read a file .
2001-10-03 16:18:20 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-09-01 05:33:55 +04:00
2001-10-03 16:18:20 +04:00
void * map_file ( char * fname , size_t size )
{
size_t s2 = 0 ;
void * p = NULL ;
# ifdef HAVE_MMAP
2004-09-01 05:33:55 +04:00
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 ;
2001-10-03 16:18:20 +04:00
}
# endif
if ( ! p ) {
2006-02-04 01:19:41 +03:00
p = file_load ( fname , & s2 , 0 ) ;
2004-09-01 05:33:55 +04:00
if ( ! p ) {
return NULL ;
}
2002-07-15 14:35:28 +04:00
if ( s2 ! = size ) {
2004-09-01 05:33:55 +04:00
DEBUG ( 1 , ( " map_file: incorrect size for %s - got %lu expected %lu \n " ,
2003-07-25 08:24:40 +04:00
fname , ( unsigned long ) s2 , ( unsigned long ) size ) ) ;
2004-09-01 05:33:55 +04:00
SAFE_FREE ( p ) ;
2001-10-03 16:18:20 +04:00
return NULL ;
}
}
return p ;
}
2000-04-16 15:00:21 +04:00
/****************************************************************************
2004-09-01 05:33:55 +04:00
Parse a buffer into lines .
2000-04-16 15:00:21 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-09-01 05:33:55 +04:00
2001-07-04 11:15:53 +04:00
static char * * file_lines_parse ( char * p , size_t size , int * numlines )
2000-04-16 15:00:21 +04:00
{
int i ;
char * s , * * ret ;
2004-09-01 05:33:55 +04:00
if ( ! p ) {
return NULL ;
}
2000-04-16 15:00:21 +04:00
2000-04-16 10:19:11 +04:00
for ( s = p , i = 0 ; s < p + size ; s + + ) {
if ( s [ 0 ] = = ' \n ' ) i + + ;
}
2004-12-07 21:25:53 +03:00
ret = SMB_MALLOC_ARRAY ( char * , i + 2 ) ;
2000-04-16 10:19:11 +04:00
if ( ! ret ) {
2001-09-17 06:19:44 +04:00
SAFE_FREE ( p ) ;
2000-04-16 10:19:11 +04:00
return NULL ;
}
2000-04-16 13:40:02 +04:00
memset ( ret , 0 , sizeof ( ret [ 0 ] ) * ( i + 2 ) ) ;
2000-04-16 10:19:11 +04:00
ret [ 0 ] = p ;
for ( s = p , i = 0 ; s < p + size ; s + + ) {
if ( s [ 0 ] = = ' \n ' ) {
s [ 0 ] = 0 ;
i + + ;
ret [ i ] = s + 1 ;
}
2004-09-01 05:33:55 +04:00
if ( s [ 0 ] = = ' \r ' ) {
s [ 0 ] = 0 ;
}
2000-04-16 10:19:11 +04:00
}
2006-12-15 04:50:04 +03:00
/* remove any blank lines at the end */
while ( i > 0 & & ret [ i - 1 ] [ 0 ] = = 0 ) {
i - - ;
}
if ( numlines ) {
* numlines = i ;
}
2000-04-16 10:19:11 +04:00
return ret ;
}
2000-04-16 15:00:21 +04:00
/****************************************************************************
2004-09-01 05:33:55 +04:00
Load a file into memory and return an array of pointers to lines in the file
must be freed with file_lines_free ( ) .
2000-04-16 15:00:21 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-09-01 05:33:55 +04:00
2006-02-04 01:19:41 +03:00
char * * file_lines_load ( const char * fname , int * numlines , size_t maxsize )
2000-04-16 15:00:21 +04:00
{
char * p ;
2005-12-03 09:46:46 +03:00
size_t size = 0 ;
2000-04-16 15:00:21 +04:00
2006-02-04 01:19:41 +03:00
p = file_load ( fname , & size , maxsize ) ;
2004-09-01 05:33:55 +04:00
if ( ! p ) {
return NULL ;
}
2000-04-16 15:00:21 +04:00
2001-07-04 11:15:53 +04:00
return file_lines_parse ( p , size , numlines ) ;
2000-04-16 15:00:21 +04:00
}
2001-04-13 04:37:00 +04:00
/****************************************************************************
2004-09-01 05:33:55 +04:00
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 .
2001-04-13 04:37:00 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-09-01 05:33:55 +04:00
2006-02-04 01:19:41 +03:00
char * * fd_lines_load ( int fd , int * numlines , size_t maxsize )
2001-04-13 04:37:00 +04:00
{
char * p ;
size_t size ;
2006-02-04 01:19:41 +03:00
p = fd_load ( fd , & size , maxsize ) ;
2004-09-01 05:33:55 +04:00
if ( ! p ) {
return NULL ;
}
2001-04-13 04:37:00 +04:00
2001-07-04 11:15:53 +04:00
return file_lines_parse ( p , size , numlines ) ;
2001-04-13 04:37:00 +04:00
}
2000-04-16 15:00:21 +04:00
/****************************************************************************
2004-09-01 05:33:55 +04:00
Load a pipe into memory and return an array of pointers to lines in the data
must be freed with file_lines_free ( ) .
2000-04-16 15:00:21 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-09-01 05:33:55 +04:00
2001-07-04 11:15:53 +04:00
char * * file_lines_pload ( char * syscmd , int * numlines )
2000-04-16 15:00:21 +04:00
{
char * p ;
size_t size ;
p = file_pload ( syscmd , & size ) ;
2004-09-01 05:33:55 +04:00
if ( ! p ) {
return NULL ;
}
2000-04-16 15:00:21 +04:00
2001-07-04 11:15:53 +04:00
return file_lines_parse ( p , size , numlines ) ;
2000-04-16 15:00:21 +04:00
}
2000-04-16 10:19:11 +04:00
/****************************************************************************
2004-09-01 05:33:55 +04:00
Free lines loaded with file_lines_load .
2000-04-16 10:19:11 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-09-01 05:33:55 +04:00
2000-04-16 10:19:11 +04:00
void file_lines_free ( char * * lines )
{
2004-09-01 05:33:55 +04:00
if ( ! lines ) {
return ;
}
2001-09-17 06:19:44 +04:00
SAFE_FREE ( lines [ 0 ] ) ;
SAFE_FREE ( lines ) ;
2000-04-16 10:19:11 +04:00
}
2000-04-16 13:40:02 +04:00
2000-04-16 15:17:19 +04:00
/****************************************************************************
2004-09-01 05:33:55 +04:00
Take a list of lines and modify them to produce a list where \ continues
a line .
2000-04-16 15:17:19 +04:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2004-09-01 05:33:55 +04:00
2000-04-16 15:17:19 +04:00
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 ] ;
2004-09-01 05:33:55 +04:00
while ( p < lines [ i + 1 ] ) {
* p + + = ' ' ;
}
for ( j = i + 1 ; lines [ j ] ; j + + ) {
lines [ j ] = lines [ j + 1 ] ;
}
2000-04-16 15:17:19 +04:00
}
} else {
i + + ;
}
}
}
2001-10-11 11:42:52 +04:00
2004-09-01 05:33:55 +04:00
/****************************************************************************
Save a lump of data into a file . Mostly used for debugging .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2007-10-19 04:40:25 +04:00
bool file_save ( const char * fname , void * packet , size_t length )
2001-10-11 11:42:52 +04:00
{
int fd ;
fd = open ( fname , O_WRONLY | O_CREAT | O_TRUNC , 0644 ) ;
if ( fd = = - 1 ) {
return False ;
}
2003-02-24 06:09:08 +03:00
if ( write ( fd , packet , length ) ! = ( size_t ) length ) {
2001-10-11 11:42:52 +04:00
return False ;
}
close ( fd ) ;
return True ;
}