2 * Alternate testbrowse utility provided by Mikhail Kshevetskiy.
3 * This version tests use of multiple contexts.
9 #include <libsmbclient.h>
12 const char *workgroup
= "NT";
13 const char *username
= "guest";
14 const char *password
= "";
16 typedef struct smbitem smbitem
;
17 typedef int(*qsort_cmp
)(const void *, const void *);
25 static void smbc_auth_fn(
28 char *wrkgrp
, int wrkgrplen
,
29 char *user
, int userlen
,
30 char *passwd
, int passwdlen
){
37 strncpy(wrkgrp
, workgroup
, wrkgrplen
- 1); wrkgrp
[wrkgrplen
- 1] = 0;
38 strncpy(user
, username
, userlen
- 1); user
[userlen
- 1] = 0;
39 strncpy(passwd
, password
, passwdlen
- 1); passwd
[passwdlen
- 1] = 0;
42 static SMBCCTX
* create_smbctx(void){
45 if ((ctx
= smbc_new_context()) == NULL
) return NULL
;
47 smbc_setDebug(ctx
, debuglevel
);
48 smbc_setFunctionAuthData(ctx
, smbc_auth_fn
);
50 if (smbc_init_context(ctx
) == NULL
){
51 smbc_free_context(ctx
, 1);
58 static void delete_smbctx(SMBCCTX
* ctx
){
59 smbc_getFunctionPurgeCachedServers(ctx
)(ctx
);
60 smbc_free_context(ctx
, 1);
63 static smbitem
* get_smbitem_list(SMBCCTX
*ctx
, char *smb_path
){
65 struct smbc_dirent
*dirent
;
66 smbitem
*list
= NULL
, *item
;
68 if ((fd
= smbc_getFunctionOpendir(ctx
)(ctx
, smb_path
)) == NULL
)
70 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;
76 slen
= strlen(dirent
->name
)+1;
77 if ((item
= malloc(sizeof(smbitem
) + slen
)) == NULL
)
81 item
->type
= dirent
->smbc_type
;
82 memcpy(item
->name
, dirent
->name
, slen
);
85 smbc_getFunctionClose(ctx
)(ctx
, fd
);
86 return /* smbitem_list_sort */ (list
);
90 static void print_smb_path(const char *group
, const char *path
){
91 if ((strlen(group
) == 0) && (strlen(path
) == 0)) printf("/\n");
92 else if (strlen(path
) == 0) printf("/%s\n", group
);
94 if (strlen(group
) == 0) group
= "(unknown_group)";
95 printf("/%s/%s\n", group
, path
);
99 static void recurse(SMBCCTX
*ctx
, const char *smb_group
, char *smb_path
, int maxlen
){
101 smbitem
*list
, *item
;
104 len
= strlen(smb_path
);
106 list
= get_smbitem_list(ctx
, smb_path
);
111 if (list
->type
== SMBC_WORKGROUP
){
112 print_smb_path(list
->name
, "");
113 smb_group
= list
->name
;
115 else print_smb_path(smb_group
, list
->name
);
117 if (maxlen
< 7 + strlen(list
->name
)) break;
118 strncpy(smb_path
+ 6, list
->name
, maxlen
- 6);
119 smb_path
[maxlen
-1] = '\0';
120 if ((ctx1
= create_smbctx()) != NULL
){
121 recurse(ctx1
, smb_group
, smb_path
, maxlen
);
124 recurse(ctx
, smb_group
, smb_path
, maxlen
);
125 smbc_getFunctionPurgeCachedServers(ctx
)(ctx
);
128 case SMBC_FILE_SHARE
:
131 if (maxlen
< len
+ strlen(list
->name
) + 2) break;
134 strncpy(smb_path
+ len
+ 1, list
->name
, maxlen
- len
- 1);
135 smb_path
[maxlen
-1] = '\0';
136 print_smb_path(smb_group
, smb_path
+ 6);
137 if (list
->type
!= SMBC_FILE
){
138 recurse(ctx
, smb_group
, smb_path
, maxlen
);
139 if (list
->type
== SMBC_FILE_SHARE
)
140 smbc_getFunctionPurgeCachedServers(ctx
)(ctx
);
148 smb_path
[len
] = '\0';
151 int main(int argc
, char *argv
[]){
154 char smb_path
[32768] = "smb://";
156 if ((ctx
= create_smbctx()) == NULL
){
157 perror("Cant create samba context.");
161 if (argc
== 1) recurse(ctx
, "", smb_path
, sizeof(smb_path
));
162 else for(i
= 1; i
< argc
; i
++){
163 strncpy(smb_path
+ 6, argv
[i
], sizeof(smb_path
) - 7);
164 smb_path
[sizeof(smb_path
) - 1] = '\0';
165 recurse(ctx
, "", smb_path
, sizeof(smb_path
));