2022-08-17 11:51:06 +03:00
/*
2003-01-23 02:50:14 +03:00
Unix SMB / CIFS implementation .
2001-05-17 06:29:00 +04:00
SMB client library test program
Copyright ( C ) Andrew Tridgell 1998
Copyright ( C ) Richard Sharpe 2000
Copyright ( C ) John Terpsra 2000
2022-08-17 11:51:06 +03:00
2001-05-17 06:29:00 +04:00
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
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
2001-05-17 06:29:00 +04:00
( at your option ) any later version .
2022-08-17 11:51:06 +03:00
2001-05-17 06:29:00 +04:00
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 .
2022-08-17 11:51:06 +03:00
2001-05-17 06:29:00 +04:00
You should have received a copy of the GNU General Public License
2007-07-10 04:57:11 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2001-05-17 06:29:00 +04:00
*/
# include <stdio.h>
# include <errno.h>
2009-03-28 01:03:00 +03:00
# include <time.h>
2001-05-17 06:29:00 +04:00
# include <sys/time.h>
# include <string.h>
# include <unistd.h>
# include <stdlib.h>
2008-03-04 02:13:33 +03:00
# include "libsmbclient.h"
2005-03-31 09:06:04 +04:00
# include "get_auth_data_fn.h"
2001-05-17 06:29:00 +04:00
int main ( int argc , char * argv [ ] )
{
2022-08-17 11:51:06 +03:00
int err , fd , dh1 , dsize , dirc ;
const char * file = " smb://samba/public/testfile.txt " ;
const char * file2 = " smb://samba/public/testfile2.txt " ;
char buff [ 256 ] ;
char dirbuf [ 512 ] ;
char * dirp ;
struct stat st1 , st2 ;
err = smbc_init ( get_auth_data_fn , 10 ) ; /* Initialize things */
if ( err < 0 ) {
fprintf ( stderr , " Initializing the smbclient library ...: %s \n " , strerror ( errno ) ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
}
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
if ( argc > 1 ) {
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
if ( ( dh1 = smbc_opendir ( argv [ 1 ] ) ) < 1 ) {
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stderr , " Could not open directory: %s: %s \n " ,
argv [ 1 ] , strerror ( errno ) ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
exit ( 1 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
}
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stdout , " Directory handle: %u \n " , dh1 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
/* Now, list those directories, but in funny ways ... */
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
dirp = ( char * ) dirbuf ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
if ( ( dirc = smbc_getdents ( dh1 , ( struct smbc_dirent * ) dirp ,
sizeof ( dirbuf ) ) ) < 0 ) {
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stderr , " Problems getting directory entries: %s \n " ,
strerror ( errno ) ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
exit ( 1 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
}
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
/* Now, process the list of names ... */
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stdout , " Directory listing, size = %u \n " , dirc ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
while ( dirc > 0 ) {
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
dsize = ( ( struct smbc_dirent * ) dirp ) - > dirlen ;
fprintf ( stdout , " Dir Ent, Type: %u, Name: %s, Comment: %s \n " ,
( ( struct smbc_dirent * ) dirp ) - > smbc_type ,
( ( struct smbc_dirent * ) dirp ) - > name ,
( ( struct smbc_dirent * ) dirp ) - > comment ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
dirp + = dsize ;
dirc - = dsize ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
}
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
dirp = ( char * ) dirbuf ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
exit ( 1 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
}
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
/* For now, open a file on a server that is hard coded ... later will
* read from the command line . . .
*/
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fd = smbc_open ( file , O_RDWR | O_CREAT | O_TRUNC , 0666 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
if ( fd < 0 ) {
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stderr , " Creating file: %s: %s \n " , file , strerror ( errno ) ) ;
exit ( 0 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
}
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stdout , " Opened or created file: %s \n " , file ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
/* Now, write some date to the file ... */
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
memset ( buff , ' \0 ' , sizeof ( buff ) ) ;
snprintf ( buff , sizeof ( buff ) , " %s " , " Some test data for the moment ... " ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
err = smbc_write ( fd , buff , sizeof ( buff ) ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
if ( err < 0 ) {
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stderr , " writing file: %s: %s \n " , file , strerror ( errno ) ) ;
exit ( 0 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
}
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stdout , " Wrote %lu bytes to file: %s \n " ,
( unsigned long ) sizeof ( buff ) , buff ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
/* Now, seek the file back to offset 0 */
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
err = smbc_lseek ( fd , SEEK_SET , 0 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
if ( err < 0 ) {
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stderr , " Seeking file: %s: %s \n " , file , strerror ( errno ) ) ;
exit ( 0 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
}
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stdout , " Completed lseek on file: %s \n " , file ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
/* Now, read the file contents back ... */
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
err = smbc_read ( fd , buff , sizeof ( buff ) ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
if ( err < 0 ) {
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stderr , " Reading file: %s: %s \n " , file , strerror ( errno ) ) ;
exit ( 0 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
}
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stdout , " Read file: %s \n " , buff ) ; /* Should check the contents */
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stdout , " Now fstat'ing file: %s \n " , file ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
err = smbc_fstat ( fd , & st1 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
if ( err < 0 ) {
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stderr , " Fstat'ing file: %s: %s \n " , file , strerror ( errno ) ) ;
exit ( 0 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
}
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
/* Now, close the file ... */
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
err = smbc_close ( fd ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
if ( err < 0 ) {
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stderr , " Closing file: %s: %s \n " , file , strerror ( errno ) ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
}
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
/* Now, rename the file ... */
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
err = smbc_rename ( file , file2 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
if ( err < 0 ) {
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stderr , " Renaming file: %s to %s: %s \n " , file , file2 , strerror ( errno ) ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
}
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stdout , " Renamed file %s to %s \n " , file , file2 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
/* Now, create a file and delete it ... */
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stdout , " Now, creating file: %s so we can delete it. \n " , file ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fd = smbc_open ( file , O_RDWR | O_CREAT , 0666 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
if ( fd < 0 ) {
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stderr , " Creating file: %s: %s \n " , file , strerror ( errno ) ) ;
exit ( 0 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
}
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stdout , " Opened or created file: %s \n " , file ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
err = smbc_close ( fd ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
if ( err < 0 ) {
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stderr , " Closing file: %s: %s \n " , file , strerror ( errno ) ) ;
exit ( 0 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
}
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
/* Now, delete the file ... */
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stdout , " File %s created, now deleting ... \n " , file ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
err = smbc_unlink ( file ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
if ( err < 0 ) {
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stderr , " Deleting file: %s: %s \n " , file , strerror ( errno ) ) ;
exit ( 0 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
}
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
/* Now, stat the file, file 2 ... */
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stdout , " Now stat'ing file: %s \n " , file ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
err = smbc_stat ( file2 , & st2 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
if ( err < 0 ) {
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stderr , " Stat'ing file: %s: %s \n " , file , strerror ( errno ) ) ;
exit ( 0 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
}
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stdout , " Stat'ed file: %s. Size = %d, mode = %04X \n " , file2 ,
( int ) st2 . st_size , ( unsigned int ) st2 . st_mode ) ;
fprintf ( stdout , " time: %s \n " , ctime ( & st2 . st_atime ) ) ;
fprintf ( stdout , " Earlier stat: %s, Size = %d, mode = %04X \n " , file ,
( int ) st1 . st_size , ( unsigned int ) st1 . st_mode ) ;
fprintf ( stdout , " time: %s \n " , ctime ( & st1 . st_atime ) ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
/* Now, make a directory ... */
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stdout , " Making directory smb://samba/public/make-dir \n " ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
if ( smbc_mkdir ( " smb://samba/public/make-dir " , 0666 ) < 0 ) {
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stderr , " Error making directory: smb://samba/public/make-dir: %s \n " ,
strerror ( errno ) ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
if ( errno = = EEXIST ) { /* Try to delete the directory */
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stdout , " Trying to delete directory: smb://samba/public/make-dir \n " ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
if ( smbc_rmdir ( " smb://samba/public/make-dir " ) < 0 ) { /* Error */
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stderr , " Error removing directory: smb://samba/public/make-dir: %s \n " , strerror ( errno ) ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
exit ( 0 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
}
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stdout , " Making directory: smb://samba/public/make-dir \n " ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
if ( smbc_mkdir ( " smb://samba/public/make-dir " , 666 ) < 0 ) {
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stderr , " Error making directory: smb://samba/public/make-dir: %s \n " ,
strerror ( errno ) ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stderr , " I give up! \n " ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
exit ( 1 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
}
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
}
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
exit ( 0 ) ;
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
}
2001-05-17 06:29:00 +04:00
2022-08-17 11:51:06 +03:00
fprintf ( stdout , " Made dir: make-dir \n " ) ;
return 0 ;
2001-05-17 06:29:00 +04:00
}