1 |
// Samba Test |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include <iostream> |
8 |
#include <string> |
9 |
#include <cstdio> |
10 |
#include <cstring> |
11 |
|
12 |
#include <errno.h> |
13 |
#include <pwd.h> |
14 |
#include <unistd.h> |
15 |
#include <libsmbclient.h> |
16 |
|
17 |
struct Exception |
18 |
{ |
19 |
int code; |
20 |
Exception(int code) : code(code) {} |
21 |
}; |
22 |
|
23 |
std::ostream& operator<<(std::ostream& out, const Exception& exception) |
24 |
{ |
25 |
return out << "Exception(" << exception.code << ")[" << ::strerror(exception.code) << ']'; |
26 |
} |
27 |
|
28 |
inline int check(int code) |
29 |
{ |
30 |
if (code < 0) throw Exception(errno); |
31 |
|
32 |
return code; |
33 |
} |
34 |
|
35 |
extern "C" |
36 |
{ |
37 |
void authenticate(const char* srv, const char* shr, char* wg, int wglen, char* un, int unlen, char* pw, int pwlen) |
38 |
{ |
39 |
std::cout << "srv = " << srv << std::endl << "shr = " << shr << std::endl << "wg = " << wg << std::endl << "un = " << un << std::endl << "un = "; |
40 |
|
41 |
std::string line; |
42 |
|
43 |
std::getline(std::cin, line); |
44 |
|
45 |
if (!line.empty()) ::snprintf(un, unlen, line.c_str()); |
46 |
|
47 |
::snprintf(pw, pwlen, "%s", ::getpass("pw = ")); |
48 |
} |
49 |
} |
50 |
|
51 |
int main(int argc, char* argv[]) |
52 |
{ |
53 |
std::string uri("smb://"); |
54 |
|
55 |
for (int index(1); index < argc; ++index) |
56 |
{ |
57 |
uri += argv[index]; |
58 |
uri += '/'; |
59 |
} |
60 |
|
61 |
try |
62 |
{ |
63 |
check(::smbc_init(authenticate, 2)); |
64 |
|
65 |
int dir(check(::smbc_opendir(uri.c_str()))); |
66 |
::smbc_dirent* ent; |
67 |
|
68 |
while ((ent = ::smbc_readdir(dir)) != NULL) |
69 |
{ |
70 |
std::cout << "ent.smbc_type = "; |
71 |
|
72 |
switch (ent->smbc_type) |
73 |
{ |
74 |
case SMBC_WORKGROUP: |
75 |
std::cout << "workgroup"; |
76 |
break; |
77 |
case SMBC_SERVER: |
78 |
std::cout << "server"; |
79 |
break; |
80 |
case SMBC_FILE_SHARE: |
81 |
std::cout << "file share"; |
82 |
break; |
83 |
case SMBC_PRINTER_SHARE: |
84 |
std::cout << "printer share"; |
85 |
break; |
86 |
case SMBC_COMMS_SHARE: |
87 |
std::cout << "comms share"; |
88 |
break; |
89 |
case SMBC_IPC_SHARE: |
90 |
std::cout << "ipc share"; |
91 |
break; |
92 |
case SMBC_DIR: |
93 |
std::cout << "dir"; |
94 |
break; |
95 |
case SMBC_FILE: |
96 |
std::cout << "file"; |
97 |
break; |
98 |
case SMBC_LINK: |
99 |
std::cout << "link"; |
100 |
} |
101 |
|
102 |
std::cout << std::endl << "ent.comment = " << ent->comment << std::endl << "ent.name = " << ent->name << std::endl; |
103 |
} |
104 |
|
105 |
check(::smbc_closedir(dir)); |
106 |
} |
107 |
catch (Exception exception) |
108 |
{ |
109 |
std::cerr << exception << std::endl; |
110 |
} |
111 |
|
112 |
return 0; |
113 |
} |