/* Compile: g++ -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -I/usr/local/samba/include -L/usr/local/samba/lib -lsmbclient smbtest.cpp -o smbtest */ #include #include #include #include using namespace std; #define MAX_BUFF_SIZE 255 char g_workgroup[MAX_BUFF_SIZE]; char g_username[MAX_BUFF_SIZE]; char g_password[MAX_BUFF_SIZE]; char g_server[MAX_BUFF_SIZE]; char g_share[MAX_BUFF_SIZE]; void auth_fn(const char* server, const char* share, char* workgroup, int wgmaxlen, char* username, int unmaxlen, char* password, int pwmaxlen) { strncpy(workgroup, "", 0); // strncpy(workgroup, g_workgroup, wgmaxlen - 1); strncpy(username, g_username, unmaxlen - 1); strncpy(password, g_password, pwmaxlen - 1); strcpy(g_server, server); strcpy(g_share, share); } void GetContext(SMBCCTX** _ppContext ) { if( *_ppContext != NULL ) { smbc_free_context( *_ppContext, 1 ); } *_ppContext = smbc_new_context(); if( !(*_ppContext) ) throw exception(); (*_ppContext)->debug = 0; (*_ppContext)->callbacks.auth_fn = auth_fn; (*_ppContext)->timeout = 5000; (*_ppContext)->options.urlencode_readdir_entries = 1; // initialize the context using the previously specified options if( !smbc_init_context(*_ppContext) ) { smbc_free_context( *_ppContext, 1 ); throw exception(); } smbc_option_set( *_ppContext, "full_time_names", 1 ); } int main( int _nArgC, char** _ppArgV ) { // edit these strcpy( g_workgroup, "" ); strcpy( g_username, "jonas" ); strcpy( g_password, "xxxxx" ); strcpy( g_server, "192.168.1.xxx" ); strcpy( g_share, "jonas" ); // --- string strURL( "smb://192.168.1.203/jonas/testdir" ); cout << strURL << endl; SMBCCTX *pCtx = NULL; GetContext( &pCtx ); SMBCFILE *pDir = pCtx->opendir( pCtx, strURL.c_str() ); struct smbc_dirent *pDirEnt; while( pDirEnt = pCtx->readdir(pCtx, pDir) ) { switch( pDirEnt->smbc_type ) { case SMBC_FILE: cout << setw(8) << ""; break; case SMBC_DIR: cout << setw(8) << left << ""; break; default: cout << setw(8) << left << ""; break; } cout << "name: " << setw(30) << left << pDirEnt->name; size_t nBuffSize = strlen(pDirEnt->name) + 1; char *pBuffer = new char[nBuffSize]; int nReturnCode = smbc_urldecode( pBuffer, pDirEnt->name, nBuffSize ); if( 0 == nReturnCode ) cout << "decoded: " << setw(30) << left << pBuffer; delete[] pBuffer; cout << endl; } return 0; }