Xit/xor.c

33 lines
786 B
C
Raw Normal View History

#include <stdio.h>
int main(int argc, char **argv) {
if (argc < 2) {
fprintf(stderr, "Usage: xor [FILE]...\nAll files XORed to stdout, size: minimum of all.\n");
return(1);
}
int nbFiles = argc-1;
FILE *files[nbFiles];
bool errors = false;
for (int i=0;i<nbFiles;i++) {
files[i] = fopen(argv[i+1], "r");
if (files[i] == NULL) {
fprintf(stderr, "Could not open '%s'.\n", argv[i+1]);
errors = true;
}
}
if (errors) return(1);
char c[argc-1];
do {
int cc;
for (int i=0;i<nbFiles;i++) {
cc = fgetc(files[i]);
if (cc == EOF) break;
c[i] = (char)cc;
}
if (cc == EOF) break;
char xored = '\0';
for (int i=0;i<nbFiles;i++) xored ^= c[i];
printf("%c", xored);
} while(1);
}