r6108: Added smbsh/smbwrapper for Linux to example/libsmbclient tree; provided more...
[kai/samba.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 <libsmbclient.h>
9 #include <stdlib.h>
10
11 void error_message(char * pMessage)
12 {
13     printf("ERROR: %s\n", pMessage);
14 }
15
16
17 static void
18 get_auth_data_fn(const char * pServer,
19                  const char * pShare,
20                  char * pWorkgroup,
21                  int maxLenWorkgroup,
22                  char * pUsername,
23                  int maxLenUsername,
24                  char * pPassword,
25                  int maxLenPassword)
26     
27 {
28     char temp[128];
29     
30     printf("Entered get_auth_data_fn\n");
31
32     fprintf(stdout, "Need password for //%s/%s\n", pServer, pShare);
33     
34     fprintf(stdout, "Username: [%s] ", pUsername);
35     fgets(temp, sizeof(temp), stdin);
36     
37     if (temp[strlen(temp) - 1] == '\n') /* A new line? */
38     {
39         temp[strlen(temp) - 1] = '\0';
40     }
41     
42     if (temp[0] != '\0')
43     {
44         strncpy(pUsername, temp, maxLenUsername - 1);
45     }
46     
47     strcpy(temp, getpass("Password: "));
48     
49     if (temp[strlen(temp) - 1] == '\n') /* A new line? */
50     {
51         temp[strlen(temp) - 1] = '\0';
52     }
53     
54     if (temp[0] != '\0')
55     {
56         strncpy(pPassword, temp, maxLenPassword - 1);
57     }
58
59     fprintf(stdout, "Workgroup: ");
60     fgets(temp, sizeof(temp), stdin);
61     
62     if (temp[strlen(temp) - 1] == '\n') /* A new line? */
63     {
64         temp[strlen(temp) - 1] = '\0';
65     }
66     
67     if (temp[0] != '\0')
68     {
69         strncpy(pWorkgroup, temp, maxLenWorkgroup - 1);
70     }
71
72     putchar('\n');
73 }
74
75
76 int
77 main(int argc, char * argv[])
78 {
79     int                         debug = 0;
80     int                         opt;
81     char *                      p;
82     char *                      q;
83     char                        buf[1024];
84     int                         dir;
85     struct stat                 stat;
86     struct smbc_dirent *        dirent;
87     poptContext pc;
88     struct poptOption           long_options[] =
89         {
90             POPT_AUTOHELP
91             {
92                 "debug", 'd', POPT_ARG_INT, &debug,
93                 0, "Set debug level", "integer"
94             },
95             {
96                 NULL
97             }
98         };
99     
100     setbuf(stdout, NULL);
101
102     pc = poptGetContext("opendir", argc, (const char **)argv, long_options, 0);
103     
104     poptSetOtherOptionHelp(pc, "");
105     
106     while ((opt = poptGetNextOpt(pc)) != -1) {
107         printf("Got option %d = %c\n", opt, opt);
108         switch (opt) {
109         }
110     }
111
112     if (smbc_init(get_auth_data_fn, debug) != 0)
113     {
114         printf("Could not initialize smbc_ library\n");
115         return 1;
116     }
117     
118     for (fputs("url: ", stdout), p = fgets(buf, sizeof(buf), stdin);
119          p != NULL && *p != '\n' && *p != '\0';
120          fputs("url: ", stdout), p = fgets(buf, sizeof(buf), stdin))
121     {
122         if ((p = strchr(buf, '\n')) != NULL)
123         {
124             *p = '\0';
125         }
126         
127         printf("Opening (%s)...\n", buf);
128         
129         if ((dir = smbc_opendir(buf)) < 0)
130         {
131             printf("Could not open directory [%s] (%d:%s)\n",
132                    buf, errno, strerror(errno));
133             continue;
134         }
135
136         while ((dirent = smbc_readdir(dir)) != NULL)
137         {
138             printf("%-30s", dirent->name);
139             printf("%-30s", dirent->comment);
140
141             switch(dirent->smbc_type)
142             {
143             case SMBC_WORKGROUP:
144                 printf("WORKGROUP");
145                 break;
146             
147             case SMBC_SERVER:
148                 printf("SERVER");
149                 break;
150             
151             case SMBC_FILE_SHARE:
152                 printf("FILE_SHARE");
153                 break;
154             
155             case SMBC_PRINTER_SHARE:
156                 printf("PRINTER_SHARE");
157                 break;
158             
159             case SMBC_COMMS_SHARE:
160                 printf("COMMS_SHARE");
161                 break;
162             
163             case SMBC_IPC_SHARE:
164                 printf("IPC_SHARE");
165                 break;
166             
167             case SMBC_DIR:
168                 printf("DIR");
169                 break;
170             
171             case SMBC_FILE:
172                 printf("FILE");
173
174                 q = buf + strlen(buf);
175                 strcat(q, "/");
176                 strcat(q+1, dirent->name);
177                 if (smbc_stat(buf, &stat) < 0)
178                 {
179                     printf(" unknown size (reason %d: %s)",
180                            errno, strerror(errno));
181                 }
182                 else
183                 {
184                     printf(" size %lu", (unsigned long) stat.st_size);
185                 }
186                 *p = '\0';
187
188                 break;
189             
190             case SMBC_LINK:
191                 printf("LINK");
192                 break;
193             }
194
195             printf("\n");
196         }
197
198         smbc_closedir(dir);
199     }
200
201     exit(0);
202 }