/* This program will convert all LFs to CRLFs */ /* Will also expand all Tabs to spaces */ /* Usage: */ /* addcr filespec */ #include #define TAB 9 #define LF 10 #define CR 13 void Leave(char *Path) { perror(Path); exit(1); } void main(int argc, char *argv[]) { FILE *FilePtr, *TempFile; int Col, I; char C, PC; char *TempName = (char *)malloc(80); char *Path = (char *)malloc(80); /* If a command line argument was passed */ if (argc > 1) { strcpy(Path, argv[1]); } else /* Fail */ { printf("Must specify input file\n"); exit(0); } /* Open file to be converted */ FilePtr = fopen(Path, "rb+"); if (!FilePtr) { Leave(Path); } /* Set buffer size to that of a cluster */ setvbuf(FilePtr, (char *)malloc(4096), _IOFBF, 4096); /* Open temporary file */ TempFile = fopen(tmpnam(TempName), "wb+"); if (!TempFile) { Leave("Temporary File"); } /* Set buffer size to that of a cluster */ setvbuf(TempFile, (char *)malloc(4096), _IOFBF, 4096); PC = '\0'; Col = 0; /* Copy from InFile to Temporary file */ while((C = fgetc(FilePtr)) != EOF) { /* If this is a Line Feed, and there wasn't a Carriage Return before it */ /* add a Carriage Return */ if (C == LF) { if (!(PC == CR)) fputc(CR, TempFile); Col = -1; } /* Handle Tabs */ if (C == TAB) { for (I = 0; I < 8-Col; I++) fputc(' ', TempFile); Col = -1; } else fputc(C, TempFile); PC = C; Col = (Col + 1) % 8; } /* Reset both file pointers to the top */ rewind(FilePtr); rewind(TempFile); /* Copy from Temporary to original file */ while((C = fgetc(TempFile)) != EOF) fputc(C, FilePtr); fclose(TempFile); fclose(FilePtr); /* Delete the temporary file */ remove(TempName); }