2 * Alternate testbrowse utility provided by Mikhail Kshevetskiy.
3 * This version tests use of multiple contexts.
10 #include <libsmbclient.h>
13 char *workgroup = "NT";
14 char *username = "guest";
17 typedef struct smbitem smbitem;
18 typedef int(*qsort_cmp)(const void *, const void *);
26 int smbitem_cmp(smbitem *elem1, smbitem *elem2){
27 return strcmp(elem1->name, elem2->name);
30 int smbitem_list_count(smbitem *list){
40 void smbitem_list_delete(smbitem *list){
50 smbitem* smbitem_list_sort(smbitem *list){
51 smbitem *item, **array;
54 if ((count = smbitem_list_count(list)) == 0) return NULL;
55 if ((array = malloc(count * sizeof(smbitem*))) == NULL){
56 smbitem_list_delete(list);
60 for(i = 0; i < count; i++){
64 qsort(array, count, sizeof(smbitem*), (qsort_cmp)smbitem_cmp);
66 for(i = 0; i < count - 1; i++) array[i]->next = array[i + 1];
67 array[count - 1]->next = NULL;
77 char *wrkgrp, int wrkgrplen,
78 char *user, int userlen,
79 char *passwd, int passwdlen){
86 strncpy(wrkgrp, workgroup, wrkgrplen - 1); wrkgrp[wrkgrplen - 1] = 0;
87 strncpy(user, username, userlen - 1); user[userlen - 1] = 0;
88 strncpy(passwd, password, passwdlen - 1); passwd[passwdlen - 1] = 0;
91 SMBCCTX* create_smbctx(){
94 if ((ctx = smbc_new_context()) == NULL) return NULL;
96 smbc_setDebug(ctx, debuglevel);
97 smbc_setFunctionAuthData(ctx, smbc_auth_fn);
99 if (smbc_init_context(ctx) == NULL){
100 smbc_free_context(ctx, 1);
107 void delete_smbctx(SMBCCTX* ctx){
108 smbc_getFunctionPurgeCachedServers(ctx)(ctx);
109 smbc_free_context(ctx, 1);
112 smbitem* get_smbitem_list(SMBCCTX *ctx, char *smb_path){
114 struct smbc_dirent *dirent;
115 smbitem *list = NULL, *item;
117 if ((fd = smbc_getFunctionOpendir(ctx)(ctx, smb_path)) == NULL)
119 while((dirent = smbc_getFunctionReaddir(ctx)(ctx, fd)) != NULL){
120 if (strcmp(dirent->name, "") == 0) continue;
121 if (strcmp(dirent->name, ".") == 0) continue;
122 if (strcmp(dirent->name, "..") == 0) continue;
124 if ((item = malloc(sizeof(smbitem) + strlen(dirent->name))) == NULL)
128 item->type = dirent->smbc_type;
129 strcpy(item->name, dirent->name);
132 smbc_getFunctionClose(ctx)(ctx, fd);
133 return /* smbitem_list_sort */ (list);
137 void print_smb_path(char *group, char *path){
138 if ((strlen(group) == 0) && (strlen(path) == 0)) printf("/\n");
139 else if (strlen(path) == 0) printf("/%s\n", group);
141 if (strlen(group) == 0) group = "(unknown_group)";
142 printf("/%s/%s\n", group, path);
146 void recurse(SMBCCTX *ctx, char *smb_group, char *smb_path, int maxlen){
148 smbitem *list, *item;
151 len = strlen(smb_path);
153 list = get_smbitem_list(ctx, smb_path);
158 if (list->type == SMBC_WORKGROUP){
159 print_smb_path(list->name, "");
160 smb_group = list->name;
162 else print_smb_path(smb_group, list->name);
164 if (maxlen < 7 + strlen(list->name)) break;
165 strcpy(smb_path + 6, list->name);
166 if ((ctx1 = create_smbctx()) != NULL){
167 recurse(ctx1, smb_group, smb_path, maxlen);
170 recurse(ctx, smb_group, smb_path, maxlen);
171 smbc_getFunctionPurgeCachedServers(ctx)(ctx);
174 case SMBC_FILE_SHARE:
177 if (maxlen < len + strlen(list->name) + 2) break;
180 strcpy(smb_path + len + 1, list->name);
181 print_smb_path(smb_group, smb_path + 6);
182 if (list->type != SMBC_FILE){
183 recurse(ctx, smb_group, smb_path, maxlen);
184 if (list->type == SMBC_FILE_SHARE)
185 smbc_getFunctionPurgeCachedServers(ctx)(ctx);
193 smb_path[len] = '\0';
196 int main(int argc, char *argv[]){
199 char smb_path[32768] = "smb://";
201 if ((ctx = create_smbctx()) == NULL){
202 perror("Cant create samba context.");
206 if (argc == 1) recurse(ctx, "", smb_path, sizeof(smb_path));
207 else for(i = 1; i < argc; i++){
208 strncpy(smb_path + 6, argv[i], sizeof(smb_path) - 7);
209 smb_path[sizeof(smb_path) - 1] = '\0';
210 recurse(ctx, "", smb_path, sizeof(smb_path));