remove proto headers on realclean
[Samba.git] / examples / libsmbclient / testsmbc.c
blob7aae9d8561641b65dba16b54658f736da2e67387
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 <string.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <libsmbclient.h>
32 void auth_fn(const char *server, const char *share,
33 char *workgroup, int wgmaxlen, char *username, int unmaxlen,
34 char *password, int pwmaxlen)
36 char temp[128];
38 fprintf(stdout, "Need password for //%s/%s\n", server, share);
40 fprintf(stdout, "Enter workgroup: [%s] ", workgroup);
41 fgets(temp, sizeof(temp), stdin);
43 if (temp[strlen(temp) - 1] == 0x0a) /* A new line? */
44 temp[strlen(temp) - 1] = 0x00;
46 if (temp[0]) strncpy(workgroup, temp, wgmaxlen - 1);
48 fprintf(stdout, "Enter username: [%s] ", username);
49 fgets(temp, sizeof(temp), stdin);
51 if (temp[strlen(temp) - 1] == 0x0a) /* A new line? */
52 temp[strlen(temp) - 1] = 0x00;
54 if (temp[0]) strncpy(username, temp, unmaxlen - 1);
56 fprintf(stdout, "Enter password: [%s] ", password);
57 fgets(temp, sizeof(temp), stdin);
59 if (temp[strlen(temp) - 1] == 0x0a) /* A new line? */
60 temp[strlen(temp) - 1] = 0x00;
62 if (temp[0]) strncpy(password, temp, pwmaxlen - 1);
66 int global_id = 0;
68 void print_list_fn(struct print_job_info *pji)
71 fprintf(stdout, "Print job: ID: %u, Prio: %u, Size: %u, User: %s, Name: %s\n",
72 pji->id, pji->priority, pji->size, pji->user, pji->name);
74 global_id = pji->id;
78 int main(int argc, char *argv[])
80 int err, fd, dh1, dh2, dh3, dsize, dirc;
81 const char *file = "smb://samba/public/testfile.txt";
82 const char *file2 = "smb://samba/public/testfile2.txt";
83 char buff[256];
84 char dirbuf[512];
85 char *dirp;
86 struct stat st1, st2;
88 err = smbc_init(auth_fn, 10); /* Initialize things */
90 if (err < 0) {
92 fprintf(stderr, "Initializing the smbclient library ...: %s\n", strerror(errno));
96 if (argc > 1) {
98 /* Try to list the print jobs ... */
100 if (smbc_list_print_jobs("smb://samba/pclp", print_list_fn) < 0) {
102 fprintf(stderr, "Could not list print jobs: %s, %d\n", strerror(errno), errno);
103 exit(1);
107 /* Try to delete the last job listed */
109 if (global_id > 0) {
111 fprintf(stdout, "Trying to delete print job %u\n", global_id);
113 if (smbc_unlink_print_job("smb://samba/pclp", global_id) < 0) {
115 fprintf(stderr, "Failed to unlink job id %u, %s, %u\n", global_id,
116 strerror(errno), errno);
118 exit(1);
124 /* Try to print a file ... */
126 if (smbc_print_file("smb://samba/public/testfile2.txt", "smb://samba/pclp") < 0) {
128 fprintf(stderr, "Failed to print job: %s %u\n", strerror(errno), errno);
129 exit(1);
133 /* Try to delete argv[1] as a file ... */
135 if (smbc_unlink(argv[1]) < 0) {
137 fprintf(stderr, "Could not unlink: %s, %s, %d\n",
138 argv[1], strerror(errno), errno);
140 exit(0);
144 if ((dh1 = smbc_opendir("smb://"))<1) {
146 fprintf(stderr, "Could not open directory: smb://: %s\n",
147 strerror(errno));
149 exit(1);
153 if ((dh2 = smbc_opendir("smb://sambanet")) < 0) {
155 fprintf(stderr, "Could not open directory: smb://sambanet: %s\n",
156 strerror(errno));
158 exit(1);
162 if ((dh3 = smbc_opendir("smb://samba")) < 0) {
164 fprintf(stderr, "Could not open directory: smb://samba: %s\n",
165 strerror(errno));
167 exit(1);
171 fprintf(stdout, "Directory handles: %u, %u, %u\n", dh1, dh2, dh3);
173 /* Now, list those directories, but in funny ways ... */
175 dirp = (char *)dirbuf;
177 if ((dirc = smbc_getdents(dh1, (struct smbc_dirent *)dirp,
178 sizeof(dirbuf))) < 0) {
180 fprintf(stderr, "Problems getting directory entries: %s\n",
181 strerror(errno));
183 exit(1);
187 /* Now, process the list of names ... */
189 fprintf(stdout, "Directory listing, size = %u\n", dirc);
191 while (dirc > 0) {
193 dsize = ((struct smbc_dirent *)dirp)->dirlen;
194 fprintf(stdout, "Dir Ent, Type: %u, Name: %s, Comment: %s\n",
195 ((struct smbc_dirent *)dirp)->smbc_type,
196 ((struct smbc_dirent *)dirp)->name,
197 ((struct smbc_dirent *)dirp)->comment);
199 dirp += dsize;
200 (char *)dirc -= dsize;
204 dirp = (char *)dirbuf;
206 if ((dirc = smbc_getdents(dh2, (struct smbc_dirent *)dirp,
207 sizeof(dirbuf))) < 0) {
209 fprintf(stderr, "Problems getting directory entries: %s\n",
210 strerror(errno));
212 exit(1);
216 /* Now, process the list of names ... */
218 fprintf(stdout, "\nDirectory listing, size = %u\n", dirc);
220 while (dirc > 0) {
222 dsize = ((struct smbc_dirent *)dirp)->dirlen;
223 fprintf(stdout, "Dir Ent, Type: %u, Name: %s, Comment: %s\n",
224 ((struct smbc_dirent *)dirp)->smbc_type,
225 ((struct smbc_dirent *)dirp)->name,
226 ((struct smbc_dirent *)dirp)->comment);
228 dirp += dsize;
229 (char *)dirc -= dsize;
233 dirp = (char *)dirbuf;
235 if ((dirc = smbc_getdents(dh3, (struct smbc_dirent *)dirp,
236 sizeof(dirbuf))) < 0) {
238 fprintf(stderr, "Problems getting directory entries: %s\n",
239 strerror(errno));
241 exit(1);
245 /* Now, process the list of names ... */
247 fprintf(stdout, "Directory listing, size = %u\n", dirc);
249 while (dirc > 0) {
251 dsize = ((struct smbc_dirent *)dirp)->dirlen;
252 fprintf(stdout, "\nDir Ent, Type: %u, Name: %s, Comment: %s\n",
253 ((struct smbc_dirent *)dirp)->smbc_type,
254 ((struct smbc_dirent *)dirp)->name,
255 ((struct smbc_dirent *)dirp)->comment);
257 (char *)dirp += dsize;
258 (char *)dirc -= dsize;
262 exit(1);
266 /* For now, open a file on a server that is hard coded ... later will
267 * read from the command line ...
270 fd = smbc_open(file, O_RDWR | O_CREAT | O_TRUNC, 0666);
272 if (fd < 0) {
274 fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
275 exit(0);
279 fprintf(stdout, "Opened or created file: %s\n", file);
281 /* Now, write some date to the file ... */
283 bzero(buff, sizeof(buff));
284 strcpy(buff, "Some test data for the moment ...");
286 err = smbc_write(fd, buff, sizeof(buff));
288 if (err < 0) {
290 fprintf(stderr, "writing file: %s: %s\n", file, strerror(errno));
291 exit(0);
295 fprintf(stdout, "Wrote %d bytes to file: %s\n", sizeof(buff), buff);
297 /* Now, seek the file back to offset 0 */
299 err = smbc_lseek(fd, SEEK_SET, 0);
301 if (err < 0) {
303 fprintf(stderr, "Seeking file: %s: %s\n", file, strerror(errno));
304 exit(0);
308 fprintf(stdout, "Completed lseek on file: %s\n", file);
310 /* Now, read the file contents back ... */
312 err = smbc_read(fd, buff, sizeof(buff));
314 if (err < 0) {
316 fprintf(stderr, "Reading file: %s: %s\n", file, strerror(errno));
317 exit(0);
321 fprintf(stdout, "Read file: %s\n", buff); /* Should check the contents */
323 fprintf(stdout, "Now fstat'ing file: %s\n", file);
325 err = smbc_fstat(fd, &st1);
327 if (err < 0) {
329 fprintf(stderr, "Fstat'ing file: %s: %s\n", file, strerror(errno));
330 exit(0);
335 /* Now, close the file ... */
337 err = smbc_close(fd);
339 if (err < 0) {
341 fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
345 /* Now, rename the file ... */
347 err = smbc_rename(file, file2);
349 if (err < 0) {
351 fprintf(stderr, "Renaming file: %s to %s: %s\n", file, file2, strerror(errno));
355 fprintf(stdout, "Renamed file %s to %s\n", file, file2);
357 /* Now, create a file and delete it ... */
359 fprintf(stdout, "Now, creating file: %s so we can delete it.\n", file);
361 fd = smbc_open(file, O_RDWR | O_CREAT, 0666);
363 if (fd < 0) {
365 fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
366 exit(0);
370 fprintf(stdout, "Opened or created file: %s\n", file);
372 err = smbc_close(fd);
374 if (err < 0) {
376 fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
377 exit(0);
381 /* Now, delete the file ... */
383 fprintf(stdout, "File %s created, now deleting ...\n", file);
385 err = smbc_unlink(file);
387 if (err < 0) {
389 fprintf(stderr, "Deleting file: %s: %s\n", file, strerror(errno));
390 exit(0);
394 /* Now, stat the file, file 2 ... */
396 fprintf(stdout, "Now stat'ing file: %s\n", file);
398 err = smbc_stat(file2, &st2);
400 if (err < 0) {
402 fprintf(stderr, "Stat'ing file: %s: %s\n", file, strerror(errno));
403 exit(0);
407 fprintf(stdout, "Stat'ed file: %s. Size = %d, mode = %04X\n", file2,
408 (int)st2.st_size, st2.st_mode);
409 fprintf(stdout, " time: %s\n", ctime(&st2.st_atime));
410 fprintf(stdout, "Earlier stat: %s, Size = %d, mode = %04X\n", file,
411 (int)st1.st_size, st1.st_mode);
412 fprintf(stdout, " time: %s\n", ctime(&st1.st_atime));
414 /* Now, make a directory ... */
416 fprintf(stdout, "Making directory smb://samba/public/make-dir\n");
418 if (smbc_mkdir("smb://samba/public/make-dir", 0666) < 0) {
420 fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n",
421 strerror(errno));
423 if (errno == EEXIST) { /* Try to delete the directory */
425 fprintf(stdout, "Trying to delete directory: smb://samba/public/make-dir\n");
427 if (smbc_rmdir("smb://samba/public/make-dir") < 0) { /* Error */
429 fprintf(stderr, "Error removing directory: smb://samba/public/make-dir: %s\n", strerror(errno));
431 exit(0);
435 fprintf(stdout, "Making directory: smb://samba/public/make-dir\n");
437 if (smbc_mkdir("smb://samba/public/make-dir", 666) < 0) {
439 fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n",
440 strerror(errno));
442 fprintf(stderr, "I give up!\n");
444 exit(1);
450 exit(0);
454 fprintf(stdout, "Made dir: make-dir\n");
455 return 0;