WHATSNEW: Update changes.
[Samba.git] / source3 / utils / smbget.c
blob8b88a0fef30d9239bf5a442fb3a9cd56f727f4fa
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>
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.
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.
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/>. */
19 #include "includes.h"
20 #include "libsmbclient.h"
22 #if _FILE_OFFSET_BITS==64
23 #define OFF_T_FORMAT "%lld"
24 #define OFF_T_FORMAT_CAST long long
25 #else
26 #define OFF_T_FORMAT "%ld"
27 #define OFF_T_FORMAT_CAST long
28 #endif
30 static int columns = 0;
32 static int debuglevel, update;
33 static char *outputfile;
36 static time_t total_start_time = 0;
37 static off_t total_bytes = 0;
39 #define SMB_MAXPATHLEN MAXPATHLEN
41 /* Number of bytes to read when checking whether local and remote file are really the same file */
42 #define RESUME_CHECK_SIZE 512
43 #define RESUME_DOWNLOAD_OFFSET 1024
44 #define RESUME_CHECK_OFFSET RESUME_DOWNLOAD_OFFSET+RESUME_CHECK_SIZE
45 /* Number of bytes to read at once */
46 #define SMB_DEFAULT_BLOCKSIZE 64000
48 static const char *username = NULL, *password = NULL, *workgroup = NULL;
49 static int nonprompt = 0, quiet = 0, dots = 0, keep_permissions = 0, verbose = 0, send_stdout = 0;
50 static int blocksize = SMB_DEFAULT_BLOCKSIZE;
52 static int smb_download_file(const char *base, const char *name, int recursive,
53 int resume, int toplevel, char *outfile);
55 static int get_num_cols(void)
57 #ifdef TIOCGWINSZ
58 struct winsize ws;
59 if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
60 return 0;
62 return ws.ws_col;
63 #else
64 #warning No support for TIOCGWINSZ
65 char *cols = getenv("COLUMNS");
66 if(!cols) return 0;
67 return atoi(cols);
68 #endif
71 static void change_columns(int sig)
73 columns = get_num_cols();
76 static void human_readable(off_t s, char *buffer, int l)
78 if(s > 1024 * 1024 * 1024) snprintf(buffer, l, "%.2fGb", 1.0 * s / (1024 * 1024 * 1024));
79 else if(s > 1024 * 1024) snprintf(buffer, l, "%.2fMb", 1.0 * s / (1024 * 1024));
80 else if(s > 1024) snprintf(buffer, l, "%.2fkb", 1.0 * s / 1024);
81 else snprintf(buffer, l, OFF_T_FORMAT"b", (OFF_T_FORMAT_CAST)s);
84 static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen, char *un, int unlen, char *pw, int pwlen)
86 static char hasasked = 0;
87 char *wgtmp, *usertmp;
88 char tmp[128];
90 if(hasasked) return;
91 hasasked = 1;
93 if(!nonprompt && !username) {
94 printf("Username for %s at %s [guest] ", shr, srv);
95 if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
96 return;
98 if ((strlen(tmp) > 0) && (tmp[strlen(tmp)-1] == '\n')) {
99 tmp[strlen(tmp)-1] = '\0';
101 strncpy(un, tmp, unlen-1);
102 } else if(username) strncpy(un, username, unlen-1);
104 if(!nonprompt && !password) {
105 char *prompt, *pass;
106 if (asprintf(&prompt, "Password for %s at %s: ", shr, srv) == -1) {
107 return;
109 pass = getpass(prompt);
110 free(prompt);
111 strncpy(pw, pass, pwlen-1);
112 } else if(password) strncpy(pw, password, pwlen-1);
114 if(workgroup)strncpy(wg, workgroup, wglen-1);
116 wgtmp = SMB_STRNDUP(wg, wglen);
117 usertmp = SMB_STRNDUP(un, unlen);
118 if(!quiet)printf("Using workgroup %s, %s%s\n", wgtmp, *usertmp?"user ":"guest user", usertmp);
119 free(wgtmp); free(usertmp);
122 /* Return 1 on error, 0 on success. */
124 static int smb_download_dir(const char *base, const char *name, int resume)
126 char path[SMB_MAXPATHLEN];
127 int dirhandle;
128 struct smbc_dirent *dirent;
129 const char *relname = name;
130 char *tmpname;
131 struct stat remotestat;
132 int ret = 0;
134 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (base[0] && name[0] && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name);
136 /* List files in directory and call smb_download_file on them */
137 dirhandle = smbc_opendir(path);
138 if(dirhandle < 1) {
139 if (errno == ENOTDIR) {
140 return smb_download_file(base, name, 1, resume,
141 0, NULL);
143 fprintf(stderr, "Can't open directory %s: %s\n", path, strerror(errno));
144 return 1;
147 while(*relname == '/')relname++;
148 mkdir(relname, 0755);
150 tmpname = SMB_STRDUP(name);
152 while((dirent = smbc_readdir(dirhandle))) {
153 char *newname;
154 if(!strcmp(dirent->name, ".") || !strcmp(dirent->name, ".."))continue;
155 if (asprintf(&newname, "%s/%s", tmpname, dirent->name) == -1) {
156 return 1;
158 switch(dirent->smbc_type) {
159 case SMBC_DIR:
160 ret = smb_download_dir(base, newname, resume);
161 break;
163 case SMBC_WORKGROUP:
164 ret = smb_download_dir("smb://", dirent->name, resume);
165 break;
167 case SMBC_SERVER:
168 ret = smb_download_dir("smb://", dirent->name, resume);
169 break;
171 case SMBC_FILE:
172 ret = smb_download_file(base, newname, 1, resume, 0,
173 NULL);
174 break;
176 case SMBC_FILE_SHARE:
177 ret = smb_download_dir(base, newname, resume);
178 break;
180 case SMBC_PRINTER_SHARE:
181 if(!quiet)printf("Ignoring printer share %s\n", dirent->name);
182 break;
184 case SMBC_COMMS_SHARE:
185 if(!quiet)printf("Ignoring comms share %s\n", dirent->name);
186 break;
188 case SMBC_IPC_SHARE:
189 if(!quiet)printf("Ignoring ipc$ share %s\n", dirent->name);
190 break;
192 default:
193 fprintf(stderr, "Ignoring file '%s' of type '%d'\n", newname, dirent->smbc_type);
194 break;
196 free(newname);
198 free(tmpname);
200 if(keep_permissions) {
201 if(smbc_fstat(dirhandle, &remotestat) < 0) {
202 fprintf(stderr, "Unable to get stats on %s on remote server\n", path);
203 smbc_closedir(dirhandle);
204 return 1;
207 if(chmod(relname, remotestat.st_mode) < 0) {
208 fprintf(stderr, "Unable to change mode of local dir %s to %o\n", relname,
209 (unsigned int)remotestat.st_mode);
210 smbc_closedir(dirhandle);
211 return 1;
215 smbc_closedir(dirhandle);
216 return ret;
219 static char *print_time(long t)
221 static char buffer[100];
222 int secs, mins, hours;
223 if(t < -1) {
224 strncpy(buffer, "Unknown", sizeof(buffer));
225 return buffer;
228 secs = (int)t % 60;
229 mins = (int)t / 60 % 60;
230 hours = (int)t / (60 * 60);
231 snprintf(buffer, sizeof(buffer)-1, "%02d:%02d:%02d", hours, mins, secs);
232 return buffer;
235 static void print_progress(const char *name, time_t start, time_t now, off_t start_pos, off_t pos, off_t total)
237 double avg = 0.0;
238 long eta = -1;
239 double prcnt = 0.0;
240 char hpos[20], htotal[20], havg[20];
241 char *status, *filename;
242 int len;
243 if(now - start)avg = 1.0 * (pos - start_pos) / (now - start);
244 eta = (total - pos) / avg;
245 if(total)prcnt = 100.0 * pos / total;
247 human_readable(pos, hpos, sizeof(hpos));
248 human_readable(total, htotal, sizeof(htotal));
249 human_readable(avg, havg, sizeof(havg));
251 len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos, htotal, prcnt, havg, print_time(eta));
252 if (len == -1) {
253 return;
256 if(columns) {
257 int required = strlen(name), available = columns - len - strlen("[] ");
258 if(required > available) {
259 if (asprintf(&filename, "...%s", name + required - available + 3) == -1) {
260 return;
262 } else {
263 filename = SMB_STRNDUP(name, available);
265 } else filename = SMB_STRDUP(name);
267 fprintf(stderr, "\r[%s] %s", filename, status);
269 free(filename); free(status);
272 /* Return 1 on error, 0 on success. */
274 static int smb_download_file(const char *base, const char *name, int recursive,
275 int resume, int toplevel, char *outfile)
277 int remotehandle, localhandle;
278 time_t start_time = time(NULL);
279 const char *newpath;
280 char path[SMB_MAXPATHLEN];
281 char checkbuf[2][RESUME_CHECK_SIZE];
282 char *readbuf = NULL;
283 off_t offset_download = 0, offset_check = 0, curpos = 0, start_offset = 0;
284 struct stat localstat, remotestat;
286 snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (*base && *name && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name);
288 remotehandle = smbc_open(path, O_RDONLY, 0755);
290 if(remotehandle < 0) {
291 switch(errno) {
292 case EISDIR:
293 if(!recursive) {
294 fprintf(stderr, "%s is a directory. Specify -R to download recursively\n", path);
295 return 1;
297 return smb_download_dir(base, name, resume);
299 case ENOENT:
300 fprintf(stderr, "%s can't be found on the remote server\n", path);
301 return 1;
303 case ENOMEM:
304 fprintf(stderr, "Not enough memory\n");
305 return 1;
307 case ENODEV:
308 fprintf(stderr, "The share name used in %s does not exist\n", path);
309 return 1;
311 case EACCES:
312 fprintf(stderr, "You don't have enough permissions to access %s\n", path);
313 return 1;
315 default:
316 perror("smbc_open");
317 return 1;
321 if(smbc_fstat(remotehandle, &remotestat) < 0) {
322 fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
323 return 1;
326 if(outfile) newpath = outfile;
327 else if(!name[0]) {
328 newpath = strrchr(base, '/');
329 if(newpath)newpath++; else newpath = base;
330 } else newpath = name;
332 if (!toplevel && (newpath[0] == '/')) {
333 newpath++;
336 /* Open local file according to the mode */
337 if(update) {
338 /* if it is up-to-date, skip */
339 if(stat(newpath, &localstat) == 0 &&
340 localstat.st_mtime >= remotestat.st_mtime) {
341 if(verbose)
342 printf("%s is up-to-date, skipping\n", newpath);
343 smbc_close(remotehandle);
344 return 0;
346 /* else open it for writing and truncate if it exists */
347 localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | O_TRUNC, 0775);
348 if(localhandle < 0) {
349 fprintf(stderr, "Can't open %s : %s\n", newpath,
350 strerror(errno));
351 smbc_close(remotehandle);
352 return 1;
354 /* no offset */
355 } else if(!send_stdout) {
356 localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | (!resume?O_EXCL:0), 0755);
357 if(localhandle < 0) {
358 fprintf(stderr, "Can't open %s: %s\n", newpath, strerror(errno));
359 smbc_close(remotehandle);
360 return 1;
363 if (fstat(localhandle, &localstat) != 0) {
364 fprintf(stderr, "Can't fstat %s: %s\n", newpath, strerror(errno));
365 smbc_close(remotehandle);
366 close(localhandle);
367 return 1;
370 start_offset = localstat.st_size;
372 if(localstat.st_size && localstat.st_size == remotestat.st_size) {
373 if(verbose)fprintf(stderr, "%s is already downloaded completely.\n", path);
374 else if(!quiet)fprintf(stderr, "%s\n", path);
375 smbc_close(remotehandle);
376 close(localhandle);
377 return 0;
380 if(localstat.st_size > RESUME_CHECK_OFFSET && remotestat.st_size > RESUME_CHECK_OFFSET) {
381 offset_download = localstat.st_size - RESUME_DOWNLOAD_OFFSET;
382 offset_check = localstat.st_size - RESUME_CHECK_OFFSET;
383 if(verbose)printf("Trying to start resume of %s at "OFF_T_FORMAT"\n"
384 "At the moment "OFF_T_FORMAT" of "OFF_T_FORMAT" bytes have been retrieved\n",
385 newpath, (OFF_T_FORMAT_CAST)offset_check,
386 (OFF_T_FORMAT_CAST)localstat.st_size,
387 (OFF_T_FORMAT_CAST)remotestat.st_size);
390 if(offset_check) {
391 off_t off1, off2;
392 /* First, check all bytes from offset_check to offset_download */
393 off1 = lseek(localhandle, offset_check, SEEK_SET);
394 if(off1 < 0) {
395 fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in local file %s\n",
396 (OFF_T_FORMAT_CAST)offset_check, newpath);
397 smbc_close(remotehandle); close(localhandle);
398 return 1;
401 off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET);
402 if(off2 < 0) {
403 fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in remote file %s\n",
404 (OFF_T_FORMAT_CAST)offset_check, newpath);
405 smbc_close(remotehandle); close(localhandle);
406 return 1;
409 if(off1 != off2) {
410 fprintf(stderr, "Offset in local and remote files is different (local: "OFF_T_FORMAT", remote: "OFF_T_FORMAT")\n",
411 (OFF_T_FORMAT_CAST)off1,
412 (OFF_T_FORMAT_CAST)off2);
413 return 1;
416 if(smbc_read(remotehandle, checkbuf[0], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
417 fprintf(stderr, "Can't read %d bytes from remote file %s\n", RESUME_CHECK_SIZE, path);
418 smbc_close(remotehandle); close(localhandle);
419 return 1;
422 if(read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
423 fprintf(stderr, "Can't read %d bytes from local file %s\n", RESUME_CHECK_SIZE, name);
424 smbc_close(remotehandle); close(localhandle);
425 return 1;
428 if(memcmp(checkbuf[0], checkbuf[1], RESUME_CHECK_SIZE) == 0) {
429 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);
430 } else {
431 fprintf(stderr, "Local and remote file appear to be different, not doing resume for %s\n", path);
432 smbc_close(remotehandle); close(localhandle);
433 return 1;
436 } else {
437 localhandle = STDOUT_FILENO;
438 start_offset = 0;
439 offset_download = 0;
440 offset_check = 0;
443 readbuf = (char *)SMB_MALLOC(blocksize);
445 /* Now, download all bytes from offset_download to the end */
446 for(curpos = offset_download; curpos < remotestat.st_size; curpos+=blocksize) {
447 ssize_t bytesread = smbc_read(remotehandle, readbuf, blocksize);
448 if(bytesread < 0) {
449 fprintf(stderr, "Can't read %u bytes at offset "OFF_T_FORMAT", file %s\n", (unsigned int)blocksize, (OFF_T_FORMAT_CAST)curpos, path);
450 smbc_close(remotehandle);
451 if (localhandle != STDOUT_FILENO) close(localhandle);
452 free(readbuf);
453 return 1;
456 total_bytes += bytesread;
458 if(write(localhandle, readbuf, bytesread) < 0) {
459 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);
460 free(readbuf);
461 smbc_close(remotehandle);
462 if (localhandle != STDOUT_FILENO) close(localhandle);
463 return 1;
466 if(dots)fputc('.', stderr);
467 else if(!quiet) {
468 print_progress(newpath, start_time, time(NULL), start_offset, curpos, remotestat.st_size);
472 free(readbuf);
474 if(dots){
475 fputc('\n', stderr);
476 printf("%s downloaded\n", path);
477 } else if(!quiet) {
478 int i;
479 fprintf(stderr, "\r%s", path);
480 if(columns) {
481 for(i = strlen(path); i < columns; i++) {
482 fputc(' ', stderr);
485 fputc('\n', stderr);
488 if(keep_permissions && !send_stdout) {
489 if(fchmod(localhandle, remotestat.st_mode) < 0) {
490 fprintf(stderr, "Unable to change mode of local file %s to %o\n", path,
491 (unsigned int)remotestat.st_mode);
492 smbc_close(remotehandle);
493 close(localhandle);
494 return 1;
498 smbc_close(remotehandle);
499 if (localhandle != STDOUT_FILENO) close(localhandle);
500 return 0;
503 static void clean_exit(void)
505 char bs[100];
506 human_readable(total_bytes, bs, sizeof(bs));
507 if(!quiet)fprintf(stderr, "Downloaded %s in %lu seconds\n", bs,
508 (unsigned long)(time(NULL) - total_start_time));
509 exit(0);
512 static void signal_quit(int v)
514 clean_exit();
517 static int readrcfile(const char *name, const struct poptOption long_options[])
519 FILE *fd = fopen(name, "r");
520 int lineno = 0, i;
521 char var[101], val[101];
522 char found;
523 int *intdata; char **stringdata;
524 if(!fd) {
525 fprintf(stderr, "Can't open RC file %s\n", name);
526 return 1;
529 while(!feof(fd)) {
530 lineno++;
531 if(fscanf(fd, "%100s %100s\n", var, val) < 2) {
532 fprintf(stderr, "Can't parse line %d of %s, ignoring.\n", lineno, name);
533 continue;
536 found = 0;
538 for(i = 0; long_options[i].shortName; i++) {
539 if(!long_options[i].longName)continue;
540 if(strcmp(long_options[i].longName, var)) continue;
541 if(!long_options[i].arg)continue;
543 switch(long_options[i].argInfo) {
544 case POPT_ARG_NONE:
545 intdata = (int *)long_options[i].arg;
546 if(!strcmp(val, "on")) *intdata = 1;
547 else if(!strcmp(val, "off")) *intdata = 0;
548 else fprintf(stderr, "Illegal value %s for %s at line %d in %s\n", val, var, lineno, name);
549 break;
550 case POPT_ARG_INT:
551 intdata = (int *)long_options[i].arg;
552 *intdata = atoi(val);
553 break;
554 case POPT_ARG_STRING:
555 stringdata = (char **)long_options[i].arg;
556 *stringdata = SMB_STRDUP(val);
557 break;
558 default:
559 fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name);
560 break;
563 found = 1;
565 if(!found) {
566 fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name);
570 fclose(fd);
571 return 0;
574 int main(int argc, const char **argv)
576 int c = 0;
577 const char *file = NULL;
578 char *rcfile = NULL;
579 bool smb_encrypt = false;
580 int resume = 0, recursive = 0;
581 TALLOC_CTX *frame = talloc_stackframe();
582 int ret = 0;
583 struct poptOption long_options[] = {
584 {"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" },
585 {"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" },
586 {"resume", 'r', POPT_ARG_NONE, &resume, 0, "Automatically resume aborted files" },
587 {"update", 'U', POPT_ARG_NONE, &update, 0, "Download only when remote file is newer than local file or local file is missing"},
588 {"recursive", 'R', POPT_ARG_NONE, &recursive, 0, "Recursively download files" },
589 {"username", 'u', POPT_ARG_STRING, &username, 'u', "Username to use" },
590 {"password", 'p', POPT_ARG_STRING, &password, 'p', "Password to use" },
591 {"workgroup", 'w', POPT_ARG_STRING, &workgroup, 'w', "Workgroup to use (optional)" },
592 {"nonprompt", 'n', POPT_ARG_NONE, &nonprompt, 'n', "Don't ask anything (non-interactive)" },
593 {"debuglevel", 'd', POPT_ARG_INT, &debuglevel, 'd', "Debuglevel to use" },
594 {"outputfile", 'o', POPT_ARG_STRING, &outputfile, 'o', "Write downloaded data to specified file" },
595 {"stdout", 'O', POPT_ARG_NONE, &send_stdout, 'O', "Write data to stdout" },
596 {"dots", 'D', POPT_ARG_NONE, &dots, 'D', "Show dots as progress indication" },
597 {"quiet", 'q', POPT_ARG_NONE, &quiet, 'q', "Be quiet" },
598 {"verbose", 'v', POPT_ARG_NONE, &verbose, 'v', "Be verbose" },
599 {"keep-permissions", 'P', POPT_ARG_NONE, &keep_permissions, 'P', "Keep permissions" },
600 {"blocksize", 'b', POPT_ARG_INT, &blocksize, 'b', "Change number of bytes in a block"},
601 {"rcfile", 'f', POPT_ARG_STRING, NULL, 'f', "Use specified rc file"},
602 POPT_AUTOHELP
603 POPT_TABLEEND
605 poptContext pc;
607 load_case_tables();
609 /* only read rcfile if it exists */
610 if (asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME")) == -1) {
611 return 1;
613 if(access(rcfile, F_OK) == 0)
614 readrcfile(rcfile, long_options);
615 free(rcfile);
617 #ifdef SIGWINCH
618 signal(SIGWINCH, change_columns);
619 #endif
620 signal(SIGINT, signal_quit);
621 signal(SIGTERM, signal_quit);
623 pc = poptGetContext(argv[0], argc, argv, long_options, 0);
625 while((c = poptGetNextOpt(pc)) >= 0) {
626 switch(c) {
627 case 'f':
628 readrcfile(poptGetOptArg(pc), long_options);
629 break;
630 case 'a':
631 username = ""; password = "";
632 break;
633 case 'e':
634 smb_encrypt = true;
635 break;
639 if((send_stdout || resume || outputfile) && update) {
640 fprintf(stderr, "The -o, -R or -O and -U options can not be used together.\n");
641 return 1;
643 if((send_stdout || outputfile) && recursive) {
644 fprintf(stderr, "The -o or -O and -R options can not be used together.\n");
645 return 1;
648 if(outputfile && send_stdout) {
649 fprintf(stderr, "The -o and -O options cannot be used together.\n");
650 return 1;
653 if(smbc_init(get_auth_data, debuglevel) < 0) {
654 fprintf(stderr, "Unable to initialize libsmbclient\n");
655 return 1;
658 if (smb_encrypt) {
659 SMBCCTX *smb_ctx = smbc_set_context(NULL);
660 smbc_option_set(smb_ctx,
661 CONST_DISCARD(char *, "smb_encrypt_level"),
662 "require");
665 columns = get_num_cols();
667 total_start_time = time(NULL);
669 while ( (file = poptGetArg(pc)) ) {
670 if (!recursive)
671 ret = smb_download_file(file, "", recursive, resume,
672 1, outputfile);
673 else
674 ret = smb_download_dir(file, "", resume);
677 TALLOC_FREE(frame);
678 if ( ret == 0){
679 clean_exit();
681 return ret;