Fix a boatload of warnings in the examples.
[rusty/samba.git] / examples / libsmbclient / testbrowse2.c
1 /*
2  * Alternate testbrowse utility provided by Mikhail Kshevetskiy.
3  * This version tests use of multiple contexts.
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <malloc.h>
9 #include <string.h>
10 #include <libsmbclient.h>
11
12 int     debuglevel      = 0;
13 const char      *workgroup      = "NT";
14 const char      *username       = "guest";
15 const char      *password       = "";
16
17 typedef struct smbitem smbitem;
18 typedef int(*qsort_cmp)(const void *, const void *);
19
20 struct smbitem{
21     smbitem     *next;
22     int         type;
23     char        name[1];
24 };
25
26 static void smbc_auth_fn(
27                 const char      *server,
28                 const char      *share,
29                 char            *wrkgrp, int wrkgrplen,
30                 char            *user,   int userlen,
31                 char            *passwd, int passwdlen){
32                 
33     (void) server;
34     (void) share;
35     (void) wrkgrp;
36     (void) wrkgrplen;
37
38     strncpy(wrkgrp, workgroup, wrkgrplen - 1); wrkgrp[wrkgrplen - 1] = 0;
39     strncpy(user, username, userlen - 1); user[userlen - 1] = 0;
40     strncpy(passwd, password, passwdlen - 1); passwd[passwdlen - 1] = 0;
41 }
42
43 static SMBCCTX* create_smbctx(void){
44     SMBCCTX     *ctx;
45
46     if ((ctx = smbc_new_context()) == NULL) return NULL;
47
48     smbc_setDebug(ctx, debuglevel);
49     smbc_setFunctionAuthData(ctx, smbc_auth_fn);
50
51     if (smbc_init_context(ctx) == NULL){
52         smbc_free_context(ctx, 1);
53         return NULL;
54     }
55
56     return ctx;
57 }
58
59 static void delete_smbctx(SMBCCTX* ctx){
60     smbc_getFunctionPurgeCachedServers(ctx)(ctx);
61     smbc_free_context(ctx, 1);
62 }
63
64 static smbitem* get_smbitem_list(SMBCCTX *ctx, char *smb_path){
65     SMBCFILE            *fd;
66     struct smbc_dirent  *dirent;
67     smbitem             *list = NULL, *item;
68
69     if ((fd = smbc_getFunctionOpendir(ctx)(ctx, smb_path)) == NULL)
70         return NULL;
71     while((dirent = smbc_getFunctionReaddir(ctx)(ctx, fd)) != NULL){
72         if (strcmp(dirent->name, "") == 0) continue;
73         if (strcmp(dirent->name, ".") == 0) continue;
74         if (strcmp(dirent->name, "..") == 0) continue;
75         
76         if ((item = malloc(sizeof(smbitem) + strlen(dirent->name))) == NULL)
77             continue;
78         
79         item->next = list;
80         item->type = dirent->smbc_type;
81         strcpy(item->name, dirent->name);
82         list = item;
83     }
84     smbc_getFunctionClose(ctx)(ctx, fd);
85     return /* smbitem_list_sort */ (list);    
86         
87 }
88
89 static void print_smb_path(const char *group, const char *path){
90     if ((strlen(group) == 0) && (strlen(path) == 0)) printf("/\n");
91     else if (strlen(path) == 0) printf("/%s\n", group);
92     else{
93         if (strlen(group) == 0) group = "(unknown_group)";
94         printf("/%s/%s\n", group, path);
95     }
96 }
97
98 static void recurse(SMBCCTX *ctx, const char *smb_group, char *smb_path, int maxlen){
99     int         len;
100     smbitem     *list, *item;
101     SMBCCTX     *ctx1;
102     
103     len = strlen(smb_path);
104     
105     list = get_smbitem_list(ctx, smb_path);
106     while(list != NULL){
107         switch(list->type){
108             case SMBC_WORKGROUP:
109             case SMBC_SERVER:
110                 if (list->type == SMBC_WORKGROUP){
111                     print_smb_path(list->name, "");
112                     smb_group = list->name;
113                 }
114                 else print_smb_path(smb_group, list->name);
115                 
116                 if (maxlen < 7 + strlen(list->name)) break;
117                 strcpy(smb_path + 6, list->name);
118                 if ((ctx1 = create_smbctx()) != NULL){
119                     recurse(ctx1, smb_group, smb_path, maxlen);
120                     delete_smbctx(ctx1);
121                 }else{
122                     recurse(ctx, smb_group, smb_path, maxlen);
123                     smbc_getFunctionPurgeCachedServers(ctx)(ctx);
124                 }
125                 break;
126             case SMBC_FILE_SHARE:
127             case SMBC_DIR:
128             case SMBC_FILE:
129                 if (maxlen < len + strlen(list->name) + 2) break;
130                 
131                 smb_path[len] = '/';
132                 strcpy(smb_path + len + 1, list->name);
133                 print_smb_path(smb_group, smb_path + 6);
134                 if (list->type != SMBC_FILE){
135                     recurse(ctx, smb_group, smb_path, maxlen);
136                     if (list->type == SMBC_FILE_SHARE)
137                         smbc_getFunctionPurgeCachedServers(ctx)(ctx);
138                 }
139                 break;
140         }
141         item = list;
142         list = list->next;
143         free(item);
144     }
145     smb_path[len] = '\0';
146 }
147
148 int main(int argc, char *argv[]){
149     int         i;
150     SMBCCTX     *ctx;
151     char        smb_path[32768] = "smb://";
152
153     if ((ctx = create_smbctx()) == NULL){
154         perror("Cant create samba context.");
155         return 1;
156     }
157
158     if (argc == 1) recurse(ctx, "", smb_path, sizeof(smb_path));
159     else for(i = 1; i < argc; i++){
160         strncpy(smb_path + 6, argv[i], sizeof(smb_path) - 7);
161         smb_path[sizeof(smb_path) - 1] = '\0';
162         recurse(ctx, "", smb_path, sizeof(smb_path));
163     }
164     
165     delete_smbctx(ctx);
166     return 0;   
167 }