source: branches/samba-3.0/examples/libsmbclient/testsmbc.c@ 334

Last change on this file since 334 was 1, checked in by Paul Smedley, 18 years ago

Initial code import

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