27d6a6973882e377f6b4429d7ac14d742408872f
[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 void error_message(char * pMessage)
13 {
14     printf("ERROR: %s\n", pMessage);
15 }
16
17
18 int
19 main(int argc, char * argv[])
20 {
21     int                         debug = 0;
22     int                         opt;
23     char *                      p;
24     char *                      q;
25     char                        buf[1024];
26     int                         dir;
27     struct stat                 stat;
28     struct smbc_dirent *        dirent;
29     poptContext pc;
30     struct poptOption           long_options[] =
31         {
32             POPT_AUTOHELP
33             {
34                 "debug", 'd', POPT_ARG_INT, &debug,
35                 0, "Set debug level", "integer"
36             },
37             {
38                 NULL
39             }
40         };
41     
42     setbuf(stdout, NULL);
43
44     pc = poptGetContext("opendir", argc, (const char **)argv, long_options, 0);
45     
46     poptSetOtherOptionHelp(pc, "");
47     
48     while ((opt = poptGetNextOpt(pc)) != -1) {
49         printf("Got option %d = %c\n", opt, opt);
50         switch (opt) {
51         }
52     }
53
54     if (smbc_init(get_auth_data_fn, debug) != 0)
55     {
56         printf("Could not initialize smbc_ library\n");
57         return 1;
58     }
59     
60     for (fputs("url: ", stdout), p = fgets(buf, sizeof(buf), stdin);
61          p != NULL && *p != '\n' && *p != '\0';
62          fputs("url: ", stdout), p = fgets(buf, sizeof(buf), stdin))
63     {
64         if ((p = strchr(buf, '\n')) != NULL)
65         {
66             *p = '\0';
67         }
68         
69         printf("Opening (%s)...\n", buf);
70         
71         if ((dir = smbc_opendir(buf)) < 0)
72         {
73             printf("Could not open directory [%s] (%d:%s)\n",
74                    buf, errno, strerror(errno));
75             continue;
76         }
77
78         while ((dirent = smbc_readdir(dir)) != NULL)
79         {
80             printf("%-30s", dirent->name);
81             printf("%-30s", dirent->comment);
82
83             switch(dirent->smbc_type)
84             {
85             case SMBC_WORKGROUP:
86                 printf("WORKGROUP");
87                 break;
88             
89             case SMBC_SERVER:
90                 printf("SERVER");
91                 break;
92             
93             case SMBC_FILE_SHARE:
94                 printf("FILE_SHARE");
95                 break;
96             
97             case SMBC_PRINTER_SHARE:
98                 printf("PRINTER_SHARE");
99                 break;
100             
101             case SMBC_COMMS_SHARE:
102                 printf("COMMS_SHARE");
103                 break;
104             
105             case SMBC_IPC_SHARE:
106                 printf("IPC_SHARE");
107                 break;
108             
109             case SMBC_DIR:
110                 printf("DIR");
111                 break;
112             
113             case SMBC_FILE:
114                 printf("FILE");
115
116                 q = buf + strlen(buf);
117                 strcat(q, "/");
118                 strcat(q+1, dirent->name);
119                 if (smbc_stat(buf, &stat) < 0)
120                 {
121                     printf(" unknown size (reason %d: %s)",
122                            errno, strerror(errno));
123                 }
124                 else
125                 {
126                     printf(" size %lu", (unsigned long) stat.st_size);
127                 }
128                 *p = '\0';
129
130                 break;
131             
132             case SMBC_LINK:
133                 printf("LINK");
134                 break;
135             }
136
137             printf("\n");
138         }
139
140         smbc_closedir(dir);
141     }
142
143     exit(0);
144 }