2 Unix SMB/Netbios implementation.
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.
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
)
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);
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
);
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";
88 err
= smbc_init(auth_fn
, 10); /* Initialize things */
92 fprintf(stderr
, "Initializing the smbclient library ...: %s\n", strerror(errno
));
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
);
107 /* Try to delete the last job listed */
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
);
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
);
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
);
144 if ((dh1
= smbc_opendir("smb://"))<1) {
146 fprintf(stderr
, "Could not open directory: smb://: %s\n",
153 if ((dh2
= smbc_opendir("smb://sambanet")) < 0) {
155 fprintf(stderr
, "Could not open directory: smb://sambanet: %s\n",
162 if ((dh3
= smbc_opendir("smb://samba")) < 0) {
164 fprintf(stderr
, "Could not open directory: smb://samba: %s\n",
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",
187 /* Now, process the list of names ... */
189 fprintf(stdout
, "Directory listing, size = %u\n", dirc
);
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
);
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",
216 /* Now, process the list of names ... */
218 fprintf(stdout
, "\nDirectory listing, size = %u\n", dirc
);
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
);
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",
245 /* Now, process the list of names ... */
247 fprintf(stdout
, "Directory listing, size = %u\n", dirc
);
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
;
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);
274 fprintf(stderr
, "Creating file: %s: %s\n", file
, strerror(errno
));
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
));
290 fprintf(stderr
, "writing file: %s: %s\n", file
, strerror(errno
));
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);
303 fprintf(stderr
, "Seeking file: %s: %s\n", file
, strerror(errno
));
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
));
316 fprintf(stderr
, "Reading file: %s: %s\n", file
, strerror(errno
));
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
);
329 fprintf(stderr
, "Fstat'ing file: %s: %s\n", file
, strerror(errno
));
335 /* Now, close the file ... */
337 err
= smbc_close(fd
);
341 fprintf(stderr
, "Closing file: %s: %s\n", file
, strerror(errno
));
345 /* Now, rename the file ... */
347 err
= smbc_rename(file
, file2
);
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);
365 fprintf(stderr
, "Creating file: %s: %s\n", file
, strerror(errno
));
370 fprintf(stdout
, "Opened or created file: %s\n", file
);
372 err
= smbc_close(fd
);
376 fprintf(stderr
, "Closing file: %s: %s\n", file
, strerror(errno
));
381 /* Now, delete the file ... */
383 fprintf(stdout
, "File %s created, now deleting ...\n", file
);
385 err
= smbc_unlink(file
);
389 fprintf(stderr
, "Deleting file: %s: %s\n", file
, strerror(errno
));
394 /* Now, stat the file, file 2 ... */
396 fprintf(stdout
, "Now stat'ing file: %s\n", file
);
398 err
= smbc_stat(file2
, &st2
);
402 fprintf(stderr
, "Stat'ing file: %s: %s\n", file
, strerror(errno
));
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",
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
));
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",
442 fprintf(stderr
, "I give up!\n");
454 fprintf(stdout
, "Made dir: make-dir\n");