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 .
*/
2011-04-07 19:19:03 +04:00
sbcErr smbconf_init_internal ( TALLOC_CTX * mem_ctx , struct smbconf_ctx * * conf_ctx ,
2008-04-13 18:26:14 +04:00
const char * path , struct smbconf_ops * ops )
2008-04-07 17:01:44 +04:00
{
2011-04-07 19:19:03 +04:00
sbcErr err = SBC_ERR_OK ;
2008-04-07 17:01:44 +04:00
struct smbconf_ctx * ctx ;
if ( conf_ctx = = NULL ) {
2011-04-07 19:19:03 +04:00
return SBC_ERR_INVALID_PARAM ;
2008-04-07 17:01:44 +04:00
}
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 ) {
2011-04-07 19:19:03 +04:00
return SBC_ERR_NOMEM ;
2008-04-07 17:01:44 +04:00
}
ctx - > ops = ops ;
2011-04-07 19:19:03 +04:00
err = ctx - > ops - > init ( ctx , path ) ;
if ( ! SBC_ERROR_IS_OK ( err ) ) {
2008-04-07 17:01:44 +04:00
goto fail ;
}
talloc_set_destructor ( ctx , smbconf_destroy_ctx ) ;
* conf_ctx = ctx ;
2011-04-07 19:19:03 +04:00
return err ;
2008-04-07 17:01:44 +04:00
fail :
2009-01-21 19:11:02 +03:00
talloc_free ( ctx ) ;
2011-04-07 19:19:03 +04:00
return err ;
2008-04-07 17:01:44 +04:00
}
/**
* add a string to a talloced array of strings .
*/
2011-04-08 16:19:15 +04:00
sbcErr smbconf_add_string_to_array ( TALLOC_CTX * mem_ctx ,
2008-04-07 17:01:44 +04:00
char * * * array ,
uint32_t count ,
const char * string )
{
char * * new_array = NULL ;
2008-04-15 15:49:59 +04:00
if ( array = = NULL ) {
2011-04-08 16:19:15 +04:00
return SBC_ERR_INVALID_PARAM ;
2008-04-07 17:01:44 +04:00
}
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 ) {
2011-04-08 16:19:15 +04:00
return SBC_ERR_NOMEM ;
2008-04-07 17:01:44 +04:00
}
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 ) ;
2011-04-08 16:19:15 +04:00
return SBC_ERR_NOMEM ;
2008-04-15 15:49:59 +04:00
}
2008-04-07 17:01:44 +04:00
}
* array = new_array ;
2011-04-08 16:19:15 +04:00
return SBC_ERR_OK ;
2008-04-07 17:01:44 +04:00
}
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 ;
}