source: branches/samba-3.2.x/examples/libsmbclient/testsmbc.c

Last change on this file was 133, checked in by Paul Smedley, 17 years ago

Update trunk to 3.2.0pre3

File size: 6.7 KB
Line 
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 <sys/time.h>
25#include <string.h>
26#include <unistd.h>
27#include <stdlib.h>
28#include "libsmbclient.h"
29#include "get_auth_data_fn.h"
30
31int global_id = 0;
32
33void print_list_fn(struct print_job_info *pji)
34{
35
36 fprintf(stdout, "Print job: ID: %u, Prio: %u, Size: %u, User: %s, Name: %s\n",
37 pji->id, pji->priority, pji->size, pji->user, pji->name);
38
39 global_id = pji->id;
40
41}
42
43int main(int argc, char *argv[])
44{
45 int err, fd, dh1, dh2, dh3, dsize, dirc;
46 const char *file = "smb://samba/public/testfile.txt";
47 const char *file2 = "smb://samba/public/testfile2.txt";
48 char buff[256];
49 char dirbuf[512];
50 char *dirp;
51 struct stat st1, st2;
52
53 err = smbc_init(get_auth_data_fn, 10); /* Initialize things */
54
55 if (err < 0) {
56
57 fprintf(stderr, "Initializing the smbclient library ...: %s\n", strerror(errno));
58
59 }
60
61 if (argc > 1) {
62
63 if ((dh1 = smbc_opendir(argv[1]))<1) {
64
65 fprintf(stderr, "Could not open directory: %s: %s\n",
66 argv[1], strerror(errno));
67
68 exit(1);
69
70 }
71
72 fprintf(stdout, "Directory handles: %u, %u, %u\n", dh1, dh2, dh3);
73
74 /* Now, list those directories, but in funny ways ... */
75
76 dirp = (char *)dirbuf;
77
78 if ((dirc = smbc_getdents(dh1, (struct smbc_dirent *)dirp,
79 sizeof(dirbuf))) < 0) {
80
81 fprintf(stderr, "Problems getting directory entries: %s\n",
82 strerror(errno));
83
84 exit(1);
85
86 }
87
88 /* Now, process the list of names ... */
89
90 fprintf(stdout, "Directory listing, size = %u\n", dirc);
91
92 while (dirc > 0) {
93
94 dsize = ((struct smbc_dirent *)dirp)->dirlen;
95 fprintf(stdout, "Dir Ent, Type: %u, Name: %s, Comment: %s\n",
96 ((struct smbc_dirent *)dirp)->smbc_type,
97 ((struct smbc_dirent *)dirp)->name,
98 ((struct smbc_dirent *)dirp)->comment);
99
100 dirp += dsize;
101 dirc -= dsize;
102
103 }
104
105 dirp = (char *)dirbuf;
106
107 exit(1);
108
109 }
110
111 /* For now, open a file on a server that is hard coded ... later will
112 * read from the command line ...
113 */
114
115 fd = smbc_open(file, O_RDWR | O_CREAT | O_TRUNC, 0666);
116
117 if (fd < 0) {
118
119 fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
120 exit(0);
121
122 }
123
124 fprintf(stdout, "Opened or created file: %s\n", file);
125
126 /* Now, write some date to the file ... */
127
128 bzero(buff, sizeof(buff));
129 strcpy(buff, "Some test data for the moment ...");
130
131 err = smbc_write(fd, buff, sizeof(buff));
132
133 if (err < 0) {
134
135 fprintf(stderr, "writing file: %s: %s\n", file, strerror(errno));
136 exit(0);
137
138 }
139
140 fprintf(stdout, "Wrote %d bytes to file: %s\n", sizeof(buff), buff);
141
142 /* Now, seek the file back to offset 0 */
143
144 err = smbc_lseek(fd, SEEK_SET, 0);
145
146 if (err < 0) {
147
148 fprintf(stderr, "Seeking file: %s: %s\n", file, strerror(errno));
149 exit(0);
150
151 }
152
153 fprintf(stdout, "Completed lseek on file: %s\n", file);
154
155 /* Now, read the file contents back ... */
156
157 err = smbc_read(fd, buff, sizeof(buff));
158
159 if (err < 0) {
160
161 fprintf(stderr, "Reading file: %s: %s\n", file, strerror(errno));
162 exit(0);
163
164 }
165
166 fprintf(stdout, "Read file: %s\n", buff); /* Should check the contents */
167
168 fprintf(stdout, "Now fstat'ing file: %s\n", file);
169
170 err = smbc_fstat(fd, &st1);
171
172 if (err < 0) {
173
174 fprintf(stderr, "Fstat'ing file: %s: %s\n", file, strerror(errno));
175 exit(0);
176
177 }
178
179
180 /* Now, close the file ... */
181
182 err = smbc_close(fd);
183
184 if (err < 0) {
185
186 fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
187
188 }
189
190 /* Now, rename the file ... */
191
192 err = smbc_rename(file, file2);
193
194 if (err < 0) {
195
196 fprintf(stderr, "Renaming file: %s to %s: %s\n", file, file2, strerror(errno));
197
198 }
199
200 fprintf(stdout, "Renamed file %s to %s\n", file, file2);
201
202 /* Now, create a file and delete it ... */
203
204 fprintf(stdout, "Now, creating file: %s so we can delete it.\n", file);
205
206 fd = smbc_open(file, O_RDWR | O_CREAT, 0666);
207
208 if (fd < 0) {
209
210 fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
211 exit(0);
212
213 }
214
215 fprintf(stdout, "Opened or created file: %s\n", file);
216
217 err = smbc_close(fd);
218
219 if (err < 0) {
220
221 fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
222 exit(0);
223
224 }
225
226 /* Now, delete the file ... */
227
228 fprintf(stdout, "File %s created, now deleting ...\n", file);
229
230 err = smbc_unlink(file);
231
232 if (err < 0) {
233
234 fprintf(stderr, "Deleting file: %s: %s\n", file, strerror(errno));
235 exit(0);
236
237 }
238
239 /* Now, stat the file, file 2 ... */
240
241 fprintf(stdout, "Now stat'ing file: %s\n", file);
242
243 err = smbc_stat(file2, &st2);
244
245 if (err < 0) {
246
247 fprintf(stderr, "Stat'ing file: %s: %s\n", file, strerror(errno));
248 exit(0);
249
250 }
251
252 fprintf(stdout, "Stat'ed file: %s. Size = %d, mode = %04X\n", file2,
253 (int)st2.st_size, st2.st_mode);
254 fprintf(stdout, " time: %s\n", ctime(&st2.st_atime));
255 fprintf(stdout, "Earlier stat: %s, Size = %d, mode = %04X\n", file,
256 (int)st1.st_size, st1.st_mode);
257 fprintf(stdout, " time: %s\n", ctime(&st1.st_atime));
258
259 /* Now, make a directory ... */
260
261 fprintf(stdout, "Making directory smb://samba/public/make-dir\n");
262
263 if (smbc_mkdir("smb://samba/public/make-dir", 0666) < 0) {
264
265 fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n",
266 strerror(errno));
267
268 if (errno == EEXIST) { /* Try to delete the directory */
269
270 fprintf(stdout, "Trying to delete directory: smb://samba/public/make-dir\n");
271
272 if (smbc_rmdir("smb://samba/public/make-dir") < 0) { /* Error */
273
274 fprintf(stderr, "Error removing directory: smb://samba/public/make-dir: %s\n", strerror(errno));
275
276 exit(0);
277
278 }
279
280 fprintf(stdout, "Making directory: smb://samba/public/make-dir\n");
281
282 if (smbc_mkdir("smb://samba/public/make-dir", 666) < 0) {
283
284 fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n",
285 strerror(errno));
286
287 fprintf(stderr, "I give up!\n");
288
289 exit(1);
290
291 }
292
293 }
294
295 exit(0);
296
297 }
298
299 fprintf(stdout, "Made dir: make-dir\n");
300 return 0;
301}
Note: See TracBrowser for help on using the repository browser.