This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[kai/samba.git] / examples / libsmbclient / testsmbc.c
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
8    
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.
13    
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.
18    
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.
22 */
23
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>
31
32 void auth_fn(const char *server, const char *share,
33              char *workgroup, int wgmaxlen, char *username, int unmaxlen,
34              char *password, int pwmaxlen)
35 {
36   char temp[128];
37
38   fprintf(stdout, "Need password for //%s/%s\n", server, share);
39
40   fprintf(stdout, "Enter workgroup: [%s] ", workgroup);
41   fgets(temp, sizeof(temp), stdin);
42
43   if (temp[strlen(temp) - 1] == 0x0a) /* A new line? */
44     temp[strlen(temp) - 1] = 0x00;
45
46   if (temp[0]) strncpy(workgroup, temp, wgmaxlen - 1);
47
48   fprintf(stdout, "Enter username: [%s] ", username);
49   fgets(temp, sizeof(temp), stdin);
50
51   if (temp[strlen(temp) - 1] == 0x0a) /* A new line? */
52     temp[strlen(temp) - 1] = 0x00;
53
54   if (temp[0]) strncpy(username, temp, unmaxlen - 1);
55
56   fprintf(stdout, "Enter password: [%s] ", password);
57   fgets(temp, sizeof(temp), stdin);
58
59   if (temp[strlen(temp) - 1] == 0x0a) /* A new line? */
60     temp[strlen(temp) - 1] = 0x00;
61
62   if (temp[0]) strncpy(password, temp, pwmaxlen - 1);
63
64 }
65
66 int global_id = 0;
67
68 void print_list_fn(struct print_job_info *pji)
69 {
70
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);
73
74   global_id = pji->id;
75
76 }
77
78 int main(int argc, char *argv[])
79 {
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;
87
88   err = smbc_init(auth_fn,  10); /* Initialize things */
89
90   if (err < 0) {
91
92     fprintf(stderr, "Initializing the smbclient library ...: %s\n", strerror(errno));
93
94   }
95
96   if (argc > 1) {
97
98     /* Try to list the print jobs ... */
99
100     if (smbc_list_print_jobs("smb://samba/pclp", print_list_fn) < 0) {
101
102       fprintf(stderr, "Could not list print jobs: %s, %d\n", strerror(errno), errno);
103       exit(1);
104
105     }
106
107     /* Try to delete the last job listed */
108
109     if (global_id > 0) {
110
111       fprintf(stdout, "Trying to delete print job %u\n", global_id);
112
113       if (smbc_unlink_print_job("smb://samba/pclp", global_id) < 0) {
114
115         fprintf(stderr, "Failed to unlink job id %u, %s, %u\n", global_id, 
116                 strerror(errno), errno);
117
118         exit(1);
119
120       }
121
122     }
123
124     /* Try to print a file ... */
125
126     if (smbc_print_file("smb://samba/public/testfile2.txt", "smb://samba/pclp") < 0) {
127
128       fprintf(stderr, "Failed to print job: %s %u\n", strerror(errno), errno);
129       exit(1);
130
131     }
132
133     /* Try to delete argv[1] as a file ... */
134     
135     if (smbc_unlink(argv[1]) < 0) {
136
137       fprintf(stderr, "Could not unlink: %s, %s, %d\n",
138               argv[1], strerror(errno), errno);
139
140       exit(0);
141
142     }
143
144     if ((dh1 = smbc_opendir("smb://"))<1) {
145
146       fprintf(stderr, "Could not open directory: smb://: %s\n",
147               strerror(errno));
148
149       exit(1);
150
151     }
152
153     if ((dh2 = smbc_opendir("smb://sambanet")) < 0) {
154
155       fprintf(stderr, "Could not open directory: smb://sambanet: %s\n",
156               strerror(errno));
157
158       exit(1);
159
160     }
161
162     if ((dh3 = smbc_opendir("smb://samba")) < 0) {
163
164       fprintf(stderr, "Could not open directory: smb://samba: %s\n",
165               strerror(errno));
166
167       exit(1);
168
169     }
170
171     fprintf(stdout, "Directory handles: %u, %u, %u\n", dh1, dh2, dh3);
172
173     /* Now, list those directories, but in funny ways ... */
174
175     dirp = (char *)dirbuf;
176
177     if ((dirc = smbc_getdents(dh1, (struct smbc_dirent *)dirp, 
178                               sizeof(dirbuf))) < 0) {
179
180       fprintf(stderr, "Problems getting directory entries: %s\n",
181               strerror(errno));
182
183       exit(1);
184
185     }
186
187     /* Now, process the list of names ... */
188
189     fprintf(stdout, "Directory listing, size = %u\n", dirc);
190
191     while (dirc > 0) {
192
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);
198
199       dirp += dsize;
200       (char *)dirc -= dsize;
201
202     }
203
204     dirp = (char *)dirbuf;
205
206     if ((dirc = smbc_getdents(dh2, (struct smbc_dirent *)dirp, 
207                               sizeof(dirbuf))) < 0) {
208
209       fprintf(stderr, "Problems getting directory entries: %s\n",
210               strerror(errno));
211
212       exit(1);
213
214     }
215
216     /* Now, process the list of names ... */
217
218     fprintf(stdout, "\nDirectory listing, size = %u\n", dirc);
219
220     while (dirc > 0) {
221
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);
227
228       dirp += dsize;
229       (char *)dirc -= dsize;
230
231     }
232
233     dirp = (char *)dirbuf;
234
235     if ((dirc = smbc_getdents(dh3, (struct smbc_dirent *)dirp, 
236                               sizeof(dirbuf))) < 0) {
237
238       fprintf(stderr, "Problems getting directory entries: %s\n",
239               strerror(errno));
240
241       exit(1);
242
243     }
244
245     /* Now, process the list of names ... */
246
247     fprintf(stdout, "Directory listing, size = %u\n", dirc);
248
249     while (dirc > 0) {
250
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);
256
257       (char *)dirp += dsize;
258       (char *)dirc -= dsize;
259
260     }
261
262     exit(1);
263
264   }
265
266   /* For now, open a file on a server that is hard coded ... later will
267    * read from the command line ...
268    */
269
270   fd = smbc_open(file, O_RDWR | O_CREAT | O_TRUNC, 0666);
271
272   if (fd < 0) {
273
274     fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
275     exit(0);
276
277   }
278
279   fprintf(stdout, "Opened or created file: %s\n", file);
280
281   /* Now, write some date to the file ... */
282
283   bzero(buff, sizeof(buff));
284   strcpy(buff, "Some test data for the moment ...");
285
286   err = smbc_write(fd, buff, sizeof(buff));
287
288   if (err < 0) {
289     
290     fprintf(stderr, "writing file: %s: %s\n", file, strerror(errno));
291     exit(0);
292
293   }
294
295   fprintf(stdout, "Wrote %d bytes to file: %s\n", sizeof(buff), buff);
296
297   /* Now, seek the file back to offset 0 */
298
299   err = smbc_lseek(fd, SEEK_SET, 0);
300
301   if (err < 0) {
302
303     fprintf(stderr, "Seeking file: %s: %s\n", file, strerror(errno));
304     exit(0);
305
306   }
307
308   fprintf(stdout, "Completed lseek on file: %s\n", file);
309
310   /* Now, read the file contents back ... */
311
312   err = smbc_read(fd, buff, sizeof(buff));
313
314   if (err < 0) {
315
316     fprintf(stderr, "Reading file: %s: %s\n", file, strerror(errno));
317     exit(0);
318
319   }
320
321   fprintf(stdout, "Read file: %s\n", buff);  /* Should check the contents */
322
323   fprintf(stdout, "Now fstat'ing file: %s\n", file);
324
325   err = smbc_fstat(fd, &st1);
326
327   if (err < 0) {
328
329     fprintf(stderr, "Fstat'ing file: %s: %s\n", file, strerror(errno));
330     exit(0);
331
332   }
333
334
335   /* Now, close the file ... */
336
337   err = smbc_close(fd);
338
339   if (err < 0) {
340
341     fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
342
343   }
344
345   /* Now, rename the file ... */
346
347   err = smbc_rename(file, file2);
348
349   if (err < 0) {
350
351     fprintf(stderr, "Renaming file: %s to %s: %s\n", file, file2, strerror(errno));
352
353   }
354
355   fprintf(stdout, "Renamed file %s to %s\n", file, file2);
356
357   /* Now, create a file and delete it ... */
358
359   fprintf(stdout, "Now, creating file: %s so we can delete it.\n", file);
360
361   fd = smbc_open(file, O_RDWR | O_CREAT, 0666);
362
363   if (fd < 0) {
364
365     fprintf(stderr, "Creating file: %s: %s\n", file, strerror(errno));
366     exit(0);
367
368   }
369
370   fprintf(stdout, "Opened or created file: %s\n", file);
371
372   err = smbc_close(fd);
373
374   if (err < 0) {
375
376     fprintf(stderr, "Closing file: %s: %s\n", file, strerror(errno));
377     exit(0);
378
379   }
380   
381   /* Now, delete the file ... */
382
383   fprintf(stdout, "File %s created, now deleting ...\n", file);
384
385   err = smbc_unlink(file);
386
387   if (err < 0) {
388
389     fprintf(stderr, "Deleting file: %s: %s\n", file, strerror(errno));
390     exit(0);
391
392   }
393
394   /* Now, stat the file, file 2 ... */
395
396   fprintf(stdout, "Now stat'ing file: %s\n", file);
397
398   err = smbc_stat(file2, &st2);
399
400   if (err < 0) {
401
402     fprintf(stderr, "Stat'ing file: %s: %s\n", file, strerror(errno));
403     exit(0);
404
405   }
406
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));
413
414   /* Now, make a directory ... */
415
416   fprintf(stdout, "Making directory smb://samba/public/make-dir\n");
417
418   if (smbc_mkdir("smb://samba/public/make-dir", 0666) < 0) {
419
420     fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n", 
421             strerror(errno));
422
423     if (errno == EEXIST) { /* Try to delete the directory */
424
425       fprintf(stdout, "Trying to delete directory: smb://samba/public/make-dir\n");
426
427       if (smbc_rmdir("smb://samba/public/make-dir") < 0) { /* Error */
428
429         fprintf(stderr, "Error removing directory: smb://samba/public/make-dir: %s\n", strerror(errno));
430
431         exit(0);
432
433       }
434
435       fprintf(stdout, "Making directory: smb://samba/public/make-dir\n");
436
437       if (smbc_mkdir("smb://samba/public/make-dir", 666) < 0) {
438
439         fprintf(stderr, "Error making directory: smb://samba/public/make-dir: %s\n",
440                 strerror(errno));
441
442         fprintf(stderr, "I give up!\n");
443
444         exit(1);
445
446       }
447
448     }
449
450     exit(0);
451     
452   }
453
454   fprintf(stdout, "Made dir: make-dir\n");
455   return 0;
456 }