James G. Sack (jim) wrote:
> Christoph Maier wrote:
>..
>..my little program (attached).


I guess I got bitten again by improperly config'd thunderbird for
sending attachments. I can't now find the magic option to embed
attachments automatically, so I'll just paste it in

So, here's another try.
= = = = = = = = = = = =

/* mycmp.c
 * mycmp orig copy
 * compares orig to copy
 *  compares at most len(orig) bytes of both files
 *  quits at first difference
 *  reports whether 2nd has 'left-overs'
 *
 * compile via
 * -----------
 *   make mycmp
 * or
 *   gcc -Wall -o mycmp mycmp.c
 * or on x86_64, to create a 32bit version
 *   gcc -m32 -Wall -o mycmp32 mycmp.c
 *
 * This code released to the public domain
 *  by James G. Sack (jim) 2006.11.21
 ****************************************************/

#include <stdio.h>
#include <stdlib.h>     // for exit()
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>      // for O_RDONLY
#include <string.h>     // for memcmp()
#include <unistd.h>     // for read()

#define SIZ 64*1024
unsigned char
 ibuf[SIZ],
 dbuf[SIZ];

/* needed? */
#ifdef O_LARGEFILE
# define FLAGS (O_RDONLY | O_LARGEFILE)
#else
# define FLAGS O_RDONLY
#endif


int main(int ac, char* av[])
{
    int ifil, dfil;
    ssize_t isiz, dsiz;
    long count;

    if (ac < 3) {
        printf("Usage: mycmp origfile copy\n");
        exit(-1);
    }
    ifil = open(av[1], FLAGS);
    if (ifil < 0) {
        printf("ERR: Cannot open original '%s' for input\n", av[1]);
        exit(99);
    }
    dfil = open(av[2], FLAGS);
    if (dfil < 0) {
        printf("ERR: Cannot open copy '%s' for input\n", av[2]);
        exit(98);
    }

    count = 0;
    do {
        isiz = read(ifil, ibuf, SIZ);
        dsiz = read(dfil, dbuf, SIZ);
        if (isiz) {
            //ASSERT: got data in ibuf, isiz > 0
            if (dsiz < isiz) {
                printf("FAIL: orig(%s) is longer than copy(%s); "
                    "copy has premature EOF at %ld bytes\n",
                    av[1], av[2], dsiz + count*SIZ);
                exit(2);
            }
            //ASSERT: data in dbuf; dsiz >= isiz
            // (could now hunt for 1st diff,
            //  or write a memcmp replacement)
            if (memcmp(ibuf,dbuf,isiz)){
                printf("FAIL: orig(%s) and copy(%s) mismatch "
                    "(at/after) %ld bytes\n",
                    av[1], av[2], count*SIZ);
                exit(1);
            }
            ++count;
        }else{
            //ASSERT: eof on original
            ; //nop -- but see dsiz test below
        }
        if (dsiz > isiz){
            printf("FAIL/CURIOUS: orig(%s) shorter than copy(%s),  "
                "but matches up to orig EOF at %ld bytes\n",
                av[1], av[2], isiz + (count-1)*SIZ);
            exit(3);
        }
    }while (isiz > 0);
    return 0;
}

/*===eof===*/

-- 
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-newbie

Reply via email to