s3-param: Rename loadparm_s3_context -> loadparm_s3_helpers
[kai/samba.git] / source3 / client / tree.c
index 5c2ced5e02b57fc6f4468befabf34ea819e7bed4..e0b8c91949ad6a1af14f2d5b8c684672fccb4992 100644 (file)
@@ -1,6 +1,5 @@
 /* 
 /* 
-   Unix SMB/Netbios implementation.
-   Version 2.0
+   Unix SMB/CIFS implementation.
    SMB client GTK+ tree-based application
    Copyright (C) Andrew Tridgell 1998
    Copyright (C) Richard Sharpe 2001
    SMB client GTK+ tree-based application
    Copyright (C) Andrew Tridgell 1998
    Copyright (C) Richard Sharpe 2001
@@ -8,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
    
    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,
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -17,8 +16,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    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/>.
 */
 
 /* example-gtk+ application, ripped off from the gtk+ tree.c sample */
 */
 
 /* example-gtk+ application, ripped off from the gtk+ tree.c sample */
@@ -37,14 +35,14 @@ struct tree_data {
 
 };
 
 
 };
 
-void error_message(gchar *message) {
+static void tree_error_message(gchar *message) {
 
   GtkWidget *dialog, *label, *okay_button;
      
   /* Create the widgets */
      
   dialog = gtk_dialog_new();
 
   GtkWidget *dialog, *label, *okay_button;
      
   /* Create the widgets */
      
   dialog = gtk_dialog_new();
-  gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
+  gtk_window_set_modal(GTK_WINDOW(dialog), True);
   label = gtk_label_new (message);
   okay_button = gtk_button_new_with_label("Okay");
      
   label = gtk_label_new (message);
   okay_button = gtk_button_new_with_label("Okay");
      
@@ -70,9 +68,9 @@ void error_message(gchar *message) {
  * workgroup type and return a path from there
  */
 
  * workgroup type and return a path from there
  */
 
-static char path_string[1024];
+static char *path_string;
 
 
-char *get_path(GtkWidget *item)
+char *get_path(TALLOC_CTX *ctx, GtkWidget *item)
 {
   GtkWidget *p = item;
   struct tree_data *pd;
 {
   GtkWidget *p = item;
   struct tree_data *pd;
@@ -94,7 +92,7 @@ char *get_path(GtkWidget *item)
 
     /* Find the parent and extract the data etc ... */
 
 
     /* Find the parent and extract the data etc ... */
 
-    p = GTK_WIDGET(p->parent);    
+    p = GTK_WIDGET(p->parent);
     p = GTK_WIDGET(GTK_TREE(p)->tree_owner);
 
     pd = (struct tree_data *)gtk_object_get_user_data(GTK_OBJECT(p));
     p = GTK_WIDGET(GTK_TREE(p)->tree_owner);
 
     pd = (struct tree_data *)gtk_object_get_user_data(GTK_OBJECT(p));
@@ -106,23 +104,25 @@ char *get_path(GtkWidget *item)
 
   }
 
 
   }
 
-  /* 
+  /*
    * Got a list of comps now, should check that we did not hit a workgroup
    * when we got other things as well ... Later
    *
    * Now, build the path
    */
 
    * Got a list of comps now, should check that we did not hit a workgroup
    * when we got other things as well ... Later
    *
    * Now, build the path
    */
 
-  snprintf(path_string, sizeof(path_string), "smb:/");
+  TALLOC_FREE(path_string);
+  path_string = talloc_strdup(ctx, "smb:/");
 
 
-  for (j = i - 1; j >= 0; j--) {
-
-    strncat(path_string, "/", sizeof(path_string) - strlen(path_string));
-    strncat(path_string, comps[j], sizeof(path_string) - strlen(path_string));
+  if (path_string) {
+    for (j = i - 1; j >= 0; j--) {
+      path_string = talloc_asprintf_append(path_string, "/%s", comps[j]);
+    }
+  }
 
 
+  if (path_string) {
+    fprintf(stdout, "Path string = %s\n", path_string);
   }
   }
-  
-  fprintf(stdout, "Path string = %s\n", path_string);
 
   return path_string;
 
 
   return path_string;
 
@@ -130,7 +130,7 @@ char *get_path(GtkWidget *item)
 
 struct tree_data *make_tree_data(guint32 type, const char *name)
 {
 
 struct tree_data *make_tree_data(guint32 type, const char *name)
 {
-  struct tree_data *p = (struct tree_data *)malloc(sizeof(struct tree_data));
+  struct tree_data *p = SMB_MALLOC_P(struct tree_data);
 
   if (p) {
 
 
   if (p) {
 
@@ -152,7 +152,8 @@ static void cb_select_child (GtkWidget *root_tree, GtkWidget *child,
   char dirbuf[512];
   struct smbc_dirent *dirp;
   struct stat st1;
   char dirbuf[512];
   struct smbc_dirent *dirp;
   struct stat st1;
-  char path[1024], path1[1024];
+  char *path;
+  TALLOC_CTX *ctx = talloc_stackframe();
 
   g_print ("select_child called for root tree %p, subtree %p, child %p\n",
           root_tree, subtree, child);
 
   g_print ("select_child called for root tree %p, subtree %p, child %p\n",
           root_tree, subtree, child);
@@ -163,31 +164,29 @@ static void cb_select_child (GtkWidget *root_tree, GtkWidget *child,
 
   /* Now, get the private data for the subtree */
 
 
   /* Now, get the private data for the subtree */
 
-  strncpy(path, get_path(child), 1024);
+  path = get_path(ctx, child);
+  if (!path) {
+    gtk_main_quit();
+    TALLOC_FREE(ctx);
+    return;
+  }
 
   if ((dh = smbc_opendir(path)) < 0) { /* Handle error */
 
   if ((dh = smbc_opendir(path)) < 0) { /* Handle error */
-
     g_print("cb_select_child: Could not open dir %s, %s\n", path,
            strerror(errno));
     g_print("cb_select_child: Could not open dir %s, %s\n", path,
            strerror(errno));
-
     gtk_main_quit();
     gtk_main_quit();
-
+    TALLOC_FREE(ctx);
     return;
     return;
-
   }
 
   while ((err = smbc_getdents(dh, (struct smbc_dirent *)dirbuf,
                              sizeof(dirbuf))) != 0) {
   }
 
   while ((err = smbc_getdents(dh, (struct smbc_dirent *)dirbuf,
                              sizeof(dirbuf))) != 0) {
-
     if (err < 0) {
     if (err < 0) {
-
       g_print("cb_select_child: Could not read dir %s, %s\n", path,
              strerror(errno));
       g_print("cb_select_child: Could not read dir %s, %s\n", path,
              strerror(errno));
-
       gtk_main_quit();
       gtk_main_quit();
-
+      TALLOC_FREE(ctx);
       return;
       return;
-
     }
 
     dirp = (struct smbc_dirent *)dirbuf;
     }
 
     dirp = (struct smbc_dirent *)dirbuf;
@@ -242,27 +241,27 @@ static void cb_select_child (GtkWidget *root_tree, GtkWidget *child,
 
        if ((strcmp(dirp->name, ".") != 0) &&
            (strcmp(dirp->name, "..") != 0)) {
 
        if ((strcmp(dirp->name, ".") != 0) &&
            (strcmp(dirp->name, "..") != 0)) {
+          char *path1;
 
 
-         strncpy(path1, path, sizeof(path1));
-         strncat(path1, "/", sizeof(path) - strlen(path));
-         strncat(path1, dirp->name, sizeof(path) - strlen(path));
+         path1 = talloc_asprintf(ctx,
+                             "%s/%s",
+                            path,
+                            dirp->name);
+          if (!path1) {
+             gtk_main_quit();
+              TALLOC_FREE(ctx);
+             return;
+         }
 
          if (smbc_stat(path1, &st1) < 0) {
 
          if (smbc_stat(path1, &st1) < 0) {
-           
            if (errno != EBUSY) {
            if (errno != EBUSY) {
-             
-             g_print("cb_select_child: Could not stat file %s, %s\n", path1, 
+             g_print("cb_select_child: Could not stat file %s, %s\n", path1,
                      strerror(errno));
                      strerror(errno));
-           
              gtk_main_quit();
              gtk_main_quit();
-
+              TALLOC_FREE(ctx);
              return;
              return;
-
-           }
-           else {
-
+           } else {
              strncpy(col2, "Device or resource busy", sizeof(col2));
              strncpy(col2, "Device or resource busy", sizeof(col2));
-
            }
          }
          else {
            }
          }
          else {
@@ -278,7 +277,7 @@ static void cb_select_child (GtkWidget *root_tree, GtkWidget *child,
                     (st1.st_mode&S_IROTH?'r':'-'),
                     (st1.st_mode&S_IWOTH?'w':'-'),
                     (st1.st_mode&S_IXOTH?'x':'-'),
                     (st1.st_mode&S_IROTH?'r':'-'),
                     (st1.st_mode&S_IWOTH?'w':'-'),
                     (st1.st_mode&S_IXOTH?'x':'-'),
-                    st1.st_mode); 
+                    st1.st_mode);
            snprintf(col3, sizeof(col3), "%u", st1.st_size);
            snprintf(col4, sizeof(col4), "%s", ctime(&st1.st_mtime));
          }
            snprintf(col3, sizeof(col3), "%u", st1.st_size);
            snprintf(col4, sizeof(col4), "%s", ctime(&st1.st_mtime));
          }
@@ -297,9 +296,8 @@ static void cb_select_child (GtkWidget *root_tree, GtkWidget *child,
       err -= dirlen;
 
     }
       err -= dirlen;
 
     }
-
   }
   }
-
+  TALLOC_FREE(ctx);
 }
 
 /* Note that this is never called */
 }
 
 /* Note that this is never called */
@@ -345,7 +343,7 @@ static void cb_itemsignal( GtkWidget *item,
 
       slprintf(errmsg, sizeof(errmsg), "cb_itemsignal: Could not open dir %s, %s\n", get_path(item), strerror(errno));
 
 
       slprintf(errmsg, sizeof(errmsg), "cb_itemsignal: Could not open dir %s, %s\n", get_path(item), strerror(errno));
 
-      error_message(errmsg);
+      tree_error_message(errmsg);
 
       /*      gtk_main_quit();*/
 
 
       /*      gtk_main_quit();*/
 
@@ -364,7 +362,7 @@ static void cb_itemsignal( GtkWidget *item,
 
        slprintf(errmsg, sizeof(errmsg), "cb_itemsignal: Could not read dir smbc://, %s\n", strerror(errno));
 
 
        slprintf(errmsg, sizeof(errmsg), "cb_itemsignal: Could not read dir smbc://, %s\n", strerror(errno));
 
-       error_message(errmsg);
+       tree_error_message(errmsg);
 
        /*      gtk_main_quit();*/
 
 
        /*      gtk_main_quit();*/
 
@@ -616,6 +614,7 @@ int main( int   argc,
   gint i;
   char dirbuf[512];
   struct smbc_dirent *dirp;
   gint i;
   char dirbuf[512];
   struct smbc_dirent *dirp;
+  TALLOC_CTX *frame = talloc_stackframe();
 
   gtk_init (&argc, &argv);
 
 
   gtk_init (&argc, &argv);
 
@@ -807,6 +806,7 @@ int main( int   argc,
 
   /* Show the window and loop endlessly */
   gtk_main();
 
   /* Show the window and loop endlessly */
   gtk_main();
+  TALLOC_FREE(frame);
   return 0;
 }
 /* example-end */
   return 0;
 }
 /* example-end */