r12080: r10673@cabra: derrell | 2005-12-05 13:22:34 -0500
[kamenim/samba-autobuild/.git] / examples / libsmbclient / testbrowse.c
1 #include <sys/types.h>
2 #include <unistd.h>
3 #include <dirent.h>
4 #include <errno.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <popt.h>
8 #include <stdlib.h>
9 #include <libsmbclient.h>
10 #include "get_auth_data_fn.h"
11
12 static void
13 no_auth_data_fn(const char * pServer,
14                 const char * pShare,
15                 char * pWorkgroup,
16                 int maxLenWorkgroup,
17                 char * pUsername,
18                 int maxLenUsername,
19                 char * pPassword,
20                 int maxLenPassword);
21
22 static void browse(char * path,
23                    int scan,
24                    int indent);
25
26
27
28 int
29 main(int argc, char * argv[])
30 {
31     int                         debug = 0;
32     int                         scan = 0;
33     int                         iterations = -1;
34     int                         again;
35     int                         opt;
36     char *                      p;
37     char *                      q;
38     char                        buf[1024];
39     poptContext                 pc;
40     struct poptOption           long_options[] =
41         {
42             POPT_AUTOHELP
43             {
44                 "debug", 'd', POPT_ARG_INT, &debug,
45                 0, "Set debug level", "integer"
46             },
47             {
48                 "scan", 's', POPT_ARG_NONE, &scan,
49                 0, "Scan for servers and shares", "integer"
50             },
51             {
52                 "iterations", 'i', POPT_ARG_INT, &iterations,
53                 0, "Iterations", "integer"
54             },
55             {
56                 NULL
57             }
58         };
59     
60     setbuf(stdout, NULL);
61
62     pc = poptGetContext("opendir", argc, (const char **)argv, long_options, 0);
63     
64     poptSetOtherOptionHelp(pc, "");
65     
66     while ((opt = poptGetNextOpt(pc)) != -1) {
67         printf("Got option %d = %c\n", opt, opt);
68         switch (opt) {
69         }
70     }
71
72     if (scan)
73     {
74         if (smbc_init(no_auth_data_fn, debug) != 0)
75         {
76             printf("Could not initialize smbc_ library\n");
77             return 1;
78         }
79
80         for (;
81              iterations == -1 || iterations > 0;
82              iterations = (iterations == -1 ? iterations : --iterations))
83         {
84             snprintf(buf, sizeof(buf), "smb://");
85             browse(buf, scan, 0);
86         }
87     }
88     else
89     {
90         if (smbc_init(get_auth_data_fn, debug) != 0)
91         {
92             printf("Could not initialize smbc_ library\n");
93             return 1;
94         }
95     
96         for (;
97              iterations == -1 || iterations > 0;
98              iterations = (iterations == -1 ? iterations : --iterations))
99         {
100             fputs("url: ", stdout);
101             p = fgets(buf, sizeof(buf), stdin);
102             if (! p)
103             {
104                 break;
105             }
106
107             if ((p = strchr(buf, '\n')) != NULL)
108             {
109                 *p = '\0';
110             }
111             
112             browse(buf, scan, 0);
113         }
114     }
115
116     exit(0);
117 }
118
119
120 static void
121 no_auth_data_fn(const char * pServer,
122                 const char * pShare,
123                 char * pWorkgroup,
124                 int maxLenWorkgroup,
125                 char * pUsername,
126                 int maxLenUsername,
127                 char * pPassword,
128                 int maxLenPassword)
129 {
130     return;
131 }
132
133 static void browse(char * path, int scan, int indent)
134 {
135     char *                      p;
136     char                        buf[1024];
137     int                         dir;
138     struct stat                 stat;
139     struct smbc_dirent *        dirent;
140
141     if (! scan)
142     {
143         printf("Opening (%s)...\n", path);
144     }
145         
146     if ((dir = smbc_opendir(path)) < 0)
147     {
148         printf("Could not open directory [%s] (%d:%s)\n",
149                path, errno, strerror(errno));
150         return;
151     }
152
153     while ((dirent = smbc_readdir(dir)) != NULL)
154     {
155         printf("%*.*s%-30s", indent, indent, "", dirent->name);
156
157         switch(dirent->smbc_type)
158         {
159         case SMBC_WORKGROUP:
160             printf("WORKGROUP");
161             break;
162             
163         case SMBC_SERVER:
164             printf("SERVER");
165             break;
166             
167         case SMBC_FILE_SHARE:
168             printf("FILE_SHARE");
169             break;
170             
171         case SMBC_PRINTER_SHARE:
172             printf("PRINTER_SHARE");
173             break;
174             
175         case SMBC_COMMS_SHARE:
176             printf("COMMS_SHARE");
177             break;
178             
179         case SMBC_IPC_SHARE:
180             printf("IPC_SHARE");
181             break;
182             
183         case SMBC_DIR:
184             printf("DIR");
185             break;
186             
187         case SMBC_FILE:
188             printf("FILE");
189
190             p = path + strlen(path);
191             strcat(p, "/");
192             strcat(p+1, dirent->name);
193             if (smbc_stat(path, &stat) < 0)
194             {
195                 printf(" unknown size (reason %d: %s)",
196                        errno, strerror(errno));
197             }
198             else
199             {
200                 printf(" size %lu", (unsigned long) stat.st_size);
201             }
202             *p = '\0';
203
204             break;
205             
206         case SMBC_LINK:
207             printf("LINK");
208             break;
209         }
210
211         printf("\n");
212
213         if (scan &&
214             (dirent->smbc_type == SMBC_WORKGROUP ||
215              dirent->smbc_type == SMBC_SERVER))
216         {
217             /*
218              * don't append server name to workgroup; what we want is:
219              *
220              *   smb://workgroup_name
221              * or
222              *   smb://server_name
223              *
224              */
225             snprintf(buf, sizeof(buf), "smb://%s", dirent->name);
226             browse(buf, scan, indent + 2);
227         }
228     }
229
230     smbc_closedir(dir);
231 }
232