r23785: use the GPLv3 boilerplate as recommended by the FSF and the license text
[samba.git] / examples / libsmbclient / smbwrapper / smbw.c
index d2f1c18695ddca2973df1a304722f6abd6292e86..b88290ff6d8907734c44eaf9f32fd784578d0383 100644 (file)
@@ -7,7 +7,7 @@
    
    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 2 of the License, or
+   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,
@@ -16,8 +16,7 @@
    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, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include <stdio.h>
 #include <stdarg.h>
 #include <assert.h>
 #include "smbw.h"
+#include "bsd-strlfunc.h"
+
+typedef enum StartupType
+{
+        StartupType_Fake,
+        StartupType_Real
+} StartupType;
 
 int smbw_fd_map[__FD_SETSIZE];
 int smbw_ref_count[__FD_SETSIZE];
@@ -44,6 +50,9 @@ static SMBCCTX *smbw_ctx;
 extern int smbw_debug;
 
 
+/*****************************************************
+smbw_ref -- manipulate reference counts
+******************************************************/
 int smbw_ref(int client_fd, Ref_Count_Type type, ...)
 {
         va_list ap;
@@ -100,9 +109,9 @@ static void get_envvar_auth_data(const char *srv,
        p = getenv("PASSWORD");
        if (p == NULL) p = "";
 
-        strncpy(wg, w, wglen);
-        strncpy(un, u, unlen);
-        strncpy(pw, p, pwlen);
+        smbw_strlcpy(wg, w, wglen);
+        smbw_strlcpy(un, u, unlen);
+        smbw_strlcpy(pw, p, pwlen);
 }
 
 static smbc_get_auth_data_fn get_auth_data_fn = get_envvar_auth_data;
@@ -130,7 +139,7 @@ static void do_shutdown(void)
 /***************************************************** 
 initialise structures
 *******************************************************/
-static void do_init(int is_real_startup)
+static void do_init(StartupType startupType)
 {
         int i;
         char *p;
@@ -147,7 +156,7 @@ static void do_init(int is_real_startup)
 
         /* See if we've been told to start in a particular directory */
         if ((p=getenv("SMBW_DIR")) != NULL) {
-                strncpy(smbw_cwd, p, PATH_MAX);
+                smbw_strlcpy(smbw_cwd, p, PATH_MAX);
 
                 /* we don't want the old directory to be busy */
                 (* smbw_libc.chdir)("/");
@@ -161,6 +170,7 @@ static void do_init(int is_real_startup)
        }
 
         if ((smbw_ctx = smbc_new_context()) == NULL) {
+                fprintf(stderr, "Could not create a context.\n");
                 exit(1);
         }
         
@@ -169,16 +179,16 @@ static void do_init(int is_real_startup)
         smbw_ctx->options.browse_max_lmb_count = 0;
         smbw_ctx->options.urlencode_readdir_entries = 1;
         smbw_ctx->options.one_share_per_server = 1;
-//        smbw_cache_functions(smbw_ctx);
         
         if (smbc_init_context(smbw_ctx) == NULL) {
+                fprintf(stderr, "Could not initialize context.\n");
                 exit(1);
         }
                 
         smbc_set_context(smbw_ctx);
 
         /* if not real startup, exit handler has already been established */
-        if (is_real_startup) {
+        if (startupType == StartupType_Real) {
             atexit(do_shutdown);
         }
 }
@@ -188,7 +198,7 @@ initialise structures, real start up vs a fork()
 *******************************************************/
 void smbw_init(void)
 {
-    do_init(1);
+    do_init(StartupType_Real);
 }
 
 
@@ -407,6 +417,10 @@ ssize_t smbw_pread(int smbw_fd, void *buf, size_t count, SMBW_OFF_T ofs)
         int saved_errno;
         SMBW_OFF_T old_ofs;
         
+        if (count == 0) {
+                return 0;
+        }
+
         client_fd = smbw_fd_map[smbw_fd];
 
         if ((old_ofs = smbc_lseek(client_fd, 0, SEEK_CUR)) < 0 ||
@@ -460,6 +474,10 @@ ssize_t smbw_pwrite(int smbw_fd, void *buf, size_t count, SMBW_OFF_T ofs)
        ssize_t ret;
         SMBW_OFF_T old_ofs;
         
+        if (count == 0) {
+                return 0;
+        }
+
         client_fd = smbw_fd_map[smbw_fd];
 
         if ((old_ofs = smbc_lseek(client_fd, 0, SEEK_CUR)) < 0 ||
@@ -731,7 +749,7 @@ int smbw_fork(void)
         }
 
         /* Re-initialize this library for the child */
-        do_init(0);
+        do_init(StartupType_Fake);
 
        /* and continue in the child */
        return 0;