summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-08-05 14:06:51 -0400
committerJakob Kaivo <jkk@ung.org>2019-08-05 14:06:51 -0400
commit111d65490a2e8f6122d84bde4545f75e8ec0dda0 (patch)
treefd0e657a74de8841fe5b960231aa0a0e58239491
parent23b68d3a7356f9cf3182759fa559af58704497f0 (diff)
formatting
-rw-r--r--cmp.c23
1 files changed, 16 insertions, 7 deletions
diff --git a/cmp.c b/cmp.c
index 6a03002..1c8897d 100644
--- a/cmp.c
+++ b/cmp.c
@@ -17,9 +17,10 @@
*
*/
-#define _XOPEN_SOURCE 700
+#define _POSIX_C_SOURCE 2
#include <errno.h>
#include <stdio.h>
+#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
@@ -33,7 +34,7 @@ FILE *fileopen(const char *path)
FILE *f = fopen(path, "rb");
if (f == NULL) {
- fprintf(stderr, "cmp: Couldn't open %s: %s\n", path, strerror(errno));
+ fprintf(stderr, "cmp: %s: %s\n", path, strerror(errno));
exit(1);
}
@@ -42,7 +43,7 @@ FILE *fileopen(const char *path)
int main(int argc, char *argv[])
{
- int c = 0;
+ int c;
enum { FIRSTONLY, ALL, SILENT } output = FIRSTONLY;
while ((c = getopt(argc, argv, "ls")) != EOF) {
@@ -61,7 +62,7 @@ int main(int argc, char *argv[])
}
if (optind != argc - 2) {
- fprintf(stderr, "cmp: Two files are required\n");
+ fprintf(stderr, "cmp: missing operand\n");
return 1;
}
@@ -86,14 +87,21 @@ int main(int argc, char *argv[])
}
}
- for (int byte = 1, line = 1; !(feof(f1) || feof(f2)); byte++) {
+ intmax_t byte = 0;
+ intmax_t line = 1;
+ while (++byte) {
int c1 = fgetc(f1);
int c2 = fgetc(f2);
if (c1 == c2) {
+ if (c1 == EOF) {
+ return 0;
+ }
+
if (c1 == '\n') {
line++;
}
+
continue;
}
@@ -102,10 +110,11 @@ int main(int argc, char *argv[])
} else if (c2 == EOF && output != SILENT) {
fprintf(stderr, "cmp: EOF on %s\n", file2);
} else if (output == ALL) {
- printf("%d %o %o\n", byte, c1, c2);
+ printf("%zd %o %o\n", byte, c1, c2);
continue;
} else if (output != SILENT) {
- printf("%s %s differ: char %d, line %d\n", file1, file2, byte, line);
+ printf("%s %s differ: char %zd, line %zd\n",
+ file1, file2, byte, line);
}
return 1;
}