home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 6 File
/
06-File.zip
/
lftocrlf.lzh
/
LFTOCRLF.C
< prev
next >
Wrap
Text File
|
1993-11-03
|
9KB
|
203 lines
/************************************************************************
* Program: LFtoCRLF.C *
* *
* Program to convert Unix files (each logical record terminated by LF) *
* to DOS and OS/2 files (each logical record terminated by CR/LF). *
* *
* This program is to be run from a command prompt, either in an OS/2 *
* full-screen session or an OS/2 Window. *
* *
* The user can supply the input and output file paths/names on the *
* command line, as follows: *
* *
* LFtoCRLF <Input file> <Output file> *
* *
* where <Input file> is the path and file name of the Unix delimited *
* input file, and <Output file> is the path and file name of the OS/2 *
* file to be created. If the path information is omitted from the *
* output file then it is created in the same directory as the input *
* file. *
* *
* If the user does not put any file specifications on the command line *
* the program will prompt for them individually. Again, if the output *
* file has no path information it will be created in the same directory*
* as the input file currently resides. *
* *
* This program was compiled and tested using the IBM CSet/++ compiler. *
* I have kept the C fairly standard and so it should port to any other *
* compiler with a minimum of reworking. *
* *
* The author retains all copyrights on the code, but expressly permits *
* its distribution provided no charge is levied beyond that of the *
* medium of its distribution. (I.e. it's a freebie!) *
* *
* *
* *
* (C) 1993 Copyright David W. Noon *
* 13583 Lynn St. *
* Woodbridge VA 22191-2121 *
* Phone: (703) 490-5652 *
* Fax: (703) 491-6390 *
* Compuserve: 72172,431 *
* *
************************************************************************/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define Buffer_size 8192
/* Internal subroutine to merge the drive and directory of the input file
into the output file specification if they were omitted. */
void merge_path(char *Input_file, char *Output_file)
{
char Input_drive[_MAX_DRIVE], Input_dir[_MAX_DIR],
Input_fname[_MAX_FNAME], Input_ext[_MAX_EXT],
Output_drive[_MAX_DRIVE], Output_dir[_MAX_DIR],
Output_fname[_MAX_FNAME], Output_ext[_MAX_EXT];
_splitpath(Input_file, Input_drive, Input_dir, Input_fname, Input_ext);
_splitpath(Output_file, Output_drive, Output_dir, Output_fname, Output_ext);
if (strcmp(Output_drive, "") == 0) /* No drive given */
strcpy(Output_drive, Input_drive);
if (strcmp(Output_dir, "") == 0) /* No directory given */
strcpy(Output_dir, Input_dir);
_makepath(Output_file, Output_drive, Output_dir, Output_fname, Output_ext);
}
int main(int argc, char *argv[])
{
unsigned long int Input_byte_count = 0L, Output_byte_count = 0L;
unsigned char current_byte;
short int Add_CR = 1;
static const unsigned char CR = 0x0D, LF = 0x0A;
char Input_file[_MAX_PATH] = "", Output_file[_MAX_PATH] = "";
FILE *ifp = NULL, *ofp = NULL;
setvbuf(stdout, NULL, _IONBF, 0); /* Make stdout unbuffered */
printf("\nUnix-to-OS/2 File Conversion\n");
switch(argc)
{
case 1: /* Only the command name was given */
while(ifp == NULL)
{
/* Obtain the path/name of the Unix file to be read */
printf("\nEnter path/name of input file: ");
fflush(stdin); /* Clear out any junk from last read */
scanf("%[^\r\n]", &Input_file);
if (strlen(Input_file) == 0) /* A null response will mean stop */
exit(EXIT_FAILURE); /* Take a quick way out */
ifp = fopen(Input_file, "rb"); /* The file must exist or else! */
if (ifp == NULL) /* File not found? */
printf("Unable to open file %s. Please try again.", Input_file);
}
while(ofp == NULL)
{
/* Obtain the path/name of the OS/2 file to be written */
printf("\nEnter path/name of output file: ");
fflush(stdin); /* Clear out any junk from last read */
scanf("%[^\r\n]", &Output_file);
if (strlen(Output_file) == 0) /* A null response will mean stop */
{
fclose(ifp); /* close what we've opened */
exit(EXIT_FAILURE); /* Take a quick way out */
}
merge_path(Input_file, Output_file);
ofp = fopen(Output_file, "wb"); /* Open will create it, if necessary */
if (ofp == NULL) /* Unable to create file? */
printf("Unable to open file %s. Please try again.", Output_file);
}
break;
case 3: /* Both input and output files were given */
strcpy(Input_file,argv[1]);
ifp = fopen(Input_file,"rb");
if (ifp == NULL) /* File not found? */
{
printf("Unable to open file %s.\n", Input_file);
exit(EXIT_FAILURE);
}
strcpy(Output_file,argv[2]);
merge_path(Input_file, Output_file);
ofp = fopen(Output_file,"wb");
if (ofp == NULL) /* Unable to create file? */
{
printf("Unable to open file %s.\n", Output_file);
fclose(ifp); /* Clean up what we've opened */
exit(EXIT_FAILURE);
}
break;
default : /* No other combinations are valid */
printf("Invalid parameter list. Your command line should be:"
"\n\tLFtoCRLF <Input file path/name> <Output file path/name>");
exit(EXIT_FAILURE);
break;
}
#if Buffer_size > 0
setvbuf(ifp, NULL, _IOFBF, Buffer_size); /* Establish buffering for input */
setvbuf(ofp, NULL, _IOFBF, Buffer_size); /* Establish buffering for output */
#endif
fread(¤t_byte, 1, 1, ifp); /* Read first byte of file. Beware EOF! */
while(feof(ifp) == 0) /* Check for EOF */
{
Input_byte_count++; /* keep the accounting straight */
if (current_byte == LF) /* LF is Unix delimiter */
{
if (Add_CR) /* then a CR is needed */
{
/* Compatibilty note: some editors require that each LF
should have a CR before it. There is no such requirement
in the DOS or OS/2 specifications, but if you want a CR
before each and every LF then the Add_CR flag should always
be non-zero, so comment out the next statement. */
Add_CR = 0; /* suppress further CR's until non-LF is read */
fwrite(&CR, 1, 1, ofp); /* Insert a CR */
Output_byte_count++;
}
}
else
Add_CR = 1; /* a non-LF means next LF needs a CR */
fwrite(¤t_byte, 1, 1, ofp); /* write out the byte read from input */
Output_byte_count++;
fread(¤t_byte, 1, 1, ifp); /* get the next byte and check for EOF */
}
/* Close both files. */
fclose(ifp);
fclose(ofp);
printf("\nNo. of bytes read from %s was: %lu\nNo. of bytes written to %s was: %lu\n",
Input_file, Input_byte_count, Output_file, Output_byte_count);
return 0;
}