Created
June 10, 2011 16:30
-
-
Save m039/1019217 to your computer and use it in GitHub Desktop.
Convert file from mac(\r)/unix(\n)/dos(\r\n) formats to the unix format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Author: m039 <flam44 (at) gmail (dot) com> */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
static FILE *src; | |
static FILE *dst; | |
int main(int argc, char **argv) { | |
int c; | |
if (argc < 3) { | |
fprintf(stderr, "Usage: ./tounix <from> <to>\n"); | |
return -1; | |
} | |
src = fopen(argv[1], "r"); | |
if (src == NULL) { | |
perror(argv[1]); | |
return -1; | |
} | |
dst = fopen(argv[2], "w"); | |
if (dst == NULL) { | |
perror(argv[2]); | |
return -1; | |
} | |
while ((c = fgetc(src)) != EOF) { | |
do { | |
if (c == '\r') { | |
fputc('\n', dst); | |
c = fgetc(src); | |
if (c == EOF) { | |
goto out; | |
} | |
if ((c != '\n') && (c != '\r')) { | |
fputc(c, dst); | |
} | |
} else { | |
fputc(c, dst); | |
} | |
} while (c == '\r'); | |
} | |
out: | |
fclose(dst); | |
fclose(src); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment