1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | SMB client library test program
|
---|
4 | Copyright (C) Andrew Tridgell 1998
|
---|
5 | Copyright (C) Richard Sharpe 2000
|
---|
6 | Copyright (C) John Terpsra 2000
|
---|
7 |
|
---|
8 | This program is free software; you can redistribute it and/or modify
|
---|
9 | it under the terms of the GNU General Public License as published by
|
---|
10 | the Free Software Foundation; either version 3 of the License, or
|
---|
11 | (at your option) any later version.
|
---|
12 |
|
---|
13 | This program is distributed in the hope that it will be useful,
|
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
16 | GNU General Public License for more details.
|
---|
17 |
|
---|
18 | You should have received a copy of the GNU General Public License
|
---|
19 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include <stdio.h>
|
---|
23 | #include <errno.h>
|
---|
24 | #include <time.h>
|
---|
25 | #include <sys/time.h>
|
---|
26 | #include <string.h>
|
---|
27 | #include <unistd.h>
|
---|
28 | #include <stdlib.h>
|
---|
29 | #include "libsmbclient.h"
|
---|
30 | #include "get_auth_data_fn.h"
|
---|
31 |
|
---|
32 | int main(int argc, char *argv[])
|
---|
33 | {
|
---|
34 | int err, fd, dh1, dsize, dirc;
|
---|
35 | const char *file = "smb://samba/public/testfile.txt";
|
---|
36 | const char *file2 = "smb://samba/public/testfile2.txt";
|
---|
37 | char buff[256];
|
---|
38 | char dirbuf[512];
|
---|
39 | char *dirp;
|
---|
40 | struct stat st1, st2;
|
---|
41 |
|
---|
42 | err = smbc_init(get_auth_data_fn, 10); /* Initialize things */
|
---|
43 |
|
---|
44 | if (err < 0) {
|
---|
45 |
|
---|
46 | fprintf(stderr, "Initializing the smbclient library ...: %s\n", strerror(errno));
|
---|
47 |
|
---|
48 | }
|
---|
49 |
|
---|
50 | if (argc > 1) {
|
---|
51 |
|
---|
52 | if ((dh1 = smbc_opendir(argv[1]))<1) {
|
---|
53 |
|
---|
54 | fprintf(stderr, "Could not open directory: %s: %s\n",
|
---|
55 | argv[1], strerror(errno));
|
---|
56 |
|
---|
57 | exit(1);
|
---|
58 |
|
---|
59 | }
|
---|
60 |
|
---|
61 | fprintf(stdout, "Directory handle: %u\n", dh1);
|
---|
62 |
|
---|
63 | /* Now, list those directories, but in funny ways ... */
|
---|
64 |
|
---|
65 | dirp = (char *)dirbuf;
|
---|
66 |
|
---|
67 | if ((dirc = smbc_getdents(dh1, (struct smbc_dirent *)dirp,
|
---|
68 | sizeof(dirbuf))) < 0) {
|
---|
69 |
|
---|
70 | fprintf(stderr, "Problems getting directory entries: %s\n",
|
---|
71 | strerror(errno));
|
---|
72 |
|
---|
73 | exit(1);
|
---|
74 |
|
---|
75 | }
|
---|
76 |
|
---|
77 | /* Now, process the list of names ... */
|
---|
78 |
|
---|
79 | fprintf(stdout, "Directory listing, size = %u\n", dirc);
|
---|
80 |
|
---|
81 | while (dirc > 0) {
|
---|
82 |
|
---|
83 | dsize = ((struct smbc_dirent *)dirp)->dirlen;
|
---|
84 | fprintf(stdout, "Dir Ent, Type: %u, Name: %s, Comment: %s\n",
|
---|
85 | ((struct smbc_dirent *)dirp)->smbc_type,
|
---|
86 | ((struct smbc_dirent *)dirp)->name,
|
---|
87 | ((struct smbc_dirent *)dirp)->comment);
|
---|
88 |
|
---|
89 | dirp += dsize;
|
---|
90 | dirc -= dsize;
|
---|
91 |
|
---|
92 | }
|
---|
93 |
|
---|
94 | dirp = (char *)dirbuf;
|
---|
95 |
|
---|
96 | exit(1);
|
---|
97 |
|
---|
98 | }
|
---|
99 |
|
---|
100 | /* For now, open a file on a server that is hard coded ... later will
|
---|
101 | * read from the command line ...
|
---|
102 | */
|
---|
103 |
|
---|
104 | fd = smbc_open(file, O_RDWR | O_CREAT | O_TRUNC, 0666);
|
---|
105 |
|
---|
106 | if (fd < 0) {
|
---|
107 |
|
---|
108 | fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
|
---|
109 | exit(0);
|
---|
110 |
|
---|
111 | }
|
---|
112 |
|
---|
113 | fprintf(stdout, "Opened or created file: %s\n", file);
|
---|
114 |
|
---|
115 | /* Now, write some date to the file ... */
|
---|
116 |
|
---|
117 | memset(buff, '\0', sizeof(buff));
|
---|
118 | strcpy(buff, "Some test data for the moment ...");
|
---|
119 |
|
---|
120 | err = smbc_write(fd, buff, sizeof(buff));
|
---|
121 |
|
---|
122 | if (err < 0) {
|
---|
123 |
|
---|
124 | fprintf(stderr, "writing file: %s: %s\n", file, strerror(errno));
|
---|
125 | exit(0);
|
---|
126 |
|
---|
127 | }
|
---|
128 |
|
---|
129 | fprintf(stdout, "Wrote %lu bytes to file: %s\n",
|
---|
130 | (unsigned long) sizeof(buff), buff);
|
---|
131 |
|
---|
132 | /* Now, seek the file back to offset 0 */
|
---|
133 |
|
---|
134 | err = smbc_lseek(fd, SEEK_SET, 0);
|
---|
135 |
|
---|
136 | if (err < 0) {
|
---|
137 |
|
---|
138 | fprintf(stderr, "Seeking file: %s: %s\n", file, strerror(errno));
|
---|
139 | exit(0);
|
---|
140 |
|
---|
141 | }
|
---|
142 |
|
---|
143 | fprintf(stdout, "Completed lseek on file: %s\n", file);
|
---|
144 |
|
---|
145 | /* Now, read the file contents back ... */
|
---|
146 |
|
---|
147 | err = smbc_read(fd, buff, sizeof(buff));
|
---|
148 |
|
---|
149 | if (err < 0) {
|
---|
150 |
|
---|
151 | fprintf(stderr, "Reading file: %s: %s\n", file, strerror(errno));
|
---|
152 | exit(0);
|
---|
153 |
|
---|
154 | }
|
---|
155 |
|
---|
156 | fprintf(stdout, "Read file: %s\n", buff); /* Should check the contents */
|
---|
157 |
|
---|
158 | fprintf(stdout, "Now fstat'ing file: %s\n", file);
|
---|
159 |
|
---|
160 | err = smbc_fstat(fd, &st1);
|
---|
161 |
|
---|
162 | if (err < 0) {
|
---|
163 |
|
---|
164 | fprintf(stderr, "Fstat'ing file: %s: %s\n", file, strerror(errno));
|
---|
165 | exit(0);
|
---|
166 |
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | /* Now, close the file ... */
|
---|
171 |
|
---|
172 | err = smbc_close(fd);
|
---|
173 |
|
---|
174 | if (err < 0) {
|
---|
175 |
|
---|
176 | fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
|
---|
177 |
|
---|
178 | }
|
---|
179 |
|
---|
180 | /* Now, rename the file ... */
|
---|
181 |
|
---|
182 | err = smbc_rename(file, file2);
|
---|
183 |
|
---|
184 | if (err < 0) {
|
---|
185 |
|
---|
186 | fprintf(stderr, "Renaming file: %s to %s: %s\n", file, file2, strerror(errno));
|
---|
187 |
|
---|
188 | }
|
---|
189 |
|
---|
190 | fprintf(stdout, "Renamed file %s to %s\n", file, file2);
|
---|
191 |
|
---|
192 | /* Now, create a file and delete it ... */
|
---|
193 |
|
---|
194 | fprintf(stdout, "Now, creating file: %s so we can delete it.\n", file);
|
---|
195 |
|
---|
196 | fd = smbc_open(file, O_RDWR | O_CREAT, 0666);
|
---|
197 |
|
---|
198 | if (fd < 0) {
|
---|
199 |
|
---|
200 | fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
|
---|
201 | exit(0);
|
---|
202 |
|
---|
203 | }
|
---|
204 |
|
---|
205 | fprintf(stdout, "Opened or created file: %s\n", file);
|
---|
206 |
|
---|
207 | err = smbc_close(fd);
|
---|
208 |
|
---|
209 | if (err < 0) {
|
---|
210 |
|
---|
211 | fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
|
---|
212 | exit(0);
|
---|
213 |
|
---|
214 | }
|
---|
215 |
|
---|
216 | /* Now, delete the file ... */
|
---|
217 |
|
---|
218 | fprintf(stdout, "File %s created, now deleting ...\n", file);
|
---|
219 |
|
---|
220 | err = smbc_unlink(file);
|
---|
221 |
|
---|
222 | if (err < 0) {
|
---|
223 |
|
---|
224 | fprintf(stderr, "Deleting file: %s: %s\n", file, strerror(errno));
|
---|
225 | exit(0);
|
---|
226 |
|
---|
227 | }
|
---|
228 |
|
---|
229 | /* Now, stat the file, file 2 ... */
|
---|
230 |
|
---|
231 | fprintf(stdout, "Now stat'ing file: %s\n", file);
|
---|
232 |
|
---|
233 | err = smbc_stat(file2, &st2);
|
---|
234 |
|
---|
235 | if (err < 0) {
|
---|
236 |
|
---|
237 | fprintf(stderr, "Stat'ing file: %s: %s\n", file, strerror(errno));
|
---|
238 | exit(0);
|
---|
239 |
|
---|
240 | }
|
---|
241 |
|
---|
242 | fprintf(stdout, "Stat'ed file: %s. Size = %d, mode = %04X\n", file2,
|
---|
243 | (int)st2.st_size, (unsigned int)st2.st_mode);
|
---|
244 | fprintf(stdout, " time: %s\n", ctime(&st2.st_atime));
|
---|
245 | fprintf(stdout, "Earlier stat: %s, Size = %d, mode = %04X\n", file,
|
---|
246 | (int)st1.st_size, (unsigned int)st1.st_mode);
|
---|
247 | fprintf(stdout, " time: %s\n", ctime(&st1.st_atime));
|
---|
248 |
|
---|
249 | /* Now, make a directory ... */
|
---|
250 |
|
---|
251 | fprintf(stdout, "Making directory smb://samba/public/make-dir\n");
|
---|
252 |
|
---|
253 | if (smbc_mkdir("smb://samba/public/make-dir", 0666) < 0) {
|
---|
254 |
|
---|
255 | fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n",
|
---|
256 | strerror(errno));
|
---|
257 |
|
---|
258 | if (errno == EEXIST) { /* Try to delete the directory */
|
---|
259 |
|
---|
260 | fprintf(stdout, "Trying to delete directory: smb://samba/public/make-dir\n");
|
---|
261 |
|
---|
262 | if (smbc_rmdir("smb://samba/public/make-dir") < 0) { /* Error */
|
---|
263 |
|
---|
264 | fprintf(stderr, "Error removing directory: smb://samba/public/make-dir: %s\n", strerror(errno));
|
---|
265 |
|
---|
266 | exit(0);
|
---|
267 |
|
---|
268 | }
|
---|
269 |
|
---|
270 | fprintf(stdout, "Making directory: smb://samba/public/make-dir\n");
|
---|
271 |
|
---|
272 | if (smbc_mkdir("smb://samba/public/make-dir", 666) < 0) {
|
---|
273 |
|
---|
274 | fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n",
|
---|
275 | strerror(errno));
|
---|
276 |
|
---|
277 | fprintf(stderr, "I give up!\n");
|
---|
278 |
|
---|
279 | exit(1);
|
---|
280 |
|
---|
281 | }
|
---|
282 |
|
---|
283 | }
|
---|
284 |
|
---|
285 | exit(0);
|
---|
286 |
|
---|
287 | }
|
---|
288 |
|
---|
289 | fprintf(stdout, "Made dir: make-dir\n");
|
---|
290 | return 0;
|
---|
291 | }
|
---|