More const fixes. Remove CONST_DISCARD.
[samba.git] / source3 / utils / smbget.c
1 /*
2    smbget: a wget-like utility with support for recursive downloading and 
3         smb:// urls
4    Copyright (C) 2003-2004 Jelmer Vernooij <jelmer@samba.org>
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include "includes.h"
20 #include "system/filesys.h"
21 #include "popt_common.h"
22 #include "libsmbclient.h"
23
24 #if _FILE_OFFSET_BITS==64
25 #define OFF_T_FORMAT "%lld"
26 #define OFF_T_FORMAT_CAST long long
27 #else
28 #define OFF_T_FORMAT "%ld"
29 #define OFF_T_FORMAT_CAST long
30 #endif
31
32 static int columns = 0;
33
34 static int debuglevel, update;
35 static char *outputfile;
36
37
38 static time_t total_start_time = 0;
39 static off_t total_bytes = 0;
40
41 #define SMB_MAXPATHLEN MAXPATHLEN
42
43 /* Number of bytes to read when checking whether local and remote file are really the same file */
44 #define RESUME_CHECK_SIZE                               512
45 #define RESUME_DOWNLOAD_OFFSET                  1024
46 #define RESUME_CHECK_OFFSET                             RESUME_DOWNLOAD_OFFSET+RESUME_CHECK_SIZE
47 /* Number of bytes to read at once */
48 #define SMB_DEFAULT_BLOCKSIZE                                   64000
49
50 static const char *username = NULL, *password = NULL, *workgroup = NULL;
51 static int nonprompt = 0, quiet = 0, dots = 0, keep_permissions = 0, verbose = 0, send_stdout = 0;
52 static int blocksize = SMB_DEFAULT_BLOCKSIZE;
53
54 static int smb_download_file(const char *base, const char *name, int recursive,
55                              int resume, int toplevel, char *outfile);
56
57 static int get_num_cols(void)
58 {
59 #ifdef TIOCGWINSZ
60         struct winsize ws;
61         if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
62                 return 0;
63         }
64         return ws.ws_col;
65 #else
66 #warning No support for TIOCGWINSZ
67         char *cols = getenv("COLUMNS");
68         if(!cols) return 0;
69         return atoi(cols);
70 #endif
71 }
72
73 static void change_columns(int sig)
74 {
75         columns = get_num_cols();
76 }
77
78 static void human_readable(off_t s, char *buffer, int l)
79 {
80         if (s > 1024 * 1024 * 1024) {
81                 snprintf(buffer, l, "%.2fGB", 1.0 * s / (1024 * 1024 * 1024));
82         } else if (s > 1024 * 1024) {
83                 snprintf(buffer, l, "%.2fMB", 1.0 * s / (1024 * 1024));
84         } else if (s > 1024) {
85                 snprintf(buffer, l, "%.2fkB", 1.0 * s / 1024);
86         } else {
87                 snprintf(buffer, l, OFF_T_FORMAT"b", (OFF_T_FORMAT_CAST)s);
88         }
89 }
90
91 static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen, char *un, int unlen, char *pw, int pwlen)
92 {
93         static char hasasked = 0;
94         char *wgtmp, *usertmp;
95         char tmp[128];
96
97         if(hasasked) return;
98         hasasked = 1;
99
100         if(!nonprompt && !username) {
101                 printf("Username for %s at %s [guest] ", shr, srv);
102                 if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
103                         return;
104                 }
105                 if ((strlen(tmp) > 0) && (tmp[strlen(tmp)-1] == '\n')) {
106                         tmp[strlen(tmp)-1] = '\0';
107                 }
108                 strncpy(un, tmp, unlen-1);
109         } else if(username) strncpy(un, username, unlen-1);
110
111         if(!nonprompt && !password) {
112                 char *prompt, *pass;
113                 if (asprintf(&prompt, "Password for %s at %s: ", shr, srv) == -1) {
114                         return;
115                 }
116                 pass = getpass(prompt);
117                 free(prompt);
118                 strncpy(pw, pass, pwlen-1);
119         } else if(password) strncpy(pw, password, pwlen-1);
120
121         if(workgroup)strncpy(wg, workgroup, wglen-1);
122
123         wgtmp = SMB_STRNDUP(wg, wglen); 
124         usertmp = SMB_STRNDUP(un, unlen);
125         if(!quiet)printf("Using workgroup %s, %s%s\n", wgtmp, *usertmp?"user ":"guest user", usertmp);
126         free(wgtmp); free(usertmp);
127 }
128
129 /* Return 1 on error, 0 on success. */
130
131 static int smb_download_dir(const char *base, const char *name, int resume)
132 {
133         char path[SMB_MAXPATHLEN];
134         int dirhandle;
135         struct smbc_dirent *dirent;
136         const char *relname = name;
137         char *tmpname;
138         struct stat remotestat;
139         int ret = 0;
140
141         snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (base[0] && name[0] && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name);
142
143         /* List files in directory and call smb_download_file on them */
144         dirhandle = smbc_opendir(path);
145         if(dirhandle < 1) {
146                 if (errno == ENOTDIR) {
147                         return smb_download_file(base, name, 1, resume,
148                                                  0, NULL);
149                 }
150                 fprintf(stderr, "Can't open directory %s: %s\n", path, strerror(errno));
151                 return 1;
152         }
153
154         while(*relname == '/')relname++;
155         mkdir(relname, 0755);
156
157         tmpname = SMB_STRDUP(name);
158
159         while((dirent = smbc_readdir(dirhandle))) {
160                 char *newname;
161                 if(!strcmp(dirent->name, ".") || !strcmp(dirent->name, ".."))continue;
162                 if (asprintf(&newname, "%s/%s", tmpname, dirent->name) == -1) {
163                         return 1;
164                 }
165                 switch(dirent->smbc_type) {
166                 case SMBC_DIR:
167                         ret = smb_download_dir(base, newname, resume);
168                         break;
169
170                 case SMBC_WORKGROUP:
171                         ret = smb_download_dir("smb://", dirent->name, resume);
172                         break;
173
174                 case SMBC_SERVER:
175                         ret = smb_download_dir("smb://", dirent->name, resume);
176                         break;
177
178                 case SMBC_FILE:
179                         ret = smb_download_file(base, newname, 1, resume, 0,
180                                                 NULL);
181                         break;
182
183                 case SMBC_FILE_SHARE:
184                         ret = smb_download_dir(base, newname, resume);
185                         break;
186
187                 case SMBC_PRINTER_SHARE:
188                         if(!quiet)printf("Ignoring printer share %s\n", dirent->name);
189                         break;
190
191                 case SMBC_COMMS_SHARE:
192                         if(!quiet)printf("Ignoring comms share %s\n", dirent->name);
193                         break;
194
195                 case SMBC_IPC_SHARE:
196                         if(!quiet)printf("Ignoring ipc$ share %s\n", dirent->name);
197                         break;
198
199                 default:
200                         fprintf(stderr, "Ignoring file '%s' of type '%d'\n", newname, dirent->smbc_type);
201                         break;
202                 }
203                 free(newname);
204         }
205         free(tmpname);
206
207         if(keep_permissions) {
208                 if(smbc_fstat(dirhandle, &remotestat) < 0) {
209                         fprintf(stderr, "Unable to get stats on %s on remote server\n", path);
210                         smbc_closedir(dirhandle);
211                         return 1;
212                 }
213
214                 if(chmod(relname, remotestat.st_mode) < 0) {
215                         fprintf(stderr, "Unable to change mode of local dir %s to %o\n", relname,
216                                 (unsigned int)remotestat.st_mode);
217                         smbc_closedir(dirhandle);
218                         return 1;
219                 }
220         }
221
222         smbc_closedir(dirhandle);
223         return ret;
224 }
225
226 static char *print_time(long t)
227 {
228         static char buffer[100];
229         int secs, mins, hours;
230         if(t < -1) {
231                 strncpy(buffer, "Unknown", sizeof(buffer));
232                 return buffer;
233         }
234
235         secs = (int)t % 60;
236         mins = (int)t / 60 % 60;
237         hours = (int)t / (60 * 60);
238         snprintf(buffer, sizeof(buffer)-1, "%02d:%02d:%02d", hours, mins, secs);
239         return buffer;
240 }
241
242 static void print_progress(const char *name, time_t start, time_t now, off_t start_pos, off_t pos, off_t total)
243 {
244         double avg = 0.0;
245         long  eta = -1; 
246         double prcnt = 0.0;
247         char hpos[20], htotal[20], havg[20];
248         char *status, *filename;
249         int len;
250         if(now - start)avg = 1.0 * (pos - start_pos) / (now - start);
251         eta = (total - pos) / avg;
252         if(total)prcnt = 100.0 * pos / total;
253
254         human_readable(pos, hpos, sizeof(hpos));
255         human_readable(total, htotal, sizeof(htotal));
256         human_readable(avg, havg, sizeof(havg));
257
258         len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos, htotal, prcnt, havg, print_time(eta));
259         if (len == -1) {
260                 return;
261         }
262
263         if(columns) {
264                 int required = strlen(name), available = columns - len - strlen("[] ");
265                 if(required > available) {
266                         if (asprintf(&filename, "...%s", name + required - available + 3) == -1) {
267                                 return;
268                         }
269                 } else {
270                         filename = SMB_STRNDUP(name, available);
271                 }
272         } else filename = SMB_STRDUP(name);
273
274         fprintf(stderr, "\r[%s] %s", filename, status);
275
276         free(filename); free(status);
277 }
278
279 /* Return 1 on error, 0 on success. */
280
281 static int smb_download_file(const char *base, const char *name, int recursive,
282                              int resume, int toplevel, char *outfile)
283 {
284         int remotehandle, localhandle;
285         time_t start_time = time_mono(NULL);
286         const char *newpath;
287         char path[SMB_MAXPATHLEN];
288         char checkbuf[2][RESUME_CHECK_SIZE];
289         char *readbuf = NULL;
290         off_t offset_download = 0, offset_check = 0, curpos = 0, start_offset = 0;
291         struct stat localstat, remotestat;
292
293         snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (*base && *name && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name);
294
295         remotehandle = smbc_open(path, O_RDONLY, 0755);
296
297         if(remotehandle < 0) {
298                 switch(errno) {
299                 case EISDIR: 
300                         if(!recursive) {
301                                 fprintf(stderr, "%s is a directory. Specify -R to download recursively\n", path);
302                                 return 1;
303                         }
304                         return smb_download_dir(base, name, resume);
305
306                 case ENOENT:
307                         fprintf(stderr, "%s can't be found on the remote server\n", path);
308                         return 1;
309
310                 case ENOMEM:
311                         fprintf(stderr, "Not enough memory\n");
312                         return 1;
313
314                 case ENODEV:
315                         fprintf(stderr, "The share name used in %s does not exist\n", path);
316                         return 1;
317
318                 case EACCES:
319                         fprintf(stderr, "You don't have enough permissions to access %s\n", path);
320                         return 1;
321
322                 default:
323                         perror("smbc_open");
324                         return 1;
325                 }
326         } 
327
328         if(smbc_fstat(remotehandle, &remotestat) < 0) {
329                 fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
330                 return 1;
331         }
332
333         if(outfile) newpath = outfile;
334         else if(!name[0]) {
335                 newpath = strrchr(base, '/');
336                 if(newpath)newpath++; else newpath = base;
337         } else newpath = name;
338
339         if (!toplevel && (newpath[0] == '/')) {
340                 newpath++;
341         }
342
343         /* Open local file according to the mode */
344         if(update) {
345                 /* if it is up-to-date, skip */
346                 if(stat(newpath, &localstat) == 0 &&
347                                 localstat.st_mtime >= remotestat.st_mtime) {
348                         if(verbose)
349                                 printf("%s is up-to-date, skipping\n", newpath);
350                         smbc_close(remotehandle);
351                         return 0;
352                 }
353                 /* else open it for writing and truncate if it exists */
354                 localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | O_TRUNC, 0775);
355                 if(localhandle < 0) {
356                         fprintf(stderr, "Can't open %s : %s\n", newpath,
357                                         strerror(errno));
358                         smbc_close(remotehandle);
359                         return 1;
360                 }
361                 /* no offset */
362         } else if(!send_stdout) {
363                 localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | (!resume?O_EXCL:0), 0755);
364                 if(localhandle < 0) {
365                         fprintf(stderr, "Can't open %s: %s\n", newpath, strerror(errno));
366                         smbc_close(remotehandle);
367                         return 1;
368                 }
369
370                 if (fstat(localhandle, &localstat) != 0) {
371                         fprintf(stderr, "Can't fstat %s: %s\n", newpath, strerror(errno));
372                         smbc_close(remotehandle);
373                         close(localhandle);
374                         return 1;
375                 }
376
377                 start_offset = localstat.st_size;
378
379                 if(localstat.st_size && localstat.st_size == remotestat.st_size) {
380                         if(verbose)fprintf(stderr, "%s is already downloaded completely.\n", path);
381                         else if(!quiet)fprintf(stderr, "%s\n", path);
382                         smbc_close(remotehandle);
383                         close(localhandle);
384                         return 0;
385                 }
386
387                 if(localstat.st_size > RESUME_CHECK_OFFSET && remotestat.st_size > RESUME_CHECK_OFFSET) {
388                         offset_download = localstat.st_size - RESUME_DOWNLOAD_OFFSET;
389                         offset_check = localstat.st_size - RESUME_CHECK_OFFSET;
390                         if(verbose)printf("Trying to start resume of %s at "OFF_T_FORMAT"\n"
391                                    "At the moment "OFF_T_FORMAT" of "OFF_T_FORMAT" bytes have been retrieved\n",
392                                 newpath, (OFF_T_FORMAT_CAST)offset_check, 
393                                 (OFF_T_FORMAT_CAST)localstat.st_size,
394                                 (OFF_T_FORMAT_CAST)remotestat.st_size);
395                 }
396
397                 if(offset_check) { 
398                         off_t off1, off2;
399                         /* First, check all bytes from offset_check to offset_download */
400                         off1 = lseek(localhandle, offset_check, SEEK_SET);
401                         if(off1 < 0) {
402                                 fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in local file %s\n",
403                                         (OFF_T_FORMAT_CAST)offset_check, newpath);
404                                 smbc_close(remotehandle); close(localhandle);
405                                 return 1;
406                         }
407
408                         off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET); 
409                         if(off2 < 0) {
410                                 fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in remote file %s\n",
411                                         (OFF_T_FORMAT_CAST)offset_check, newpath);
412                                 smbc_close(remotehandle); close(localhandle);
413                                 return 1;
414                         }
415
416                         if(off1 != off2) {
417                                 fprintf(stderr, "Offset in local and remote files is different (local: "OFF_T_FORMAT", remote: "OFF_T_FORMAT")\n",
418                                         (OFF_T_FORMAT_CAST)off1,
419                                         (OFF_T_FORMAT_CAST)off2);
420                                 smbc_close(remotehandle); close(localhandle);
421                                 return 1;
422                         }
423
424                         if(smbc_read(remotehandle, checkbuf[0], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
425                                 fprintf(stderr, "Can't read %d bytes from remote file %s\n", RESUME_CHECK_SIZE, path);
426                                 smbc_close(remotehandle); close(localhandle);
427                                 return 1;
428                         }
429
430                         if(read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
431                                 fprintf(stderr, "Can't read %d bytes from local file %s\n", RESUME_CHECK_SIZE, name);
432                                 smbc_close(remotehandle); close(localhandle);
433                                 return 1;
434                         }
435
436                         if(memcmp(checkbuf[0], checkbuf[1], RESUME_CHECK_SIZE) == 0) {
437                                 if(verbose)printf("Current local and remote file appear to be the same. Starting download from offset "OFF_T_FORMAT"\n", (OFF_T_FORMAT_CAST)offset_download);
438                         } else {
439                                 fprintf(stderr, "Local and remote file appear to be different, not doing resume for %s\n", path);
440                                 smbc_close(remotehandle); close(localhandle);
441                                 return 1;
442                         }
443                 }
444         } else {
445                 localhandle = STDOUT_FILENO;
446                 start_offset = 0;
447                 offset_download = 0;
448                 offset_check = 0;
449         }
450
451         readbuf = (char *)SMB_MALLOC(blocksize);
452         if (!readbuf) {
453                 return 1;
454         }
455
456         /* Now, download all bytes from offset_download to the end */
457         for(curpos = offset_download; curpos < remotestat.st_size; curpos+=blocksize) {
458                 ssize_t bytesread = smbc_read(remotehandle, readbuf, blocksize);
459                 if(bytesread < 0) {
460                         fprintf(stderr, "Can't read %u bytes at offset "OFF_T_FORMAT", file %s\n", (unsigned int)blocksize, (OFF_T_FORMAT_CAST)curpos, path);
461                         smbc_close(remotehandle);
462                         if (localhandle != STDOUT_FILENO) close(localhandle);
463                         free(readbuf);
464                         return 1;
465                 }
466
467                 total_bytes += bytesread;
468
469                 if(write(localhandle, readbuf, bytesread) < 0) {
470                         fprintf(stderr, "Can't write %u bytes to local file %s at offset "OFF_T_FORMAT"\n", (unsigned int)bytesread, path, (OFF_T_FORMAT_CAST)curpos);
471                         free(readbuf);
472                         smbc_close(remotehandle);
473                         if (localhandle != STDOUT_FILENO) close(localhandle);
474                         return 1;
475                 }
476
477                 if(dots)fputc('.', stderr);
478                 else if(!quiet) {
479                         print_progress(newpath, start_time, time_mono(NULL),
480                                         start_offset, curpos, remotestat.st_size);
481                 }
482         }
483
484         free(readbuf);
485
486         if(dots){
487                 fputc('\n', stderr);
488                 printf("%s downloaded\n", path);
489         } else if(!quiet) {
490                 int i;
491                 fprintf(stderr, "\r%s", path);
492                 if(columns) {
493                         for(i = strlen(path); i < columns; i++) {
494                                 fputc(' ', stderr);
495                         }
496                 }
497                 fputc('\n', stderr);
498         }
499
500         if(keep_permissions && !send_stdout) {
501                 if(fchmod(localhandle, remotestat.st_mode) < 0) {
502                         fprintf(stderr, "Unable to change mode of local file %s to %o\n", path,
503                                 (unsigned int)remotestat.st_mode);
504                         smbc_close(remotehandle);
505                         close(localhandle);
506                         return 1;
507                 }
508         }
509
510         smbc_close(remotehandle);
511         if (localhandle != STDOUT_FILENO) close(localhandle);
512         return 0;
513 }
514
515 static void clean_exit(void)
516 {
517         char bs[100];
518         human_readable(total_bytes, bs, sizeof(bs));
519         if(!quiet)fprintf(stderr, "Downloaded %s in %lu seconds\n", bs,
520                 (unsigned long)(time_mono(NULL) - total_start_time));
521         exit(0);
522 }
523
524 static void signal_quit(int v)
525 {
526         clean_exit();
527 }
528
529 static int readrcfile(const char *name, const struct poptOption long_options[])
530 {
531         FILE *fd = fopen(name, "r");
532         int lineno = 0, i;
533         char var[101], val[101];
534         char found;
535         int *intdata; char **stringdata;
536         if(!fd) {
537                 fprintf(stderr, "Can't open RC file %s\n", name);
538                 return 1;
539         }
540
541         while(!feof(fd)) {
542                 lineno++;
543                 if(fscanf(fd, "%100s %100s\n", var, val) < 2) {
544                         fprintf(stderr, "Can't parse line %d of %s, ignoring.\n", lineno, name);
545                         continue;
546                 }
547
548                 found = 0;
549
550                 for(i = 0; long_options[i].shortName; i++) {
551                         if(!long_options[i].longName)continue;
552                         if(strcmp(long_options[i].longName, var)) continue;
553                         if(!long_options[i].arg)continue;
554
555                         switch(long_options[i].argInfo) {
556                         case POPT_ARG_NONE:
557                                 intdata = (int *)long_options[i].arg;
558                                 if(!strcmp(val, "on")) *intdata = 1;
559                                 else if(!strcmp(val, "off")) *intdata = 0;
560                                 else fprintf(stderr, "Illegal value %s for %s at line %d in %s\n", val, var, lineno, name);
561                                 break;
562                         case POPT_ARG_INT:
563                                 intdata = (int *)long_options[i].arg;
564                                 *intdata = atoi(val);
565                                 break;
566                         case POPT_ARG_STRING:
567                                 stringdata = (char **)long_options[i].arg;
568                                 *stringdata = SMB_STRDUP(val);
569                                 break;
570                         default:
571                                 fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name);
572                                 break;
573                         }
574
575                         found = 1;
576                 }
577                 if(!found) {
578                         fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name);
579                 }
580         }
581
582         fclose(fd);
583         return 0;
584 }
585
586 int main(int argc, const char **argv)
587 {
588         int c = 0;
589         const char *file = NULL;
590         char *rcfile = NULL;
591         bool smb_encrypt = false;
592         int resume = 0, recursive = 0;
593         TALLOC_CTX *frame = talloc_stackframe();
594         int ret = 0;
595         struct poptOption long_options[] = {
596                 {"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" },        
597                 {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" },      
598                 {"resume", 'r', POPT_ARG_NONE, &resume, 0, "Automatically resume aborted files" },
599                 {"update", 'U',  POPT_ARG_NONE, &update, 0, "Download only when remote file is newer than local file or local file is missing"},
600                 {"recursive", 'R',  POPT_ARG_NONE, &recursive, 0, "Recursively download files" },
601                 {"username", 'u', POPT_ARG_STRING, &username, 'u', "Username to use" },
602                 {"password", 'p', POPT_ARG_STRING, &password, 'p', "Password to use" },
603                 {"workgroup", 'w', POPT_ARG_STRING, &workgroup, 'w', "Workgroup to use (optional)" },
604                 {"nonprompt", 'n', POPT_ARG_NONE, &nonprompt, 'n', "Don't ask anything (non-interactive)" },
605                 {"debuglevel", 'd', POPT_ARG_INT, &debuglevel, 'd', "Debuglevel to use" },
606                 {"outputfile", 'o', POPT_ARG_STRING, &outputfile, 'o', "Write downloaded data to specified file" },
607                 {"stdout", 'O', POPT_ARG_NONE, &send_stdout, 'O', "Write data to stdout" },
608                 {"dots", 'D', POPT_ARG_NONE, &dots, 'D', "Show dots as progress indication" },
609                 {"quiet", 'q', POPT_ARG_NONE, &quiet, 'q', "Be quiet" },
610                 {"verbose", 'v', POPT_ARG_NONE, &verbose, 'v', "Be verbose" },
611                 {"keep-permissions", 'P', POPT_ARG_NONE, &keep_permissions, 'P', "Keep permissions" },
612                 {"blocksize", 'b', POPT_ARG_INT, &blocksize, 'b', "Change number of bytes in a block"},
613                 {"rcfile", 'f', POPT_ARG_STRING, NULL, 'f', "Use specified rc file"},
614                 POPT_AUTOHELP
615                 POPT_TABLEEND
616         };
617         poptContext pc;
618
619         load_case_tables();
620
621         /* only read rcfile if it exists */
622         if (asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME")) == -1) {
623                 return 1;
624         }
625         if(access(rcfile, F_OK) == 0) 
626                 readrcfile(rcfile, long_options);
627         free(rcfile);
628
629 #ifdef SIGWINCH
630         signal(SIGWINCH, change_columns);
631 #endif
632         signal(SIGINT, signal_quit);
633         signal(SIGTERM, signal_quit);
634
635         pc = poptGetContext(argv[0], argc, argv, long_options, 0);
636
637         while((c = poptGetNextOpt(pc)) >= 0) {
638                 switch(c) {
639                 case 'f':
640                         readrcfile(poptGetOptArg(pc), long_options);
641                         break;
642                 case 'a':
643                         username = ""; password = "";
644                         break;
645                 case 'e':
646                         smb_encrypt = true;
647                         break;
648                 }
649         }
650
651         if((send_stdout || resume || outputfile) && update) {
652                 fprintf(stderr, "The -o, -R or -O and -U options can not be used together.\n");
653                 return 1;
654         }
655         if((send_stdout || outputfile) && recursive) {
656                 fprintf(stderr, "The -o or -O and -R options can not be used together.\n");
657                 return 1;
658         }
659
660         if(outputfile && send_stdout) {
661                 fprintf(stderr, "The -o and -O options cannot be used together.\n");
662                 return 1;
663         }
664
665         if(smbc_init(get_auth_data, debuglevel) < 0) {
666                 fprintf(stderr, "Unable to initialize libsmbclient\n");
667                 return 1;
668         }
669
670         if (smb_encrypt) {
671                 SMBCCTX *smb_ctx = smbc_set_context(NULL);
672                 smbc_option_set(smb_ctx,
673                         discard_const_p(char, "smb_encrypt_level"),
674                         "require");
675         }
676
677         columns = get_num_cols();
678
679         total_start_time = time_mono(NULL);
680
681         while ( (file = poptGetArg(pc)) ) {
682                 if (!recursive) 
683                         ret = smb_download_file(file, "", recursive, resume,
684                                                 1, outputfile);
685                 else 
686                         ret = smb_download_dir(file, "", resume);
687         }
688
689         TALLOC_FREE(frame);
690         if ( ret == 0){
691                 clean_exit();
692         }
693         return ret;
694 }