ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/SambaTest/SambaTest.cpp
Revision: 412
Committed: 2005-01-14T20:42:47-08:00 (20 years, 5 months ago) by douglas
File size: 2188 byte(s)
Log Message:
Meep!

File Contents

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