123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- /*++
- Copyright (c) 2013 Minoca Corp. All Rights Reserved
- Module Name:
- cmp.c
- Abstract:
- This module implements the cmp (compare utility).
- Author:
- Evan Green 4-Sep-2013
- Environment:
- POSIX
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <minoca/lib/types.h>
- #include <assert.h>
- #include <errno.h>
- #include <getopt.h>
- #include <libgen.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include "swlib.h"
- //
- // ---------------------------------------------------------------- Definitions
- //
- #define CMP_VERSION_MAJOR 1
- #define CMP_VERSION_MINOR 0
- #define CMP_USAGE \
- "usage: cmp [-l | -s] file1 file2\n" \
- "The cmp utility compares two files. It writes no output if both files \n" \
- "are the same. Under default options, it writes the byte and line \n" \
- "number at which the first difference occurred. Options are:\n" \
- " -l, --verbose -- Write the byte number (decimal) and the differing \n" \
- " bytes (octal) for each difference.\n" \
- " -s, --quiet, --silent -- Write nothing for differing files. Return \n" \
- " exit status only.\n" \
- " --help -- Show this help text and exit.\n" \
- " --version -- Show the application version information and exit.\n\n" \
- "The operands are paths to files to compare. If - is supplied for \n" \
- "either file, standard in will be used.\n" \
- "The cmp utility returns 0 if the files are identical, 1 if the files \n" \
- "are different or of different size, and >1 if an error occurred.\n\n"
- #define CMP_OPTIONS_STRING "ls"
- //
- // Define cmp options.
- //
- //
- // Set this option to print each byte difference.
- //
- #define CMP_OPTION_VERBOSE 0x00000001
- //
- // Set this option to print nothing.
- //
- #define CMP_OPTION_SILENT 0x00000002
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- //
- // -------------------------------------------------------------------- Globals
- //
- struct option CmpLongOptions[] = {
- {"verbose", no_argument, 0, 'l'},
- {"quiet", no_argument, 0, 's'},
- {"silent", no_argument, 0, 's'},
- {"help", no_argument, 0, 'h'},
- {"version", no_argument, 0, 'V'},
- {NULL, 0, 0, 0},
- };
- //
- // ------------------------------------------------------------------ Functions
- //
- INT
- CmpMain (
- INT ArgumentCount,
- CHAR **Arguments
- )
- /*++
- Routine Description:
- This routine is the main entry point for the cmp (compare) utility.
- Arguments:
- ArgumentCount - Supplies the number of command line arguments the program
- was invoked with.
- Arguments - Supplies a tokenized array of command line arguments.
- Return Value:
- Returns an integer exit code. 0 for success, nonzero otherwise.
- --*/
- {
- ULONG ArgumentIndex;
- INT Character1;
- INT Character2;
- ULONGLONG CharacterNumber;
- FILE *File1;
- FILE *File2;
- ULONGLONG LineNumber;
- INT Option;
- ULONG Options;
- PSTR Path1;
- PSTR Path2;
- ULONG SourceCount;
- int Status;
- File1 = NULL;
- File2 = NULL;
- SourceCount = 0;
- Options = COPY_OPTION_FOLLOW_OPERAND_LINKS;
- //
- // Process the control arguments.
- //
- while (TRUE) {
- Option = getopt_long(ArgumentCount,
- Arguments,
- CMP_OPTIONS_STRING,
- CmpLongOptions,
- NULL);
- if (Option == -1) {
- break;
- }
- if ((Option == '?') || (Option == ':')) {
- Status = 2;
- goto MainEnd;
- }
- switch (Option) {
- case 'l':
- Options |= CMP_OPTION_VERBOSE;
- Options &= ~CMP_OPTION_SILENT;
- break;
- case 's':
- Options |= CMP_OPTION_SILENT;
- Options &= ~CMP_OPTION_VERBOSE;
- break;
- case 'V':
- SwPrintVersion(CMP_VERSION_MAJOR, CMP_VERSION_MINOR);
- return 2;
- case 'h':
- printf(CMP_USAGE);
- return 2;
- default:
- assert(FALSE);
- Status = 2;
- goto MainEnd;
- }
- }
- ArgumentIndex = optind;
- if (ArgumentIndex > ArgumentCount) {
- ArgumentIndex = ArgumentCount;
- }
- SourceCount = ArgumentCount - ArgumentIndex;
- //
- // Fail if there were not enough arguments.
- //
- if ((SourceCount != 2) && (SourceCount != 1)) {
- SwPrintError(0,
- NULL,
- "One or two arguments expected. Try --help for usage");
- return 2;
- }
- Path1 = Arguments[ArgumentIndex];
- Path2 = NULL;
- if (SourceCount == 2) {
- Path2 = Arguments[ArgumentIndex + 1];
- }
- //
- // Open up the files.
- //
- if (strcmp(Path1, "-") == 0) {
- Path1 = "<stdin>";
- File1 = stdin;
- } else {
- File1 = fopen(Path1, "rb");
- if (File1 == NULL) {
- Status = errno;
- SwPrintError(Status, Path1, "Unable to open");
- goto MainEnd;
- }
- }
- if ((Path2 == NULL) || (strcmp(Path2, "-") == 0)) {
- Path2 = "<stdin>";
- File2 = stdin;
- } else {
- File2 = fopen(Path2, "rb");
- if (File2 == NULL) {
- Status = errno;
- SwPrintError(Status, Path2, "Unable to open");
- goto MainEnd;
- }
- }
- //
- // If standard in was selected for anything, then change standard in to
- // binary mode.
- //
- if ((File1 == stdin) || (File2 == stdin)) {
- Status = SwSetBinaryMode(fileno(stdin), TRUE);
- if (Status != 0) {
- SwPrintError(Status, NULL, "Failed to set stdin binary mode.");
- goto MainEnd;
- }
- }
- //
- // Perform the comparison.
- //
- CharacterNumber = 1;
- LineNumber = 1;
- Status = 0;
- while (TRUE) {
- Character1 = fgetc(File1);
- Character2 = fgetc(File2);
- //
- // Handle one or both of the files ending.
- //
- if (Character1 == EOF) {
- if (Character2 != EOF) {
- if ((Options & CMP_OPTION_SILENT) == 0) {
- fprintf(stderr, "cmp: EOF on %s\n", Path1);
- }
- Status = 1;
- }
- break;
- } else if (Character2 == EOF) {
- if ((Options & CMP_OPTION_SILENT) == 0) {
- fprintf(stderr, "cmp: EOF on %s\n", Path2);
- }
- Status = 1;
- break;
- }
- //
- // Neither character is EOF. If they're different, report it.
- //
- if (Character1 != Character2) {
- Status = 1;
- if ((Options & CMP_OPTION_VERBOSE) != 0) {
- printf("%I64d %o %o\n",
- CharacterNumber,
- Character1,
- Character2);
- } else {
- if ((Options & CMP_OPTION_SILENT) == 0) {
- printf("%s %s differ: char %I64d, line %I64d\n",
- Path1,
- Path2,
- CharacterNumber,
- LineNumber);
- }
- break;
- }
- }
- //
- // Advance the character and line numbers.
- //
- CharacterNumber += 1;
- if (Character1 == '\n') {
- LineNumber += 1;
- }
- }
- //
- // Return an error if either of the streams are funky.
- //
- if ((ferror(File1) != 0) || (ferror(File2) != 0)) {
- Status = errno;
- if (Status == 0) {
- Status = 2;
- }
- }
- MainEnd:
- if ((File1 != NULL) && (File1 != stdin)) {
- fclose(File1);
- }
- if ((File2 != NULL) && (File2 != stdin)) {
- fclose(File2);
- }
- return Status;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
|