more autogen stuff
[Samba/gbeck.git] / source / client / testsmbc.c
blobd2dce7ccab678f66be2d5df6914aca56f2478fd2
1 /*
2 Unix SMB/Netbios implementation.
3 Version 2.0
4 SMB client library test program
5 Copyright (C) Andrew Tridgell 1998
6 Copyright (C) Richard Sharpe 2000
7 Copyright (C) John Terpsra 2000
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <stdio.h>
25 #include <errno.h>
26 #include <sys/time.h>
27 #include <libsmbclient.h>
29 void auth_fn(const char *server, const char *share,
30 char *workgroup, int wgmaxlen, char *username, int unmaxlen,
31 char *password, int pwmaxlen)
33 char temp[128];
35 fprintf(stdout, "Need password for //%s/%s\n", server, share);
37 fprintf(stdout, "Enter workgroup: [%s] ", workgroup);
38 fgets(temp, sizeof(temp), stdin);
40 if (temp[strlen(temp) - 1] == 0x0a) /* A new line? */
41 temp[strlen(temp) - 1] = 0x00;
43 if (temp[0]) strncpy(workgroup, temp, wgmaxlen - 1);
45 fprintf(stdout, "Enter username: [%s] ", username);
46 fgets(temp, sizeof(temp), stdin);
48 if (temp[strlen(temp) - 1] == 0x0a) /* A new line? */
49 temp[strlen(temp) - 1] = 0x00;
51 if (temp[0]) strncpy(username, temp, unmaxlen - 1);
53 fprintf(stdout, "Enter password: [%s] ", password);
54 fgets(temp, sizeof(temp), stdin);
56 if (temp[strlen(temp) - 1] == 0x0a) /* A new line? */
57 temp[strlen(temp) - 1] = 0x00;
59 if (temp[0]) strncpy(password, temp, pwmaxlen - 1);
63 int global_id = 0;
65 void print_list_fn(struct print_job_info *pji)
68 fprintf(stdout, "Print job: ID: %u, Prio: %u, Size: %u, User: %s, Name: %s\n",
69 pji->id, pji->priority, pji->size, pji->user, pji->name);
71 global_id = pji->id;
75 int main(int argc, char *argv[])
77 int err, fd, dh1, dh2, dh3, dsize, dirc;
78 const char *file = "smb://samba/public/testfile.txt";
79 const char *file2 = "smb://samba/public/testfile2.txt";
80 char buff[256];
81 char dirbuf[512];
82 struct smbc_dirent *dirp;
83 struct stat st1, st2;
85 err = smbc_init(auth_fn, 10); /* Initialize things */
87 if (err < 0) {
89 fprintf(stderr, "Initializing the smbclient library ...: %s\n", strerror(errno));
93 if (argc > 1) {
95 /* Try to list the print jobs ... */
97 if (smbc_list_print_jobs("smb://samba/pclp", print_list_fn) < 0) {
99 fprintf(stderr, "Could not list print jobs: %s, %d\n", strerror(errno), errno);
100 exit(1);
104 /* Try to delete the last job listed */
106 if (global_id > 0) {
108 fprintf(stdout, "Trying to delete print job %u\n", global_id);
110 if (smbc_unlink_print_job("smb://samba/pclp", global_id) < 0) {
112 fprintf(stderr, "Failed to unlink job id %u, %s, %u\n", global_id,
113 strerror(errno), errno);
115 exit(1);
121 /* Try to print a file ... */
123 if (smbc_print_file("smb://samba/public/testfile2.txt", "smb://samba/pclp") < 0) {
125 fprintf(stderr, "Failed to print job: %s %u\n", strerror(errno), errno);
126 exit(1);
130 /* Try to delete argv[1] as a file ... */
132 if (smbc_unlink(argv[1]) < 0) {
134 fprintf(stderr, "Could not unlink: %s, %s, %d\n",
135 argv[1], strerror(errno), errno);
137 exit(0);
141 if ((dh1 = smbc_opendir("smb://"))<1) {
143 fprintf(stderr, "Could not open directory: smb://: %s\n",
144 strerror(errno));
146 exit(1);
150 if ((dh2 = smbc_opendir("smb://sambanet")) < 0) {
152 fprintf(stderr, "Could not open directory: smb://sambanet: %s\n",
153 strerror(errno));
155 exit(1);
159 if ((dh3 = smbc_opendir("smb://samba")) < 0) {
161 fprintf(stderr, "Could not open directory: smb://samba: %s\n",
162 strerror(errno));
164 exit(1);
168 fprintf(stdout, "Directory handles: %u, %u, %u\n", dh1, dh2, dh3);
170 /* Now, list those directories, but in funny ways ... */
172 dirp = (struct smbc_dirent *)dirbuf;
174 if ((dirc = smbc_getdents(dh1, dirp, sizeof(dirbuf))) < 0) {
176 fprintf(stderr, "Problems getting directory entries: %s\n",
177 strerror(errno));
179 exit(1);
183 /* Now, process the list of names ... */
185 fprintf(stdout, "Directory listing, size = %u\n", dirc);
187 while (dirc > 0) {
189 dsize = dirp->dirlen;
190 fprintf(stdout, "Dir Ent, Type: %u, Name: %s, Comment: %s\n",
191 dirp->smbc_type, dirp->name, dirp->comment);
193 (char *)dirp += dsize;
194 (char *)dirc -= dsize;
198 dirp = (struct smbc_dirent *)dirbuf;
200 if ((dirc = smbc_getdents(dh2, dirp, sizeof(dirbuf))) < 0) {
202 fprintf(stderr, "Problems getting directory entries: %s\n",
203 strerror(errno));
205 exit(1);
209 /* Now, process the list of names ... */
211 fprintf(stdout, "\nDirectory listing, size = %u\n", dirc);
213 while (dirc > 0) {
215 dsize = dirp->dirlen;
216 fprintf(stdout, "Dir Ent, Type: %u, Name: %s, Comment: %s\n",
217 dirp->smbc_type, dirp->name, dirp->comment);
219 (char *)dirp += dsize;
220 (char *)dirc -= dsize;
224 dirp = (struct smbc_dirent *)dirbuf;
226 if ((dirc = smbc_getdents(dh3, dirp, sizeof(dirbuf))) < 0) {
228 fprintf(stderr, "Problems getting directory entries: %s\n",
229 strerror(errno));
231 exit(1);
235 /* Now, process the list of names ... */
237 fprintf(stdout, "Directory listing, size = %u\n", dirc);
239 while (dirc > 0) {
241 dsize = dirp->dirlen;
242 fprintf(stdout, "\nDir Ent, Type: %u, Name: %s, Comment: %s\n",
243 dirp->smbc_type, dirp->name, dirp->comment);
245 (char *)dirp += dsize;
246 (char *)dirc -= dsize;
250 exit(1);
254 /* For now, open a file on a server that is hard coded ... later will
255 * read from the command line ...
258 fd = smbc_open(file, O_RDWR | O_CREAT, 0666);
260 if (fd < 0) {
262 fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
263 exit(0);
267 fprintf(stdout, "Opened or created file: %s\n", file);
269 /* Now, write some date to the file ... */
271 bzero(buff, sizeof(buff));
272 strcpy(buff, "Some test data for the moment ...");
274 err = smbc_write(fd, buff, sizeof(buff));
276 if (err < 0) {
278 fprintf(stderr, "writing file: %s: %s\n", file, strerror(errno));
279 exit(0);
283 fprintf(stdout, "Wrote %d bytes to file: %s\n", sizeof(buff), buff);
285 /* Now, seek the file back to offset 0 */
287 err = smbc_lseek(fd, SEEK_SET, 0);
289 if (err < 0) {
291 fprintf(stderr, "Seeking file: %s: %s\n", file, strerror(errno));
292 exit(0);
296 fprintf(stdout, "Completed lseek on file: %s\n", file);
298 /* Now, read the file contents back ... */
300 err = smbc_read(fd, buff, sizeof(buff));
302 if (err < 0) {
304 fprintf(stderr, "Reading file: %s: %s\n", file, strerror(errno));
305 exit(0);
309 fprintf(stdout, "Read file: %s\n", buff); /* Should check the contents */
311 fprintf(stdout, "Now fstat'ing file: %s\n", file);
313 err = smbc_fstat(fd, &st1);
315 if (err < 0) {
317 fprintf(stderr, "Fstat'ing file: %s: %s\n", file, strerror(errno));
318 exit(0);
323 /* Now, close the file ... */
325 err = smbc_close(fd);
327 if (err < 0) {
329 fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
333 /* Now, rename the file ... */
335 err = smbc_rename(file, file2);
337 if (err < 0) {
339 fprintf(stderr, "Renaming file: %s to %s: %s\n", file, file2, strerror(errno));
343 fprintf(stdout, "Renamed file %s to %s\n", file, file2);
345 /* Now, create a file and delete it ... */
347 fprintf(stdout, "Now, creating file: %s so we can delete it.\n", file);
349 fd = smbc_open(file, O_RDWR | O_CREAT, 0666);
351 if (fd < 0) {
353 fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
354 exit(0);
358 fprintf(stdout, "Opened or created file: %s\n", file);
360 err = smbc_close(fd);
362 if (err < 0) {
364 fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
365 exit(0);
369 /* Now, delete the file ... */
371 fprintf(stdout, "File %s created, now deleting ...\n", file);
373 err = smbc_unlink(file);
375 if (err < 0) {
377 fprintf(stderr, "Deleting file: %s: %s\n", file, strerror(errno));
378 exit(0);
382 /* Now, stat the file, file 2 ... */
384 fprintf(stdout, "Now stat'ing file: %s\n", file);
386 err = smbc_stat(file2, &st2);
388 if (err < 0) {
390 fprintf(stderr, "Stat'ing file: %s: %s\n", file, strerror(errno));
391 exit(0);
395 fprintf(stdout, "Stat'ed file: %s. Size = %d, mode = %04X\n", file2,
396 st2.st_size, st2.st_mode);
397 fprintf(stdout, " time: %s\n", ctime(&st2.st_atime));
398 fprintf(stdout, "Earlier stat: %s, Size = %d, mode = %04X\n", file,
399 st1.st_size, st1.st_mode);
400 fprintf(stdout, " time: %s\n", ctime(&st1.st_atime));
402 /* Now, make a directory ... */
404 fprintf(stdout, "Making directory smb://samba/public/make-dir\n");
406 if (smbc_mkdir("smb://samba/public/make-dir", 0666) < 0) {
408 fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n",
409 strerror(errno));
411 if (errno == EEXIST) { /* Try to delete the directory */
413 fprintf(stdout, "Trying to delete directory: smb://samba/public/make-dir\n");
415 if (smbc_rmdir("smb://samba/public/make-dir") < 0) { /* Error */
417 fprintf(stderr, "Error removing directory: smb://samba/public/make-dir: %s\n", strerror(errno));
419 exit(0);
423 fprintf(stdout, "Making directory: smb://samba/public/make-dir\n");
425 if (smbc_mkdir("smb://samba/public/make-dir", 666) < 0) {
427 fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n",
428 strerror(errno));
430 fprintf(stderr, "I give up!\n");
432 exit(1);
438 exit(0);
442 fprintf(stdout, "Made dir: make-dir\n");