9 #include <libsmbclient.h>
10 #include "get_auth_data_fn.h"
13 no_auth_data_fn(const char * pServer,
22 static void browse(char * path,
29 main(int argc, char * argv[])
43 struct poptOption long_options[] =
47 "debug", 'd', POPT_ARG_INT, &debug,
48 0, "Set debug level", "integer"
51 "stderr", 'e', POPT_ARG_NONE, &debug_stderr,
52 0, "Debug log to stderr instead of stdout", "integer"
55 "scan", 's', POPT_ARG_NONE, &scan,
56 0, "Scan for servers and shares", "integer"
59 "iterations", 'i', POPT_ARG_INT, &iterations,
60 0, "Iterations", "integer"
63 "noauth", 'A', POPT_ARG_NONE, &no_auth,
64 0, "Do not request authentication data", "integer"
73 pc = poptGetContext("opendir", argc, (const char **)argv, long_options, 0);
75 poptSetOtherOptionHelp(pc, "");
77 while ((opt = poptGetNextOpt(pc)) != -1) {
78 printf("Got option %d = %c\n", opt, opt);
83 /* Allocate a new context */
84 context = smbc_new_context();
86 printf("Could not allocate new smbc context\n");
90 /* If we're scanning, do no requests for authentication data */
95 /* Set mandatory options (is that a contradiction in terms?) */
96 context->debug = debug;
97 context->callbacks.auth_fn = (no_auth ? no_auth_data_fn : get_auth_data_fn);
99 /* If we've been asked to log to stderr instead of stdout... */
101 /* ... then set the option to do so */
102 smbc_option_set(context, "debug_stderr");
105 /* Initialize the context using the previously specified options */
106 if (!smbc_init_context(context)) {
107 smbc_free_context(context, 0);
108 printf("Could not initialize smbc context\n");
112 /* Tell the compatibility layer to use this context */
113 smbc_set_context(context);
118 iterations == -1 || iterations > 0;
119 iterations = (iterations == -1 ? iterations : --iterations))
121 snprintf(buf, sizeof(buf), "smb://");
122 browse(buf, scan, 0);
128 iterations == -1 || iterations > 0;
129 iterations = (iterations == -1 ? iterations : --iterations))
131 fputs("url: ", stdout);
132 p = fgets(buf, sizeof(buf), stdin);
138 if ((p = strchr(buf, '\n')) != NULL)
143 browse(buf, scan, 0);
152 no_auth_data_fn(const char * pServer,
164 static void browse(char * path, int scan, int indent)
170 struct smbc_dirent * dirent;
174 printf("Opening (%s)...\n", path);
177 if ((dir = smbc_opendir(path)) < 0)
179 printf("Could not open directory [%s] (%d:%s)\n",
180 path, errno, strerror(errno));
184 while ((dirent = smbc_readdir(dir)) != NULL)
186 printf("%*.*s%-30s", indent, indent, "", dirent->name);
188 switch(dirent->smbc_type)
198 case SMBC_FILE_SHARE:
199 printf("FILE_SHARE");
202 case SMBC_PRINTER_SHARE:
203 printf("PRINTER_SHARE");
206 case SMBC_COMMS_SHARE:
207 printf("COMMS_SHARE");
221 p = path + strlen(path);
223 strcat(p+1, dirent->name);
224 if (smbc_stat(path, &stat) < 0)
226 printf(" unknown size (reason %d: %s)",
227 errno, strerror(errno));
231 printf(" size %lu", (unsigned long) stat.st_size);
245 (dirent->smbc_type == SMBC_WORKGROUP ||
246 dirent->smbc_type == SMBC_SERVER))
249 * don't append server name to workgroup; what we want is:
251 * smb://workgroup_name
256 snprintf(buf, sizeof(buf), "smb://%s", dirent->name);
257 browse(buf, scan, indent + 2);