Skip to content

Instantly share code, notes, and snippets.

@lnicola
Created June 20, 2014 16:08
Show Gist options
  • Save lnicola/bcebadff22a5a4bbe88f to your computer and use it in GitHub Desktop.
Save lnicola/bcebadff22a5a4bbe88f to your computer and use it in GitHub Desktop.
small tool to recover some files clobbered by ASCII mode conversions
#include <stdio.h>
int main(int argc, char **argv)
{
FILE *fi, *fo;
char c1, c2;
if (argc != 3)
{
printf("Usage: %s <in> <out>\n", argv[0]);
return 1;
}
fi = fopen(argv[1], "rb");
fo = fopen(argv[2], "wb");
while (!feof(fi))
{
fscanf(fi, "%c", &c1);
if (feof(fi))
break;
if (c1 != '\x0d' || feof(fi))
{
fprintf(fo, "%c", c1);
continue;
}
loop:
fscanf(fi, "%c", &c2);
if (c2 != '\x0d' || feof(fi))
{
if (c2 != '\x0a')
fprintf(fo, "%c", c1);
fprintf(fo, "%c", c2);
}
else
if (c2 =='\x0d')
{
fprintf(fo, "%c", c1);
c1 = c2;
goto loop;
}
}
fclose(fi);
fclose(fo);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment