s3: signals are processed twice in child.
[Samba.git] / examples / libsmbclient / testbrowse2.c
blob0ac1d72bb43221672e048aeb2b4db8e8fbc4227c
1 /*
2 * Alternate testbrowse utility provided by Mikhail Kshevetskiy.
3 * This version tests use of multiple contexts.
4 */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <malloc.h>
9 #include <string.h>
10 #include <libsmbclient.h>
12 int debuglevel = 0;
13 char *workgroup = "NT";
14 char *username = "guest";
15 char *password = "";
17 typedef struct smbitem smbitem;
18 typedef int(*qsort_cmp)(const void *, const void *);
20 struct smbitem{
21 smbitem *next;
22 int type;
23 char name[1];
26 int smbitem_cmp(smbitem *elem1, smbitem *elem2){
27 return strcmp(elem1->name, elem2->name);
30 int smbitem_list_count(smbitem *list){
31 int count = 0;
33 while(list != NULL){
34 list = list->next;
35 count++;
37 return count;
40 void smbitem_list_delete(smbitem *list){
41 smbitem *elem;
43 while(list != NULL){
44 elem = list;
45 list = list->next;
46 free(elem);
50 smbitem* smbitem_list_sort(smbitem *list){
51 smbitem *item, **array;
52 int count, i;
54 if ((count = smbitem_list_count(list)) == 0) return NULL;
55 if ((array = malloc(count * sizeof(smbitem*))) == NULL){
56 smbitem_list_delete(list);
57 return NULL;
60 for(i = 0; i < count; i++){
61 array[i] = list;
62 list = list->next;
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;
69 list = array[0];
70 free(array);
71 return list;
74 void smbc_auth_fn(
75 const char *server,
76 const char *share,
77 char *wrkgrp, int wrkgrplen,
78 char *user, int userlen,
79 char *passwd, int passwdlen){
81 (void) server;
82 (void) share;
83 (void) wrkgrp;
84 (void) wrkgrplen;
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(){
92 SMBCCTX *ctx;
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);
101 return NULL;
104 return ctx;
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){
113 SMBCFILE *fd;
114 struct smbc_dirent *dirent;
115 smbitem *list = NULL, *item;
117 if ((fd = smbc_getFunctionOpendir(ctx)(ctx, smb_path)) == NULL)
118 return 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)
125 continue;
127 item->next = list;
128 item->type = dirent->smbc_type;
129 strcpy(item->name, dirent->name);
130 list = item;
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);
140 else{
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){
147 int len;
148 smbitem *list, *item;
149 SMBCCTX *ctx1;
151 len = strlen(smb_path);
153 list = get_smbitem_list(ctx, smb_path);
154 while(list != NULL){
155 switch(list->type){
156 case SMBC_WORKGROUP:
157 case SMBC_SERVER:
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);
168 delete_smbctx(ctx1);
169 }else{
170 recurse(ctx, smb_group, smb_path, maxlen);
171 smbc_getFunctionPurgeCachedServers(ctx)(ctx);
173 break;
174 case SMBC_FILE_SHARE:
175 case SMBC_DIR:
176 case SMBC_FILE:
177 if (maxlen < len + strlen(list->name) + 2) break;
179 smb_path[len] = '/';
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);
187 break;
189 item = list;
190 list = list->next;
191 free(item);
193 smb_path[len] = '\0';
196 int main(int argc, char *argv[]){
197 int i;
198 SMBCCTX *ctx;
199 char smb_path[32768] = "smb://";
201 if ((ctx = create_smbctx()) == NULL){
202 perror("Cant create samba context.");
203 return 1;
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));
213 delete_smbctx(ctx);
214 return 0;