Skip to content

Commit

Permalink
formatting fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cospplredman committed Apr 24, 2024
1 parent 3a6ac7b commit e7a99f3
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions src/cmp/cmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
#include "cgetopt.h"
#include "version_info.h"

static const char usage[] = {
"Usage: cmp [Option]... File1 File2\n"
" --version version information\n"
static const char usage[] = {"Usage: cmp [Option]... File1 File2\n"
" --version version information\n"
" --help display this help and exit\n"
" -l output byte number (decimal) and differing bytes (octal) for each difference\n"
" -l output byte number (decimal) and "
"differing bytes (octal) for each difference\n"
" -s no output except exit status\n"};

// flags
Expand All @@ -26,16 +26,17 @@ static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
};

static void cmp_print(char *path1, int ch1, char *path2, int ch2, size_t byte, size_t line){
switch(output_format){
static void cmp_print(char *path1, int ch1, char *path2, int ch2, size_t byte,
size_t line) {
switch (output_format) {
case 'd':
printf("%s %s differ: byte %ld, line %ld\n", path1, path2, byte, line);
break;
case 'l':
printf("%ld %o %o\n", byte, ch1, ch2);
break;
case 's':
break;
break;
}
}

Expand Down Expand Up @@ -65,54 +66,54 @@ int main(int argc, char **argv) {
argc -= optind;
argv += optind;

if (argc != 2){
if (argc != 2) {
fprintf(stderr, "cmp: expected two files");
return EXIT_FAILURE;
}

FILE *file1 = fopen(argv[0], "r+");
if (file1 == NULL){
fprintf(stderr, "cmp: %s: %s\n", argv[0], strerror(errno));
return EXIT_FAILURE;
if (file1 == NULL) {
fprintf(stderr, "cmp: %s: %s\n", argv[0], strerror(errno));
return EXIT_FAILURE;
}

FILE *file2 = fopen(argv[1], "r+");
if (file2 == NULL){
fprintf(stderr, "cmp: %s: %s\n", argv[1], strerror(errno));
return EXIT_FAILURE;
if (file2 == NULL) {
fprintf(stderr, "cmp: %s: %s\n", argv[1], strerror(errno));
return EXIT_FAILURE;
}

int ch1, ch2;
size_t byte = 1, line = 1;
char *path = NULL;

while(1){
while (1) {
ch1 = getc(file1);
ch2 = getc(file2);

if(ch1 == EOF || ch2 == EOF)
if (ch1 == EOF || ch2 == EOF)
break;

if(ch1 != ch2){
if (ch1 != ch2) {
cmp_print(argv[0], ch1, argv[1], ch2, byte, line);
if(output_format != 'l')
return 1; //files are different
if (output_format != 'l')
return 1; // files are different
}

if(ch1 == '\n')
if (ch1 == '\n')
line++;
byte++;
}

if(ch1 != EOF || ch2 != EOF){
if(ch1 == EOF)
if (ch1 != EOF || ch2 != EOF) {
if (ch1 == EOF)
path = argv[0];
if(ch2 == EOF)
if (ch2 == EOF)
path = argv[1];

printf("cmp: EOF on %s after byte %lu\n", path, byte);
return 1; //files are different
return 1; // files are different
}

return 0; //files are the same
return 0; // files are the same
}

0 comments on commit e7a99f3

Please sign in to comment.