[PATCH] fix a bug in smbget

Christian Ambach ambi at samba.org
Mon Jan 25 08:01:48 UTC 2016


Hi Andreas,

Am 25.01.16 08:06, schrieb Andreas Schneider:
>> the attached patchset contains a fix and some code cleanup for smbget.
>> The first patch fixes the actual bug that the recursive download only
>> works if the local username equals the username to be used for the SMB
>> server to connect to.
I've just realized that I have used a C++ style comment in this patch, 
so I have attached a new version of the patchset using a the correct 
comment style.

>> The rest of the patches are code cleanups to adopt the code to the
>> coding standards and to make use of some C99 features.
>
> I will review your patchset. I've started with smbget cleanup ~5 weeks ago.
> But I first tried to fix the option parsing, as it is broken too.
>
> Maybe you want to take a look at:
>
> https://git.samba.org/?p=asn/samba.git;a=commitdiff;h=7b9453c6510a99875cf0beef73bb2ab8476f1c6f

I like your idea of having a struct which stores all of the options 
instead of having all of these global variables. I have not looked at 
broken logic in the tool yet, just at the cosmetics.
So your work will be a useful next step on top of mine.

Cheers,
Christian

-------------- next part --------------
>From b736e5cd6a9fbb5f225a01d14a27c4d74406c73e Mon Sep 17 00:00:00 2001
From: Christian Ambach <ambi at samba.org>
Date: Wed, 30 Dec 2015 21:25:13 +0100
Subject: [PATCH 1/4] s3:utils/smbget fix recursive download

get_auth_data is called multiple times (once for the directory listing and then
for every file to be downloaded). Save the obtained values across multiple calls
to make smbclient use the correct username for each download.

Bug: https://bugzilla.samba.org/show_bug.cgi?id=6482
Signed-off-by: Christian Ambach <ambi at samba.org>
---
 source3/utils/smbget.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c
index b3ce743..a077aa1 100644
--- a/source3/utils/smbget.c
+++ b/source3/utils/smbget.c
@@ -91,10 +91,18 @@ static void human_readable(off_t s, char *buffer, int l)
 static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen, char *un, int unlen, char *pw, int pwlen)
 {
 	static char hasasked = 0;
+	static char *savedwg;
+	static char *savedun;
+	static char *savedpw;
 	char *wgtmp, *usertmp;
 	char tmp[128];
 
-	if(hasasked) return;
+	if (hasasked) {
+		strncpy(wg, savedwg, wglen - 1);
+		strncpy(un, savedun, unlen - 1);
+		strncpy(pw, savedpw, pwlen - 1);
+		return;
+	}
 	hasasked = 1;
 
 	if(!nonprompt && !username) {
@@ -119,6 +127,11 @@ static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen,
 
 	if(workgroup)strncpy(wg, workgroup, wglen-1);
 
+	/* save the values found for later */
+	savedwg = SMB_STRDUP(wg);
+	savedun = SMB_STRDUP(un);
+	savedpw = SMB_STRDUP(pw);
+
 	wgtmp = SMB_STRNDUP(wg, wglen); 
 	usertmp = SMB_STRNDUP(un, unlen);
 	if(!quiet)printf("Using workgroup %s, %s%s\n", wgtmp, *usertmp?"user ":"guest user", usertmp);
-- 
1.9.1


>From 853f93861a52f6e147c0707b9d71367810c646af Mon Sep 17 00:00:00 2001
From: Christian Ambach <ambi at samba.org>
Date: Sun, 24 Jan 2016 11:17:20 +0100
Subject: [PATCH 2/4] s3:utils/smbget code format

adopt the code to latest README.Coding standards
e.g. curly braces everywhere, blanks before braces,
obey 80 character limit (except for the popt definitions)

Signed-off-by: Christian Ambach <ambi at samba.org>
---
 source3/utils/smbget.c | 504 ++++++++++++++++++++++++++++++++-----------------
 1 file changed, 331 insertions(+), 173 deletions(-)

diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c
index a077aa1..1bc637a 100644
--- a/source3/utils/smbget.c
+++ b/source3/utils/smbget.c
@@ -1,6 +1,6 @@
 /*
-   smbget: a wget-like utility with support for recursive downloading and 
-   	smb:// urls
+   smbget: a wget-like utility with support for recursive downloading of
+	smb:// urls
    Copyright (C) 2003-2004 Jelmer Vernooij <jelmer at samba.org>
 
    This program is free software; you can redistribute it and/or modify
@@ -40,15 +40,19 @@ static off_t total_bytes = 0;
 
 #define SMB_MAXPATHLEN MAXPATHLEN
 
-/* Number of bytes to read when checking whether local and remote file are really the same file */
-#define RESUME_CHECK_SIZE 				512
-#define RESUME_DOWNLOAD_OFFSET			1024
-#define RESUME_CHECK_OFFSET				RESUME_DOWNLOAD_OFFSET+RESUME_CHECK_SIZE
+/*
+ * Number of bytes to read when checking whether local and remote file
+ * are really the same file
+ */
+#define RESUME_CHECK_SIZE 	512
+#define RESUME_DOWNLOAD_OFFSET	1024
+#define RESUME_CHECK_OFFSET	(RESUME_DOWNLOAD_OFFSET+RESUME_CHECK_SIZE)
 /* Number of bytes to read at once */
-#define SMB_DEFAULT_BLOCKSIZE 					64000
+#define SMB_DEFAULT_BLOCKSIZE 	64000
 
 static const char *username = NULL, *password = NULL, *workgroup = NULL;
-static int nonprompt = 0, quiet = 0, dots = 0, keep_permissions = 0, verbose = 0, send_stdout = 0;
+static int nonprompt = 0, quiet = 0, dots = 0, keep_permissions = 0,
+	   verbose = 0, send_stdout = 0;
 static int blocksize = SMB_DEFAULT_BLOCKSIZE;
 
 static int smb_download_file(const char *base, const char *name, int recursive,
@@ -58,14 +62,16 @@ static int get_num_cols(void)
 {
 #ifdef TIOCGWINSZ
 	struct winsize ws;
-	if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
+	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
 		return 0;
 	}
 	return ws.ws_col;
 #else
 #warning No support for TIOCGWINSZ
 	char *cols = getenv("COLUMNS");
-	if(!cols) return 0;
+	if (!cols) {
+		return 0;
+	}
 	return atoi(cols);
 #endif
 }
@@ -88,13 +94,13 @@ static void human_readable(off_t s, char *buffer, int l)
 	}
 }
 
-static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen, char *un, int unlen, char *pw, int pwlen)
+static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen,
+			  char *un, int unlen, char *pw, int pwlen)
 {
 	static char hasasked = 0;
 	static char *savedwg;
 	static char *savedun;
 	static char *savedpw;
-	char *wgtmp, *usertmp;
 	char tmp[128];
 
 	if (hasasked) {
@@ -105,37 +111,49 @@ static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen,
 	}
 	hasasked = 1;
 
-	if(!nonprompt && !username) {
+	if (!nonprompt && !username) {
 		printf("Username for %s at %s [guest] ", shr, srv);
 		if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
 			return;
 		}
-		if ((strlen(tmp) > 0) && (tmp[strlen(tmp)-1] == '\n')) {
-			tmp[strlen(tmp)-1] = '\0';
+		if ((strlen(tmp) > 0) && (tmp[strlen(tmp) - 1] == '\n')) {
+			tmp[strlen(tmp) - 1] = '\0';
 		}
-		strncpy(un, tmp, unlen-1);
-	} else if(username) strncpy(un, username, unlen-1);
+		strncpy(un, tmp, unlen - 1);
+	} else if (username) {
+		strncpy(un, username, unlen - 1);
+	}
 
-	if(!nonprompt && !password) {
+	if (!nonprompt && !password) {
 		char *prompt;
-		if (asprintf(&prompt, "Password for %s at %s: ", shr, srv) == -1) {
+		if (asprintf(&prompt, "Password for %s at %s: ", shr, srv) ==
+		    -1) {
 			return;
 		}
-		(void) samba_getpass(prompt, pw, pwlen, false, false);
+		(void)samba_getpass(prompt, pw, pwlen, false, false);
 		free(prompt);
-	} else if(password) strncpy(pw, password, pwlen-1);
+	} else if (password) {
+		strncpy(pw, password, pwlen - 1);
+	}
 
-	if(workgroup)strncpy(wg, workgroup, wglen-1);
+	if (workgroup) {
+		strncpy(wg, workgroup, wglen - 1);
+	}
 
 	/* save the values found for later */
 	savedwg = SMB_STRDUP(wg);
 	savedun = SMB_STRDUP(un);
 	savedpw = SMB_STRDUP(pw);
 
-	wgtmp = SMB_STRNDUP(wg, wglen); 
-	usertmp = SMB_STRNDUP(un, unlen);
-	if(!quiet)printf("Using workgroup %s, %s%s\n", wgtmp, *usertmp?"user ":"guest user", usertmp);
-	free(wgtmp); free(usertmp);
+	if (!quiet) {
+		char *wgtmp, *usertmp;
+		wgtmp = SMB_STRNDUP(wg, wglen);
+		usertmp = SMB_STRNDUP(un, unlen);
+		printf("Using workgroup %s, %s%s\n", wgtmp,
+		       *usertmp ? "user " : "guest user", usertmp);
+		free(wgtmp);
+		free(usertmp);
+	}
 }
 
 /* Return 1 on error, 0 on success. */
@@ -150,32 +168,40 @@ static int smb_download_dir(const char *base, const char *name, int resume)
 	struct stat remotestat;
 	int ret = 0;
 
-	snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (base[0] && name[0] && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name);
+	snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
+		 (base[0] && name[0] && name[0] != '/' &&
+		  base[strlen(base)-1] != '/') ? "/" : "",
+		 name);
 
 	/* List files in directory and call smb_download_file on them */
 	dirhandle = smbc_opendir(path);
-	if(dirhandle < 1) {
+	if (dirhandle < 1) {
 		if (errno == ENOTDIR) {
-			return smb_download_file(base, name, 1, resume,
-						 0, NULL);
+			return smb_download_file(base, name, 1, resume, 0,
+						 NULL);
 		}
-		fprintf(stderr, "Can't open directory %s: %s\n", path, strerror(errno));
+		fprintf(stderr, "Can't open directory %s: %s\n", path,
+			strerror(errno));
 		return 1;
 	}
 
-	while(*relname == '/')relname++;
+	while (*relname == '/') {
+		relname++;
+	}
 	mkdir(relname, 0755);
 
 	tmpname = SMB_STRDUP(name);
 
-	while((dirent = smbc_readdir(dirhandle))) {
+	while ((dirent = smbc_readdir(dirhandle))) {
 		char *newname;
-		if(!strcmp(dirent->name, ".") || !strcmp(dirent->name, ".."))continue;
+		if (!strcmp(dirent->name, ".") || !strcmp(dirent->name, "..")) {
+			continue;
+		}
 		if (asprintf(&newname, "%s/%s", tmpname, dirent->name) == -1) {
 			free(tmpname);
 			return 1;
 		}
-		switch(dirent->smbc_type) {
+		switch (dirent->smbc_type) {
 		case SMBC_DIR:
 			ret = smb_download_dir(base, newname, resume);
 			break;
@@ -198,35 +224,48 @@ static int smb_download_dir(const char *base, const char *name, int resume)
 			break;
 
 		case SMBC_PRINTER_SHARE:
-			if(!quiet)printf("Ignoring printer share %s\n", dirent->name);
+			if (!quiet) {
+				printf("Ignoring printer share %s\n",
+				       dirent->name);
+			}
 			break;
 
 		case SMBC_COMMS_SHARE:
-			if(!quiet)printf("Ignoring comms share %s\n", dirent->name);
+			if (!quiet) {
+				printf("Ignoring comms share %s\n",
+				       dirent->name);
+			}
 			break;
 
 		case SMBC_IPC_SHARE:
-			if(!quiet)printf("Ignoring ipc$ share %s\n", dirent->name);
+			if (!quiet) {
+				printf("Ignoring ipc$ share %s\n",
+				       dirent->name);
+			}
 			break;
 
 		default:
-			fprintf(stderr, "Ignoring file '%s' of type '%d'\n", newname, dirent->smbc_type);
+			fprintf(stderr, "Ignoring file '%s' of type '%d'\n",
+				newname, dirent->smbc_type);
 			break;
 		}
 		free(newname);
 	}
 	free(tmpname);
 
-	if(keep_permissions) {
-		if(smbc_fstat(dirhandle, &remotestat) < 0) {
-			fprintf(stderr, "Unable to get stats on %s on remote server\n", path);
+	if (keep_permissions) {
+		if (smbc_fstat(dirhandle, &remotestat) < 0) {
+			fprintf(stderr,
+				"Unable to get stats on %s on remote server\n",
+				path);
 			smbc_closedir(dirhandle);
 			return 1;
 		}
 
-		if(chmod(relname, remotestat.st_mode) < 0) {
-			fprintf(stderr, "Unable to change mode of local dir %s to %o\n", relname,
-				(unsigned int)remotestat.st_mode);
+		if (chmod(relname, remotestat.st_mode) < 0) {
+			fprintf(stderr,
+				"Unable to change mode of local dir %s to %o\n",
+				relname, (unsigned int)remotestat.st_mode);
 			smbc_closedir(dirhandle);
 			return 1;
 		}
@@ -240,7 +279,7 @@ static char *print_time(long t)
 {
 	static char buffer[100];
 	int secs, mins, hours;
-	if(t < -1) {
+	if (t < -1) {
 		strncpy(buffer, "Unknown", sizeof(buffer));
 		return buffer;
 	}
@@ -248,45 +287,57 @@ static char *print_time(long t)
 	secs = (int)t % 60;
 	mins = (int)t / 60 % 60;
 	hours = (int)t / (60 * 60);
-	snprintf(buffer, sizeof(buffer)-1, "%02d:%02d:%02d", hours, mins, secs);
+	snprintf(buffer, sizeof(buffer) - 1, "%02d:%02d:%02d", hours, mins,
+		 secs);
 	return buffer;
 }
 
-static void print_progress(const char *name, time_t start, time_t now, off_t start_pos, off_t pos, off_t total)
+static void print_progress(const char *name, time_t start, time_t now,
+			   off_t start_pos, off_t pos, off_t total)
 {
 	double avg = 0.0;
-	long  eta = -1; 
+	long eta = -1;
 	double prcnt = 0.0;
 	char hpos[20], htotal[20], havg[20];
 	char *status, *filename;
 	int len;
-	if(now - start)avg = 1.0 * (pos - start_pos) / (now - start);
+	if (now - start) {
+		avg = 1.0 * (pos - start_pos) / (now - start);
+	}
 	eta = (total - pos) / avg;
-	if(total)prcnt = 100.0 * pos / total;
+	if (total) {
+		prcnt = 100.0 * pos / total;
+	}
 
 	human_readable(pos, hpos, sizeof(hpos));
 	human_readable(total, htotal, sizeof(htotal));
 	human_readable(avg, havg, sizeof(havg));
 
-	len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos, htotal, prcnt, havg, print_time(eta));
+	len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos,
+		       htotal, prcnt, havg, print_time(eta));
 	if (len == -1) {
 		return;
 	}
 
-	if(columns) {
-		int required = strlen(name), available = columns - len - strlen("[] ");
-		if(required > available) {
-			if (asprintf(&filename, "...%s", name + required - available + 3) == -1) {
+	if (columns) {
+		int required = strlen(name),
+		    available = columns - len - strlen("[] ");
+		if (required > available) {
+			if (asprintf(&filename, "...%s",
+				     name + required - available + 3) == -1) {
 				return;
 			}
 		} else {
 			filename = SMB_STRNDUP(name, available);
 		}
-	} else filename = SMB_STRDUP(name);
+	} else {
+		filename = SMB_STRDUP(name);
+	}
 
 	fprintf(stderr, "\r[%s] %s", filename, status);
 
-	free(filename); free(status);
+	free(filename);
+	free(status);
 }
 
 /* Return 1 on error, 0 on success. */
@@ -300,24 +351,33 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 	char path[SMB_MAXPATHLEN];
 	char checkbuf[2][RESUME_CHECK_SIZE];
 	char *readbuf = NULL;
-	off_t offset_download = 0, offset_check = 0, curpos = 0, start_offset = 0;
+	off_t offset_download = 0, offset_check = 0, curpos = 0,
+	      start_offset = 0;
 	struct stat localstat, remotestat;
 
-	snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (*base && *name && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name);
+	snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base,
+		 (*base && *name && name[0] != '/' &&
+		  base[strlen(base)-1] != '/') ? "/" : "",
+		 name);
 
 	remotehandle = smbc_open(path, O_RDONLY, 0755);
 
-	if(remotehandle < 0) {
-		switch(errno) {
-		case EISDIR: 
-			if(!recursive) {
-				fprintf(stderr, "%s is a directory. Specify -R to download recursively\n", path);
+	if (remotehandle < 0) {
+		switch (errno) {
+		case EISDIR:
+			if (!recursive) {
+				fprintf(stderr,
+					"%s is a directory. Specify -R "
+					"to download recursively\n",
+					path);
 				return 1;
 			}
 			return smb_download_dir(base, name, resume);
 
 		case ENOENT:
-			fprintf(stderr, "%s can't be found on the remote server\n", path);
+			fprintf(stderr,
+				"%s can't be found on the remote server\n",
+				path);
 			return 1;
 
 		case ENOMEM:
@@ -325,63 +385,80 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 			return 1;
 
 		case ENODEV:
-			fprintf(stderr, "The share name used in %s does not exist\n", path);
+			fprintf(stderr,
+				"The share name used in %s does not exist\n",
+				path);
 			return 1;
 
 		case EACCES:
-			fprintf(stderr, "You don't have enough permissions to access %s\n", path);
+			fprintf(stderr, "You don't have enough permissions "
+				"to access %s\n",
+				path);
 			return 1;
 
 		default:
 			perror("smbc_open");
 			return 1;
 		}
-	} 
+	}
 
-	if(smbc_fstat(remotehandle, &remotestat) < 0) {
+	if (smbc_fstat(remotehandle, &remotestat) < 0) {
 		fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
 		return 1;
 	}
 
-	if(outfile) newpath = outfile;
-	else if(!name[0]) {
+	if (outfile) {
+		newpath = outfile;
+	} else if (!name[0]) {
 		newpath = strrchr(base, '/');
-		if(newpath)newpath++; else newpath = base;
-	} else newpath = name;
+		if (newpath) {
+			newpath++;
+		} else {
+			newpath = base;
+		}
+	} else {
+		newpath = name;
+	}
 
 	if (!toplevel && (newpath[0] == '/')) {
 		newpath++;
 	}
 
 	/* Open local file according to the mode */
-	if(update) {
+	if (update) {
 		/* if it is up-to-date, skip */
-		if(stat(newpath, &localstat) == 0 &&
-				localstat.st_mtime >= remotestat.st_mtime) {
-			if(verbose)
+		if (stat(newpath, &localstat) == 0 &&
+		    localstat.st_mtime >= remotestat.st_mtime) {
+			if (verbose) {
 				printf("%s is up-to-date, skipping\n", newpath);
+			}
 			smbc_close(remotehandle);
 			return 0;
 		}
 		/* else open it for writing and truncate if it exists */
-		localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | O_TRUNC, 0775);
-		if(localhandle < 0) {
+		localhandle = open(
+		    newpath, O_CREAT | O_NONBLOCK | O_RDWR | O_TRUNC, 0775);
+		if (localhandle < 0) {
 			fprintf(stderr, "Can't open %s : %s\n", newpath,
-					strerror(errno));
+				strerror(errno));
 			smbc_close(remotehandle);
 			return 1;
 		}
 		/* no offset */
-	} else if(!send_stdout) {
-		localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | (!resume?O_EXCL:0), 0755);
-		if(localhandle < 0) {
-			fprintf(stderr, "Can't open %s: %s\n", newpath, strerror(errno));
+	} else if (!send_stdout) {
+		localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR |
+						(!resume ? O_EXCL : 0),
+				   0755);
+		if (localhandle < 0) {
+			fprintf(stderr, "Can't open %s: %s\n", newpath,
+				strerror(errno));
 			smbc_close(remotehandle);
 			return 1;
 		}
 
 		if (fstat(localhandle, &localstat) != 0) {
-			fprintf(stderr, "Can't fstat %s: %s\n", newpath, strerror(errno));
+			fprintf(stderr, "Can't fstat %s: %s\n", newpath,
+				strerror(errno));
 			smbc_close(remotehandle);
 			close(localhandle);
 			return 1;
@@ -389,68 +466,112 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 
 		start_offset = localstat.st_size;
 
-		if(localstat.st_size && localstat.st_size == remotestat.st_size) {
-			if(verbose)fprintf(stderr, "%s is already downloaded completely.\n", path);
-			else if(!quiet)fprintf(stderr, "%s\n", path);
+		if (localstat.st_size &&
+		    localstat.st_size == remotestat.st_size) {
+			if (verbose) {
+				fprintf(stderr, "%s is already downloaded "
+					"completely.\n",
+					path);
+			} else if (!quiet) {
+				fprintf(stderr, "%s\n", path);
+			}
 			smbc_close(remotehandle);
 			close(localhandle);
 			return 0;
 		}
 
-		if(localstat.st_size > RESUME_CHECK_OFFSET && remotestat.st_size > RESUME_CHECK_OFFSET) {
-			offset_download = localstat.st_size - RESUME_DOWNLOAD_OFFSET;
+		if (localstat.st_size > RESUME_CHECK_OFFSET &&
+		    remotestat.st_size > RESUME_CHECK_OFFSET) {
+			offset_download =
+			    localstat.st_size - RESUME_DOWNLOAD_OFFSET;
 			offset_check = localstat.st_size - RESUME_CHECK_OFFSET;
-			if(verbose)printf("Trying to start resume of %s at "OFF_T_FORMAT"\n"
-				   "At the moment "OFF_T_FORMAT" of "OFF_T_FORMAT" bytes have been retrieved\n",
-				newpath, (OFF_T_FORMAT_CAST)offset_check, 
-				(OFF_T_FORMAT_CAST)localstat.st_size,
-				(OFF_T_FORMAT_CAST)remotestat.st_size);
+			if (verbose) {
+				printf("Trying to start resume of %s "
+				       "at " OFF_T_FORMAT "\n"
+				       "At the moment " OFF_T_FORMAT
+				       " of " OFF_T_FORMAT
+				       " bytes have been retrieved\n",
+				       newpath, (OFF_T_FORMAT_CAST)offset_check,
+				       (OFF_T_FORMAT_CAST)localstat.st_size,
+				       (OFF_T_FORMAT_CAST)remotestat.st_size);
+			}
 		}
 
-		if(offset_check) { 
+		if (offset_check) {
 			off_t off1, off2;
-			/* First, check all bytes from offset_check to offset_download */
+			/* First, check all bytes from offset_check to
+			 * offset_download */
 			off1 = lseek(localhandle, offset_check, SEEK_SET);
-			if(off1 < 0) {
-				fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in local file %s\n",
-					(OFF_T_FORMAT_CAST)offset_check, newpath);
-				smbc_close(remotehandle); close(localhandle);
+			if (off1 < 0) {
+				fprintf(stderr, "Can't seek to " OFF_T_FORMAT
+						" in local file %s\n",
+					(OFF_T_FORMAT_CAST)offset_check,
+					newpath);
+				smbc_close(remotehandle);
+				close(localhandle);
 				return 1;
 			}
 
-			off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET); 
-			if(off2 < 0) {
-				fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in remote file %s\n",
-					(OFF_T_FORMAT_CAST)offset_check, newpath);
-				smbc_close(remotehandle); close(localhandle);
+			off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET);
+			if (off2 < 0) {
+				fprintf(stderr, "Can't seek to " OFF_T_FORMAT
+						" in remote file %s\n",
+					(OFF_T_FORMAT_CAST)offset_check,
+					newpath);
+				smbc_close(remotehandle);
+				close(localhandle);
 				return 1;
 			}
 
-			if(off1 != off2) {
-				fprintf(stderr, "Offset in local and remote files is different (local: "OFF_T_FORMAT", remote: "OFF_T_FORMAT")\n",
+			if (off1 != off2) {
+				fprintf(stderr, "Offset in local and remote "
+						"files is different "
+						"(local: " OFF_T_FORMAT
+						", remote: " OFF_T_FORMAT ")\n",
 					(OFF_T_FORMAT_CAST)off1,
 					(OFF_T_FORMAT_CAST)off2);
-				smbc_close(remotehandle); close(localhandle);
+				smbc_close(remotehandle);
+				close(localhandle);
 				return 1;
 			}
 
-			if(smbc_read(remotehandle, checkbuf[0], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
-				fprintf(stderr, "Can't read %d bytes from remote file %s\n", RESUME_CHECK_SIZE, path);
-				smbc_close(remotehandle); close(localhandle);
+			if (smbc_read(remotehandle, checkbuf[0],
+				      RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
+				fprintf(stderr, "Can't read %d bytes from "
+					"remote file %s\n",
+					RESUME_CHECK_SIZE, path);
+				smbc_close(remotehandle);
+				close(localhandle);
 				return 1;
 			}
 
-			if(read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
-				fprintf(stderr, "Can't read %d bytes from local file %s\n", RESUME_CHECK_SIZE, name);
-				smbc_close(remotehandle); close(localhandle);
+			if (read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) !=
+			    RESUME_CHECK_SIZE) {
+				fprintf(stderr, "Can't read %d bytes from "
+					"local file %s\n",
+					RESUME_CHECK_SIZE, name);
+				smbc_close(remotehandle);
+				close(localhandle);
 				return 1;
 			}
 
-			if(memcmp(checkbuf[0], checkbuf[1], RESUME_CHECK_SIZE) == 0) {
-				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);
+			if (memcmp(checkbuf[0], checkbuf[1],
+				   RESUME_CHECK_SIZE) == 0) {
+				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);
+				}
 			} else {
-				fprintf(stderr, "Local and remote file appear to be different, not doing resume for %s\n", path);
-				smbc_close(remotehandle); close(localhandle);
+				fprintf(stderr, "Local and remote file appear "
+					"to be different, not "
+					"doing resume for %s\n",
+					path);
+				smbc_close(remotehandle);
+				close(localhandle);
 				return 1;
 			}
 		}
@@ -470,12 +591,15 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 	}
 
 	/* Now, download all bytes from offset_download to the end */
-	for(curpos = offset_download; curpos < remotestat.st_size; curpos+=blocksize) {
+	for (curpos = offset_download; curpos < remotestat.st_size;
+	     curpos += blocksize) {
 		ssize_t bytesread = smbc_read(remotehandle, readbuf, blocksize);
 		if(bytesread < 0) {
 			fprintf(stderr, "Can't read %u bytes at offset "OFF_T_FORMAT", file %s\n", (unsigned int)blocksize, (OFF_T_FORMAT_CAST)curpos, path);
 			smbc_close(remotehandle);
-			if (localhandle != STDOUT_FILENO) close(localhandle);
+			if (localhandle != STDOUT_FILENO) {
+				close(localhandle);
+			}
 			free(readbuf);
 			return 1;
 		}
@@ -486,37 +610,42 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 			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);
 			free(readbuf);
 			smbc_close(remotehandle);
-			if (localhandle != STDOUT_FILENO) close(localhandle);
+			if (localhandle != STDOUT_FILENO) {
+				close(localhandle);
+			}
 			return 1;
 		}
 
-		if(dots)fputc('.', stderr);
-		else if(!quiet) {
+		if (dots) {
+			fputc('.', stderr);
+		} else if (!quiet) {
 			print_progress(newpath, start_time, time_mono(NULL),
-					start_offset, curpos, remotestat.st_size);
+				       start_offset, curpos,
+				       remotestat.st_size);
 		}
 	}
 
 	free(readbuf);
 
-	if(dots){
+	if (dots) {
 		fputc('\n', stderr);
 		printf("%s downloaded\n", path);
-	} else if(!quiet) {
+	} else if (!quiet) {
 		int i;
 		fprintf(stderr, "\r%s", path);
-		if(columns) {
-			for(i = strlen(path); i < columns; i++) {
+		if (columns) {
+			for (i = strlen(path); i < columns; i++) {
 				fputc(' ', stderr);
 			}
 		}
 		fputc('\n', stderr);
 	}
 
-	if(keep_permissions && !send_stdout) {
-		if(fchmod(localhandle, remotestat.st_mode) < 0) {
-			fprintf(stderr, "Unable to change mode of local file %s to %o\n", path,
-				(unsigned int)remotestat.st_mode);
+	if (keep_permissions && !send_stdout) {
+		if (fchmod(localhandle, remotestat.st_mode) < 0) {
+			fprintf(stderr, "Unable to change mode of local "
+				"file %s to %o\n",
+				path, (unsigned int)remotestat.st_mode);
 			smbc_close(remotehandle);
 			close(localhandle);
 			return 1;
@@ -524,7 +653,9 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 	}
 
 	smbc_close(remotehandle);
-	if (localhandle != STDOUT_FILENO) close(localhandle);
+	if (localhandle != STDOUT_FILENO) {
+		close(localhandle);
+	}
 	return 0;
 }
 
@@ -532,8 +663,10 @@ static void clean_exit(void)
 {
 	char bs[100];
 	human_readable(total_bytes, bs, sizeof(bs));
-	if(!quiet)fprintf(stderr, "Downloaded %s in %lu seconds\n", bs,
-		(unsigned long)(time_mono(NULL) - total_start_time));
+	if (!quiet) {
+		fprintf(stderr, "Downloaded %s in %lu seconds\n", bs,
+			(unsigned long)(time_mono(NULL) - total_start_time));
+	}
 	exit(0);
 }
 
@@ -548,32 +681,47 @@ static int readrcfile(const char *name, const struct poptOption long_options[])
 	int lineno = 0, i;
 	char var[101], val[101];
 	char found;
-	int *intdata; char **stringdata;
-	if(!fd) {
+	int *intdata;
+	char **stringdata;
+	if (!fd) {
 		fprintf(stderr, "Can't open RC file %s\n", name);
 		return 1;
 	}
 
-	while(!feof(fd)) {
+	while (!feof(fd)) {
 		lineno++;
-		if(fscanf(fd, "%100s %100s\n", var, val) < 2) {
-			fprintf(stderr, "Can't parse line %d of %s, ignoring.\n", lineno, name);
+		if (fscanf(fd, "%100s %100s\n", var, val) < 2) {
+			fprintf(stderr,
+				"Can't parse line %d of %s, ignoring.\n",
+				lineno, name);
 			continue;
 		}
 
 		found = 0;
 
-		for(i = 0; long_options[i].shortName; i++) {
-			if(!long_options[i].longName)continue;
-			if(strcmp(long_options[i].longName, var)) continue;
-			if(!long_options[i].arg)continue;
+		for (i = 0; long_options[i].shortName; i++) {
+			if (!long_options[i].longName) {
+				continue;
+			}
+			if (strcmp(long_options[i].longName, var)) {
+				continue;
+			}
+			if (!long_options[i].arg) {
+				continue;
+			}
 
-			switch(long_options[i].argInfo) {
+			switch (long_options[i].argInfo) {
 			case POPT_ARG_NONE:
 				intdata = (int *)long_options[i].arg;
-				if(!strcmp(val, "on")) *intdata = 1;
-				else if(!strcmp(val, "off")) *intdata = 0;
-				else fprintf(stderr, "Illegal value %s for %s at line %d in %s\n", val, var, lineno, name);
+				if (!strcmp(val, "on")) {
+					*intdata = 1;
+				} else if (!strcmp(val, "off")) {
+					*intdata = 0;
+				} else {
+					fprintf(stderr, "Illegal value %s for "
+						"%s at line %d in %s\n",
+						val, var, lineno, name);
+				}
 				break;
 			case POPT_ARG_INT:
 				intdata = (int *)long_options[i].arg;
@@ -584,14 +732,18 @@ static int readrcfile(const char *name, const struct poptOption long_options[])
 				*stringdata = SMB_STRDUP(val);
 				break;
 			default:
-				fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name);
+				fprintf(stderr, "Invalid variable %s at "
+					"line %d in %s\n",
+					var, lineno, name);
 				break;
 			}
 
 			found = 1;
 		}
-		if(!found) {
-			fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name);
+		if (!found) {
+			fprintf(stderr,
+				"Invalid variable %s at line %d in %s\n", var,
+				lineno, name);
 		}
 	}
 
@@ -609,7 +761,7 @@ int main(int argc, const char **argv)
 	TALLOC_CTX *frame = talloc_stackframe();
 	int ret = 0;
 	struct poptOption long_options[] = {
-		{"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" },	
+		{"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" },
 		{"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport" },
 		{"resume", 'r', POPT_ARG_NONE, &resume, 0, "Automatically resume aborted files" },
 		{"update", 'U',  POPT_ARG_NONE, &update, 0, "Download only when remote file is newer than local file or local file is missing"},
@@ -638,8 +790,9 @@ int main(int argc, const char **argv)
 	if (asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME")) == -1) {
 		return 1;
 	}
-	if(access(rcfile, F_OK) == 0) 
+	if (access(rcfile, F_OK) == 0) {
 		readrcfile(rcfile, long_options);
+	}
 	free(rcfile);
 
 #ifdef SIGWINCH
@@ -650,13 +803,14 @@ int main(int argc, const char **argv)
 
 	pc = poptGetContext(argv[0], argc, argv, long_options, 0);
 
-	while((c = poptGetNextOpt(pc)) >= 0) {
-		switch(c) {
+	while ((c = poptGetNextOpt(pc)) >= 0) {
+		switch (c) {
 		case 'f':
 			readrcfile(poptGetOptArg(pc), long_options);
 			break;
 		case 'a':
-			username = ""; password = "";
+			username = "";
+			password = "";
 			break;
 		case 'e':
 			smb_encrypt = true;
@@ -664,21 +818,24 @@ int main(int argc, const char **argv)
 		}
 	}
 
-	if((send_stdout || resume || outputfile) && update) {
-		fprintf(stderr, "The -o, -R or -O and -U options can not be used together.\n");
+	if ((send_stdout || resume || outputfile) && update) {
+		fprintf(stderr, "The -o, -R or -O and -U options can not be "
+			"used together.\n");
 		return 1;
 	}
-	if((send_stdout || outputfile) && recursive) {
-		fprintf(stderr, "The -o or -O and -R options can not be used together.\n");
+	if ((send_stdout || outputfile) && recursive) {
+		fprintf(stderr, "The -o or -O and -R options can not be "
+			"used together.\n");
 		return 1;
 	}
 
-	if(outputfile && send_stdout) {
-		fprintf(stderr, "The -o and -O options cannot be used together.\n");
+	if (outputfile && send_stdout) {
+		fprintf(stderr, "The -o and -O options can not be "
+			"used together.\n");
 		return 1;
 	}
 
-	if(smbc_init(get_auth_data, debuglevel) < 0) {
+	if (smbc_init(get_auth_data, debuglevel) < 0) {
 		fprintf(stderr, "Unable to initialize libsmbclient\n");
 		return 1;
 	}
@@ -686,24 +843,25 @@ int main(int argc, const char **argv)
 	if (smb_encrypt) {
 		SMBCCTX *smb_ctx = smbc_set_context(NULL);
 		smbc_option_set(smb_ctx,
-			discard_const_p(char, "smb_encrypt_level"),
-			"require");
+				discard_const_p(char, "smb_encrypt_level"),
+				"require");
 	}
 
 	columns = get_num_cols();
 
 	total_start_time = time_mono(NULL);
 
-	while ( (file = poptGetArg(pc)) ) {
-		if (!recursive) 
-			ret = smb_download_file(file, "", recursive, resume,
-						1, outputfile);
-		else 
+	while ((file = poptGetArg(pc))) {
+		if (!recursive) {
+			ret = smb_download_file(file, "", recursive, resume, 1,
+						outputfile);
+		} else {
 			ret = smb_download_dir(file, "", resume);
+		}
 	}
 
 	TALLOC_FREE(frame);
-	if ( ret == 0){
+	if (ret == 0) {
 		clean_exit();
 	}
 	return ret;
-- 
1.9.1


>From f3d7e9307439664ebf5ca8b85d1d8a842880ffca Mon Sep 17 00:00:00 2001
From: Christian Ambach <ambi at samba.org>
Date: Sun, 24 Jan 2016 11:45:59 +0100
Subject: [PATCH 3/4] s3:utils/smbget use C99 format identifiers

for ssize_t and off_t variables

Signed-off-by: Christian Ambach <ambi at samba.org>
---
 source3/utils/smbget.c | 68 +++++++++++++++++++++-----------------------------
 1 file changed, 29 insertions(+), 39 deletions(-)

diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c
index 1bc637a..6de0f98 100644
--- a/source3/utils/smbget.c
+++ b/source3/utils/smbget.c
@@ -21,14 +21,6 @@
 #include "popt_common.h"
 #include "libsmbclient.h"
 
-#if _FILE_OFFSET_BITS==64
-#define OFF_T_FORMAT "%lld"
-#define OFF_T_FORMAT_CAST long long
-#else
-#define OFF_T_FORMAT "%ld"
-#define OFF_T_FORMAT_CAST long
-#endif
-
 static int columns = 0;
 
 static int debuglevel, update;
@@ -53,7 +45,7 @@ static off_t total_bytes = 0;
 static const char *username = NULL, *password = NULL, *workgroup = NULL;
 static int nonprompt = 0, quiet = 0, dots = 0, keep_permissions = 0,
 	   verbose = 0, send_stdout = 0;
-static int blocksize = SMB_DEFAULT_BLOCKSIZE;
+static unsigned int blocksize = SMB_DEFAULT_BLOCKSIZE;
 
 static int smb_download_file(const char *base, const char *name, int recursive,
 			     int resume, int toplevel, char *outfile);
@@ -90,7 +82,7 @@ static void human_readable(off_t s, char *buffer, int l)
 	} else if (s > 1024) {
 		snprintf(buffer, l, "%.2fkB", 1.0 * s / 1024);
 	} else {
-		snprintf(buffer, l, OFF_T_FORMAT"b", (OFF_T_FORMAT_CAST)s);
+		snprintf(buffer, l, "%jdb", (intmax_t)s);
 	}
 }
 
@@ -486,14 +478,12 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 			    localstat.st_size - RESUME_DOWNLOAD_OFFSET;
 			offset_check = localstat.st_size - RESUME_CHECK_OFFSET;
 			if (verbose) {
-				printf("Trying to start resume of %s "
-				       "at " OFF_T_FORMAT "\n"
-				       "At the moment " OFF_T_FORMAT
-				       " of " OFF_T_FORMAT
-				       " bytes have been retrieved\n",
-				       newpath, (OFF_T_FORMAT_CAST)offset_check,
-				       (OFF_T_FORMAT_CAST)localstat.st_size,
-				       (OFF_T_FORMAT_CAST)remotestat.st_size);
+				printf("Trying to start resume of %s at %jd\n"
+				       "At the moment %jd of %jd bytes have "
+				       "been retrieved\n",
+				       newpath, (intmax_t)offset_check,
+				       (intmax_t)localstat.st_size,
+				       (intmax_t)remotestat.st_size);
 			}
 		}
 
@@ -503,10 +493,9 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 			 * offset_download */
 			off1 = lseek(localhandle, offset_check, SEEK_SET);
 			if (off1 < 0) {
-				fprintf(stderr, "Can't seek to " OFF_T_FORMAT
-						" in local file %s\n",
-					(OFF_T_FORMAT_CAST)offset_check,
-					newpath);
+				fprintf(stderr,
+					"Can't seek to %jd in local file %s\n",
+					(intmax_t)offset_check, newpath);
 				smbc_close(remotehandle);
 				close(localhandle);
 				return 1;
@@ -514,10 +503,9 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 
 			off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET);
 			if (off2 < 0) {
-				fprintf(stderr, "Can't seek to " OFF_T_FORMAT
-						" in remote file %s\n",
-					(OFF_T_FORMAT_CAST)offset_check,
-					newpath);
+				fprintf(stderr,
+					"Can't seek to %jd in remote file %s\n",
+					(intmax_t)offset_check, newpath);
 				smbc_close(remotehandle);
 				close(localhandle);
 				return 1;
@@ -525,11 +513,9 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 
 			if (off1 != off2) {
 				fprintf(stderr, "Offset in local and remote "
-						"files is different "
-						"(local: " OFF_T_FORMAT
-						", remote: " OFF_T_FORMAT ")\n",
-					(OFF_T_FORMAT_CAST)off1,
-					(OFF_T_FORMAT_CAST)off2);
+					"files are different "
+					"(local: %jd, remote: %jd)\n",
+					(intmax_t)off1, (intmax_t)off2);
 				smbc_close(remotehandle);
 				close(localhandle);
 				return 1;
@@ -558,12 +544,11 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 			if (memcmp(checkbuf[0], checkbuf[1],
 				   RESUME_CHECK_SIZE) == 0) {
 				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);
+					printf("Current local and remote file "
+					       "appear to be the same. "
+					       "Starting download from "
+					       "offset %jd\n",
+					       (intmax_t)offset_download);
 				}
 			} else {
 				fprintf(stderr, "Local and remote file appear "
@@ -595,7 +580,9 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 	     curpos += blocksize) {
 		ssize_t bytesread = smbc_read(remotehandle, readbuf, blocksize);
 		if(bytesread < 0) {
-			fprintf(stderr, "Can't read %u bytes at offset "OFF_T_FORMAT", file %s\n", (unsigned int)blocksize, (OFF_T_FORMAT_CAST)curpos, path);
+			fprintf(stderr,
+				"Can't read %u bytes at offset %jd, file %s\n",
+				blocksize, (intmax_t)curpos, path);
 			smbc_close(remotehandle);
 			if (localhandle != STDOUT_FILENO) {
 				close(localhandle);
@@ -607,7 +594,10 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 		total_bytes += bytesread;
 
 		if(write(localhandle, readbuf, bytesread) < 0) {
-			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);
+			fprintf(stderr,
+				"Can't write %zd bytes to local file %s at "
+				"offset %jd\n", bytesread, path,
+				(intmax_t)curpos);
 			free(readbuf);
 			smbc_close(remotehandle);
 			if (localhandle != STDOUT_FILENO) {
-- 
1.9.1


>From 34a643271272073043d14db513b30b0d8c18cd7c Mon Sep 17 00:00:00 2001
From: Christian Ambach <ambi at samba.org>
Date: Sun, 24 Jan 2016 11:57:01 +0100
Subject: [PATCH 4/4] s3:utils/smbget make use of bool for flags

convert flags stored as int to bool

Signed-off-by: Christian Ambach <ambi at samba.org>
---
 source3/utils/smbget.c | 93 ++++++++++++++++++++++++++------------------------
 1 file changed, 48 insertions(+), 45 deletions(-)

diff --git a/source3/utils/smbget.c b/source3/utils/smbget.c
index 6de0f98..f596a8c 100644
--- a/source3/utils/smbget.c
+++ b/source3/utils/smbget.c
@@ -23,7 +23,7 @@
 
 static int columns = 0;
 
-static int debuglevel, update;
+static int debuglevel;
 static char *outputfile;
 
 
@@ -43,12 +43,14 @@ static off_t total_bytes = 0;
 #define SMB_DEFAULT_BLOCKSIZE 	64000
 
 static const char *username = NULL, *password = NULL, *workgroup = NULL;
-static int nonprompt = 0, quiet = 0, dots = 0, keep_permissions = 0,
-	   verbose = 0, send_stdout = 0;
+static bool nonprompt = false, quiet = false, dots = false,
+	    keep_permissions = false, verbose = false, send_stdout = false,
+	    update = false;
 static unsigned int blocksize = SMB_DEFAULT_BLOCKSIZE;
 
-static int smb_download_file(const char *base, const char *name, int recursive,
-			     int resume, int toplevel, char *outfile);
+static bool smb_download_file(const char *base, const char *name,
+			      bool recursive, bool resume, bool toplevel,
+			      char *outfile);
 
 static int get_num_cols(void)
 {
@@ -89,7 +91,7 @@ static void human_readable(off_t s, char *buffer, int l)
 static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen,
 			  char *un, int unlen, char *pw, int pwlen)
 {
-	static char hasasked = 0;
+	static bool hasasked = false;
 	static char *savedwg;
 	static char *savedun;
 	static char *savedpw;
@@ -101,7 +103,7 @@ static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen,
 		strncpy(pw, savedpw, pwlen - 1);
 		return;
 	}
-	hasasked = 1;
+	hasasked = true;
 
 	if (!nonprompt && !username) {
 		printf("Username for %s at %s [guest] ", shr, srv);
@@ -169,8 +171,8 @@ static int smb_download_dir(const char *base, const char *name, int resume)
 	dirhandle = smbc_opendir(path);
 	if (dirhandle < 1) {
 		if (errno == ENOTDIR) {
-			return smb_download_file(base, name, 1, resume, 0,
-						 NULL);
+			return smb_download_file(base, name, true, resume,
+						 false, NULL);
 		}
 		fprintf(stderr, "Can't open directory %s: %s\n", path,
 			strerror(errno));
@@ -207,8 +209,8 @@ static int smb_download_dir(const char *base, const char *name, int resume)
 			break;
 
 		case SMBC_FILE:
-			ret = smb_download_file(base, newname, 1, resume, 0,
-						NULL);
+			ret = smb_download_file(base, newname, true, resume,
+						false, NULL);
 			break;
 
 		case SMBC_FILE_SHARE:
@@ -332,10 +334,11 @@ static void print_progress(const char *name, time_t start, time_t now,
 	free(status);
 }
 
-/* Return 1 on error, 0 on success. */
+/* Return false on error, true on success. */
 
-static int smb_download_file(const char *base, const char *name, int recursive,
-			     int resume, int toplevel, char *outfile)
+static bool smb_download_file(const char *base, const char *name,
+			      bool recursive, bool resume, bool toplevel,
+			      char *outfile)
 {
 	int remotehandle, localhandle;
 	time_t start_time = time_mono(NULL);
@@ -362,7 +365,7 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 					"%s is a directory. Specify -R "
 					"to download recursively\n",
 					path);
-				return 1;
+				return false;
 			}
 			return smb_download_dir(base, name, resume);
 
@@ -370,33 +373,33 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 			fprintf(stderr,
 				"%s can't be found on the remote server\n",
 				path);
-			return 1;
+			return false;
 
 		case ENOMEM:
 			fprintf(stderr, "Not enough memory\n");
-			return 1;
+			return false;
 
 		case ENODEV:
 			fprintf(stderr,
 				"The share name used in %s does not exist\n",
 				path);
-			return 1;
+			return false;
 
 		case EACCES:
 			fprintf(stderr, "You don't have enough permissions "
 				"to access %s\n",
 				path);
-			return 1;
+			return false;
 
 		default:
 			perror("smbc_open");
-			return 1;
+			return false;
 		}
 	}
 
 	if (smbc_fstat(remotehandle, &remotestat) < 0) {
 		fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
-		return 1;
+		return false;
 	}
 
 	if (outfile) {
@@ -425,7 +428,7 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 				printf("%s is up-to-date, skipping\n", newpath);
 			}
 			smbc_close(remotehandle);
-			return 0;
+			return true;
 		}
 		/* else open it for writing and truncate if it exists */
 		localhandle = open(
@@ -434,7 +437,7 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 			fprintf(stderr, "Can't open %s : %s\n", newpath,
 				strerror(errno));
 			smbc_close(remotehandle);
-			return 1;
+			return false;
 		}
 		/* no offset */
 	} else if (!send_stdout) {
@@ -445,7 +448,7 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 			fprintf(stderr, "Can't open %s: %s\n", newpath,
 				strerror(errno));
 			smbc_close(remotehandle);
-			return 1;
+			return false;
 		}
 
 		if (fstat(localhandle, &localstat) != 0) {
@@ -453,7 +456,7 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 				strerror(errno));
 			smbc_close(remotehandle);
 			close(localhandle);
-			return 1;
+			return false;
 		}
 
 		start_offset = localstat.st_size;
@@ -469,7 +472,7 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 			}
 			smbc_close(remotehandle);
 			close(localhandle);
-			return 0;
+			return true;
 		}
 
 		if (localstat.st_size > RESUME_CHECK_OFFSET &&
@@ -498,7 +501,7 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 					(intmax_t)offset_check, newpath);
 				smbc_close(remotehandle);
 				close(localhandle);
-				return 1;
+				return false;
 			}
 
 			off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET);
@@ -508,7 +511,7 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 					(intmax_t)offset_check, newpath);
 				smbc_close(remotehandle);
 				close(localhandle);
-				return 1;
+				return false;
 			}
 
 			if (off1 != off2) {
@@ -518,7 +521,7 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 					(intmax_t)off1, (intmax_t)off2);
 				smbc_close(remotehandle);
 				close(localhandle);
-				return 1;
+				return false;
 			}
 
 			if (smbc_read(remotehandle, checkbuf[0],
@@ -528,7 +531,7 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 					RESUME_CHECK_SIZE, path);
 				smbc_close(remotehandle);
 				close(localhandle);
-				return 1;
+				return false;
 			}
 
 			if (read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) !=
@@ -538,7 +541,7 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 					RESUME_CHECK_SIZE, name);
 				smbc_close(remotehandle);
 				close(localhandle);
-				return 1;
+				return false;
 			}
 
 			if (memcmp(checkbuf[0], checkbuf[1],
@@ -557,7 +560,7 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 					path);
 				smbc_close(remotehandle);
 				close(localhandle);
-				return 1;
+				return false;
 			}
 		}
 	} else {
@@ -572,7 +575,7 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 		if (localhandle != STDOUT_FILENO) {
 			close(localhandle);
 		}
-		return 1;
+		return false;
 	}
 
 	/* Now, download all bytes from offset_download to the end */
@@ -588,7 +591,7 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 				close(localhandle);
 			}
 			free(readbuf);
-			return 1;
+			return false;
 		}
 
 		total_bytes += bytesread;
@@ -603,7 +606,7 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 			if (localhandle != STDOUT_FILENO) {
 				close(localhandle);
 			}
-			return 1;
+			return false;
 		}
 
 		if (dots) {
@@ -638,7 +641,7 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 				path, (unsigned int)remotestat.st_mode);
 			smbc_close(remotehandle);
 			close(localhandle);
-			return 1;
+			return false;
 		}
 	}
 
@@ -646,7 +649,7 @@ static int smb_download_file(const char *base, const char *name, int recursive,
 	if (localhandle != STDOUT_FILENO) {
 		close(localhandle);
 	}
-	return 0;
+	return true;
 }
 
 static void clean_exit(void)
@@ -670,7 +673,7 @@ static int readrcfile(const char *name, const struct poptOption long_options[])
 	FILE *fd = fopen(name, "r");
 	int lineno = 0, i;
 	char var[101], val[101];
-	char found;
+	bool found;
 	int *intdata;
 	char **stringdata;
 	if (!fd) {
@@ -687,7 +690,7 @@ static int readrcfile(const char *name, const struct poptOption long_options[])
 			continue;
 		}
 
-		found = 0;
+		found = false;
 
 		for (i = 0; long_options[i].shortName; i++) {
 			if (!long_options[i].longName) {
@@ -728,7 +731,7 @@ static int readrcfile(const char *name, const struct poptOption long_options[])
 				break;
 			}
 
-			found = 1;
+			found = true;
 		}
 		if (!found) {
 			fprintf(stderr,
@@ -749,7 +752,7 @@ int main(int argc, const char **argv)
 	bool smb_encrypt = false;
 	int resume = 0, recursive = 0;
 	TALLOC_CTX *frame = talloc_stackframe();
-	int ret = 0;
+	bool ret = true;
 	struct poptOption long_options[] = {
 		{"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" },
 		{"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport" },
@@ -843,16 +846,16 @@ int main(int argc, const char **argv)
 
 	while ((file = poptGetArg(pc))) {
 		if (!recursive) {
-			ret = smb_download_file(file, "", recursive, resume, 1,
-						outputfile);
+			ret = smb_download_file(file, "", recursive, resume,
+						true, outputfile);
 		} else {
 			ret = smb_download_dir(file, "", resume);
 		}
 	}
 
 	TALLOC_FREE(frame);
-	if (ret == 0) {
+	if (ret) {
 		clean_exit();
 	}
-	return ret;
+	return ret?0:1;
 }
-- 
1.9.1



More information about the samba-technical mailing list