2008-04-07 17:01:44 +04:00
/*
* Unix SMB / CIFS implementation .
* libsmbconf - Samba configuration library , utility functions
* Copyright ( C ) Michael Adam 2008
*
* 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 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 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 , see < http : //www.gnu.org/licenses/>.
*/
# include "includes.h"
# include "smbconf_private.h"
static int smbconf_destroy_ctx ( struct smbconf_ctx * ctx )
{
return ctx - > ops - > shutdown ( ctx ) ;
}
/**
* Initialize the configuration .
*
* This should be the first function in a sequence of calls to smbconf
* functions :
*
* Upon success , this creates and returns the conf context
* that should be passed around in subsequent calls to the other
* smbconf functions .
*
* After the work with the configuration is completed , smbconf_shutdown ( )
* should be called .
*/
2008-04-13 18:26:14 +04:00
WERROR smbconf_init_internal ( TALLOC_CTX * mem_ctx , struct smbconf_ctx * * conf_ctx ,
const char * path , struct smbconf_ops * ops )
2008-04-07 17:01:44 +04:00
{
WERROR werr = WERR_OK ;
struct smbconf_ctx * ctx ;
if ( conf_ctx = = NULL ) {
return WERR_INVALID_PARAM ;
}
2009-01-21 20:50:16 +03:00
ctx = talloc_zero ( mem_ctx , struct smbconf_ctx ) ;
2008-04-07 17:01:44 +04:00
if ( ctx = = NULL ) {
return WERR_NOMEM ;
}
ctx - > ops = ops ;
werr = ctx - > ops - > init ( ctx , path ) ;
if ( ! W_ERROR_IS_OK ( werr ) ) {
goto fail ;
}
talloc_set_destructor ( ctx , smbconf_destroy_ctx ) ;
* conf_ctx = ctx ;
return werr ;
fail :
2009-01-21 19:11:02 +03:00
talloc_free ( ctx ) ;
2008-04-07 17:01:44 +04:00
return werr ;
}
/**
* add a string to a talloced array of strings .
*/
WERROR smbconf_add_string_to_array ( TALLOC_CTX * mem_ctx ,
char * * * array ,
uint32_t count ,
const char * string )
{
char * * new_array = NULL ;
2008-04-15 15:49:59 +04:00
if ( array = = NULL ) {
2008-04-07 17:01:44 +04:00
return WERR_INVALID_PARAM ;
}
2009-01-21 20:50:16 +03:00
new_array = talloc_realloc ( mem_ctx , * array , char * , count + 1 ) ;
2008-04-07 17:01:44 +04:00
if ( new_array = = NULL ) {
return WERR_NOMEM ;
}
2008-04-15 15:49:59 +04:00
if ( string = = NULL ) {
new_array [ count ] = NULL ;
} else {
new_array [ count ] = talloc_strdup ( new_array , string ) ;
if ( new_array [ count ] = = NULL ) {
2009-01-21 19:11:02 +03:00
talloc_free ( new_array ) ;
2008-04-15 15:49:59 +04:00
return WERR_NOMEM ;
}
2008-04-07 17:01:44 +04:00
}
* array = new_array ;
return WERR_OK ;
}
2008-04-07 17:09:28 +04:00
bool smbconf_find_in_array ( const char * string , char * * list ,
uint32_t num_entries , uint32_t * entry )
{
uint32_t i ;
2008-04-15 15:50:27 +04:00
if ( list = = NULL ) {
2008-04-07 17:09:28 +04:00
return false ;
}
for ( i = 0 ; i < num_entries ; i + + ) {
2008-04-15 15:50:27 +04:00
if ( ( ( string = = NULL ) & & ( list [ i ] = = NULL ) ) | |
strequal ( string , list [ i ] ) )
{
2008-04-07 17:09:28 +04:00
if ( entry ! = NULL ) {
* entry = i ;
}
return true ;
}
}
return false ;
}
2008-04-08 02:03:39 +04:00
bool smbconf_reverse_find_in_array ( const char * string , char * * list ,
uint32_t num_entries , uint32_t * entry )
{
2008-07-02 02:51:54 +04:00
int32_t i ;
2008-04-08 02:03:39 +04:00
if ( ( string = = NULL ) | | ( list = = NULL ) | | ( num_entries = = 0 ) ) {
return false ;
}
2008-07-02 02:28:13 +04:00
for ( i = num_entries - 1 ; i > = 0 ; i - - ) {
2008-04-08 02:03:39 +04:00
if ( strequal ( string , list [ i ] ) ) {
if ( entry ! = NULL ) {
* entry = i ;
}
return true ;
}
}
return false ;
}