changeset 439:a64003e250d4

Iterative cleanups on cmp.c: silence warnings, only free if TOYBOX_FREE, use xopen(), style cleanup on curly brackets.
author Rob Landley <rob@landley.net>
date Tue, 07 Feb 2012 21:32:32 -0600
parents a2f530c4c442
children 7cff5420c90a
files toys/cmp.c
diffstat 1 files changed, 13 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/toys/cmp.c	Tue Feb 07 12:13:41 2012 -0800
+++ b/toys/cmp.c	Tue Feb 07 21:32:32 2012 -0600
@@ -30,14 +30,13 @@
 	int fd;
 
 	if (!strcmp(file,"-")) fd=0;
-	else if (0>(fd = open(file, O_RDONLY, 0))) {
-		perror_exit("%s", file);
-	}
+	else fd = xopen(file, O_RDONLY);
 	return fd;
 }
 
 void do_cmp(int fd1, int fd2, char *file1, char *file2, char *buf1, char *buf2,
-	size_t size) {
+	size_t size)
+{
 	int i, len1, len2, min_len;
 	size_t byte_no = 1, line_no = 1;
 
@@ -49,15 +48,13 @@
 		for (i=0; i<min_len; i++) {
 			if (buf1[i] != buf2[i]) {
 				toys.exitval = 1;
-				if (toys.optflags & FLAG_l) {
-					printf("%d %o %o\n", byte_no, buf1[i],
-						buf2[i]);
-				}
+				if (toys.optflags & FLAG_l)
+					printf("%ld %o %o\n", (long)byte_no, buf1[i], buf2[i]);
 				else {
 					if (!(toys.optflags & FLAG_s)) {
-						printf("%s %s differ: char %d, line %d\n",
-							file1, file2, byte_no,
-							line_no);
+						printf("%s %s differ: char %ld, line %ld\n",
+							file1, file2, (long)byte_no,
+							(long)line_no);
 					}
 					return;
 				}
@@ -90,8 +87,10 @@
 
 	do_cmp(fd1, fd2, file1, file2, toybuf, toybuf2, size);
 
-	close(fd1);
-	close(fd2);
-	free(toybuf2);
+	if (CFG_TOYBOX_FREE) {
+		close(fd1);
+		close(fd2);
+		free(toybuf2);
+	}
 }