diff --git a/diff-rcm b/diff-rcm index 034913b..b109891 100755 Binary files a/diff-rcm and b/diff-rcm differ diff --git a/diff.c b/diff.c index 79db9f3..d4ac62c 100644 --- a/diff.c +++ b/diff.c @@ -7,37 +7,17 @@ // ================================================================ // -void init(int argc, const char* argv[]); -void setoption(const char* arg, const char* s, const char* t, int* value); -void diff_output_conflict_error(void); - -void loadfiles(const char* filename1, const char* filename2); - -void version(); -void todo(); - -int normal(const char* filename1, const char* filename2); -int sideside(const char* filename1, const char* filename2); -void quiet(const char* filename1, const char* filename2); -void loud(const char* filename1, const char* filename2); - -// ================================================================ // - int main(int argc, const char* argv[]) { init(--argc, ++argv); loadfiles(files[0], files[1]); if (**argv != '-') { normal(files[0], files[1]); } - if (argc < 3) { printf("Usage: ./diff [options] file1 file2\n"); } - //if (!diffnormal) { normal(files[0], files[1]); printf("\n"); } if (showsidebyside) { sideside(files[0], files[1]); } if (showbrief) { quiet(files[0], files[1]); } if (report_identical) { loud(files[0], files[1]); } - if (!showversion && !showbrief && !ignorecase && !report_identical && - !diffnormal && !showsidebyside && !showleftcolumn && !suppresscommon && - !showcontext && !showunified && !showhelp) { printf("Usage: ./diff [options] file1 file2\n"); } + return 0; } @@ -70,7 +50,10 @@ void init(int argc, const char* argv[]) { ++argv; } + // ================================= // + if (showversion) { version(); exit(0); } + if (showhelp) { help(); exit(0); } if (!showcontext && !showunified && !showsidebyside && !showleftcolumn) { diffnormal = 1; } @@ -95,6 +78,8 @@ void diff_output_conflict_error(void) { void loadfiles(const char* filename1, const char* filename2) { + if (filename2 == NULL) { printf("Usage: ./diff [options] file1 file2\n"); exit(1); } + memset(buf, 0, sizeof(buf)); memset(strings1, 0, sizeof(strings1)); memset(strings2, 0, sizeof(strings2)); @@ -108,8 +93,6 @@ void loadfiles(const char* filename1, const char* filename2) { p = pa_first(strings1, count1); q = pa_first(strings2, count2); - int foundmatch = 0; - } void version() { @@ -130,27 +113,40 @@ void version() { printf("\tv0.5 alpha | Smoking is bad for your health.\n\n"); printf("Copyright (C) 2019 | All Rights Reserved.\n"); printf("Any unauthorized use or re-distribution of this code is permitted.\n\n"); - printf("\tChris Nutter\tWilliam McCarthy Rasputin\n\n\n"); + printf("\tChris Nutter\tWilliam McCarthy Rasputin\n\n\n"); } -void todo() { - printf("\nTODO: check line by line in a pagraph, using '|' for differences"); - printf("\nTODO: this starter code does not yet handle printing all of fin1's pagraphs."); - printf("\nTODO: handle the rest of diff's options"); - printf("\nTODO: fix standard printing with no pameters"); - printf("\nTODO: implement multiple types of pameters\n"); +void help() { + printf("\nUsage: diff-rcm [OPTION]... FILES\n"); + printf("Compare FILES line by line.\n\n"); + printf("Mandatory arguments to long options are mandatory for short options too.\n\n"); + printf("\t --normal\t\t output a normal diff (the default)\n"); + printf("\t-q, --brief\t\t report only when files differ\n"); + printf("\t-s, --report-identical-files report when two files are the same\n"); + printf("\t-c, -C NUM, --context[=NUM] output NUM (default 3) lines of copied context\n"); + printf("\t-u, -U NUM, --unified[=NUM] output NUM (default 3) lines of unified context\n"); + printf("\t-y, --side-by-side\t output in two columns\n\n"); + printf("\t-i, --ignore-case\t ignore case differences in file contents\n\n"); + printf("\t --help\t\t display this help and exit\n"); + printf("\t-v, --version\t\t output version information and exit\n\n"); + + printf("FILES are 'FILE1 FILE2'\n"); + printf("If --from-file or --to-file is given, there are no restrictions on FILE(s).\n"); + printf("If a FILE is '-', read standard input.\n"); + printf("Exit status is 0 if inputs are the same, 1 if different, 2 if trouble.\n\n"); + printf("Report bugs to: cdnutter@gmail.com\n"); + printf("diff-rcm homepage: \n\n"); } int normal(const char* filename1, const char* filename2) { printf("\nTHIS IS NOT NORMAL FOR NOW. THIS IS PLACEHOLDER. MMKAY.\n"); + printf("THIS IS NOT NORMAL FOR NOW. THIS IS PLACEHOLDER. MMKAY.\n"); + printf("THIS IS NOT NORMAL FOR NOW. THIS IS PLACEHOLDER. MMKAY.\n"); printf("THIS IS NOT NORMAL FOR NOW. THIS IS PLACEHOLDER. MMKAY.\n\n\n"); pa_print(p, printleft); pa_print(q, printright); - printf("\nTHIS IS NOT NORMAL FOR NOW. THIS IS PLACEHOLDER. MMKAY.\n"); - printf("THIS IS NOT NORMAL FOR NOW. THIS IS PLACEHOLDER. MMKAY.\n\n\n"); - return 0; } int sideside(const char* filename1, const char* filename2) { @@ -162,7 +158,7 @@ int sideside(const char* filename1, const char* filename2) { return 0; } -void quiet(const char* filename1, const char* filename2) { if (pa_equal(p, q) == 0) { printf("The files are not the same.\n"); } } +void quiet(const char* filename1, const char* filename2) { if (pa_equal(p, q) == 0) { printf("The files are not the same.\n"); } else { return; } } void loud(const char* filename1, const char* filename2) { if (pa_equal(p, q) != 0) { printf("The files are equal.\n"); } @@ -171,3 +167,11 @@ void loud(const char* filename1, const char* filename2) { } // ================================================================ // + +/* + TODO: check line by line in a pagraph, using '|' for differences"); + TODO: this starter code does not yet handle printing all of fin1's pagraphs."); + TODO: handle the rest of diff's options"); + TODO: fix standard printing with no pameters"); + TODO: implement multiple types of pameters\n"); +*/ diff --git a/diff.h b/diff.h index abd3b2c..8f9ba97 100644 --- a/diff.h +++ b/diff.h @@ -12,7 +12,7 @@ #define HASHLEN 200 #define BUFLEN 256 -// ======================================================================= // +// ================================================================= // FILE *fin1, *fin2; const char* files[2] = { NULL, NULL }; @@ -27,3 +27,19 @@ int showhelp = 0; int equal = 0, cnt = 0, count = 0; int count1 = 0, count2 = 0; + +// ================================================================ // + +void init(int argc, const char* argv[]); +void setoption(const char* arg, const char* s, const char* t, int* value); +void diff_output_conflict_error(void); + +void loadfiles(const char* filename1, const char* filename2); + +void version(); +void help(); + +int normal(const char* filename1, const char* filename2); +int sideside(const char* filename1, const char* filename2); +void quiet(const char* filename1, const char* filename2); +void loud(const char* filename1, const char* filename2); diff --git a/helptxt.txt b/helptxt.txt new file mode 100644 index 0000000..9347481 --- /dev/null +++ b/helptxt.txt @@ -0,0 +1,23 @@ +Usage: diff-rcm [OPTION]... FILES +Compare FILES line by line. + +Mandatory arguments to long options are mandatory for short options too. + --normal output a normal diff (the default) + -q, --brief report only when files differ + -s, --report-identical-files report when two files are the same + -c, -C NUM, --context[=NUM] output NUM (default 3) lines of copied context + -u, -U NUM, --unified[=NUM] output NUM (default 3) lines of unified context + -y, --side-by-side output in two columns + + -i, --ignore-case ignore case differences in file contents + + --help display this help and exit + -v, --version output version information and exit + +FILES are 'FILE1 FILE2' +If --from-file or --to-file is given, there are no restrictions on FILE(s). +If a FILE is '-', read standard input. +Exit status is 0 if inputs are the same, 1 if different, 2 if trouble. + +Report bugs to: cdnutter@gmail.com +diff-rcm home page: diff --git a/pa.c b/pa.c index 6bc08f3..79c839c 100644 --- a/pa.c +++ b/pa.c @@ -29,6 +29,7 @@ pa* pa_next(pa* p) { return pnew; } + void pa_print(pa* p, void (*fp)(const char*)) { if (p == NULL) { return; } for (int i = p->start; i <= p->stop && i != p->filesize; ++i) { fp(p->base[i]); } @@ -55,16 +56,19 @@ void printboth(const char* left_right) { buf[len - 1] = '\0'; printf("%-50s %s", buf, left_right); } + size_t pa_filesize(pa* p) { return p == NULL ? 0 : p->filesize; } size_t pa_size(pa* p) { return p == NULL || p->stop < p->start ? 0 : p->stop - p->start + 1; } + char** pa_base(pa* p) { return p->base; } -//char* yesorno(int condition) { return condition == 0 ? "no" : "YES"; } +char* yesorno(int condition) { return condition == 0 ? "no" : "YES"; } char* pa_info(pa* p) { static char buf[BUFLEN]; // static for a reason snprintf(buf, sizeof(buf), "base: %p, filesize: %d, start: %d, stop: %d\n", p->base, p->filesize, p->start, p->stop); return buf; // buf MUST be static } + int pa_equal(pa* p, pa* q) { if (p == NULL || q == NULL) { return 0; } if (pa_size(p) != pa_size(q)) { return 0; } @@ -72,6 +76,7 @@ int pa_equal(pa* p, pa* q) { while ((equal = strcmp(p->base[i], q->base[i])) == 0) { ++i; ++j; } return equal; } + FILE* openfile(const char* filename, const char* openflags) { FILE* f; if ((f = fopen(filename, openflags)) == NULL) { printf("can't open '%s'\n", filename); exit(1); }