Skip to content

Instantly share code, notes, and snippets.

@aynik
Created June 13, 2022 11:25
Show Gist options
  • Save aynik/72654196521e912b0c13671f03e3ace5 to your computer and use it in GitHub Desktop.
Save aynik/72654196521e912b0c13671f03e3ace5 to your computer and use it in GitHub Desktop.
mix-bytes
#!/usr/bin/env C -m
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
void mix_bytes(const char *in_even_fname, const char *in_odd_fname, int bytes)
{
int c;
FILE *fin_odd, *fin_even;
fin_even = fopen(in_even_fname, "rb");
if (!fin_even)
{
fprintf(stderr, "Couldn't open %s.\n", in_even_fname);
return;
}
fin_odd = fopen(in_odd_fname, "rb");
if (!fin_odd)
{
fprintf(stderr, "Couldn't open %s.\n", in_odd_fname);
fclose(fin_even);
return;
}
do
{
for (int i = 0; i < bytes; i++)
{
c = fgetc(fin_even);
if (c != EOF)
{
fputc(c, stdout);
}
}
for (int i = 0; i < bytes; i++)
{
c = fgetc(fin_odd);
if (c != EOF)
{
fputc(c, stdout);
}
}
} while (c != EOF);
fclose(fin_even);
fclose(fin_odd);
}
void usage()
{
printf("Usage: mix-bytes\n");
printf(" mix-bytes in.even in.odd <bytes per> > out\n");
}
int main(int argc, char **argv)
{
int bytes = 1;
if (argc > 3) {
bytes = atoi(argv[3]);
}
if (argc < 3)
{
usage();
return 0;
}
mix_bytes(argv[1], argv[2], bytes);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment