/* * This program strips the CRs from a file. MSDOS uses CRLF for * \n while Unix uses only a LF. In addition, ^Z characters are * stripped from the input file. Unix does not use ^Z to mark the * EOF, while some MSDOS editors put a ^Z at the end of a file. * ^D characters are stripped from the file also. Some PC products * which produce Postscript output put ^Ds in the file. Some * Postscript printers will complain about these files and refuse * to print them. */ #include #include #include #define CTRL_D '\004' /* EOT */ #define CTRL_M '\015' /* CR */ #define CTRL_Z '\032' /* SUB */ int main(int argc, char **argv) { FILE *infp; FILE *outfp; char buffer[2]; if (argc > 2) { /* don't translate CRLF into LF when reading */ if ((infp = fopen(argv[1], "rb")) == NULL) { // perror(strerror(errno)); fprintf(stderr, "Open file %s has errno %d %s\n", argv[1], errno, strerror(errno)); return 1; } /* don't translate LF into CRLF when writing */ if ((outfp = fopen(argv[2], "wb")) == NULL) { // perror(strerror(errno)); fprintf(stderr, "Open file %s has errno %d %s\n", argv[2], errno, strerror(errno)); return 1; } } else { fprintf(stderr, "Usage: dos2unix [infile] [outfile]\n"); return 2; } while (fread(buffer, sizeof (char), 1, infp) == 1) if ( (buffer[0] != CTRL_D) && (buffer[0] != CTRL_M) && (buffer[0] != CTRL_Z) ) (void) fputc(buffer[0], outfp); return 0; }