home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 6 File
/
06-File.zip
/
CONVERT.ZIP
/
CONVERT.C
next >
Wrap
C/C++ Source or Header
|
1991-11-16
|
3KB
|
118 lines
#include <stdio.h>
#define INCL_NOPM
#define INCL_BASE
#include <os2.h>
#include <ctype.h>
#ifndef MI_386
#define DosQueryFileInfo DosQFileInfo
#endif
FILE
*fIn,
*fOut;
USHORT
usDir,
usRslt;
#define FROM_UNIX 0
#define TO_UNIX 1
#define FILE_INFO_1 1
char
*szOutName,
*szTempName = "c02453rt.9qz",
*szOpenMode[] = { "w", "wb" };
FILESTATUS2
fsIn,
fsOut;
void
usage(char *);
int main(int argc, char *argv[])
{
int
c;
if (argc < 3 || argv[1][0] != '-')
{
usage(argv[0]);
return 1;
}
switch(argv[1][1])
{
case 'u':
case 'U':
usDir = TO_UNIX;
break;
case 'o':
case 'O':
case 'd':
case 'D':
case 'p':
case 'P':
usDir = FROM_UNIX;
break;
default:
usage(argv[0]);
return 1;
}
if ((fIn = fopen(argv[2], "r")) == NULL)
{
fprintf(stderr, "Unable to open input file %s\n", argv[2]);
return 2;
}
if (DosQueryFileInfo(fileno(fIn), FILE_INFO_1, &fsIn, sizeof fsIn))
{
fclose(fIn);
fprintf(stderr, "Error fetching file information for %s\n", argv[2]);
return 3;
}
szOutName = argc > 3 ? argv[3] : szTempName;
if ((fOut = fopen(szOutName, szOpenMode[usDir])) == NULL)
{
fclose(fIn);
fprintf(stderr, "Unable to open output file %s\n", szOutName);
return 2;
}
while ((c = fgetc(fIn)) != EOF)
fputc(c, fOut);
fclose(fIn);
fclose(fOut);
fOut = fopen(szOutName, "r+b");
if (DosQueryFileInfo(fileno(fOut), FILE_INFO_1, &fsOut, sizeof fsOut))
{
fclose(fOut);
fprintf(stderr, "Error fetching file information for %s\n", szOutName);
return 3;
}
fsOut.fdateCreation = fsIn.fdateCreation;
fsOut.ftimeCreation = fsIn.ftimeCreation;
fsOut.fdateLastAccess = fsIn.fdateLastAccess;
fsOut.ftimeLastAccess = fsIn.ftimeLastAccess;
fsOut.fdateLastWrite = fsIn.fdateLastWrite;
fsOut.ftimeLastWrite = fsIn.ftimeLastWrite;
if (DosSetFileInfo(fileno(fOut), FILE_INFO_1, &fsOut, sizeof fsOut))
{
fclose(fOut);
fprintf(stderr, "Error setting file information for %s\n", szOutName);
return 3;
}
fclose(fOut);
if (argc < 3) // then it was an in-place conversion
{
unlink(argv[2]);
rename(szOutName, argv[2]);
}
}
void usage(char *n)
{
char use[] =
"Usage:\t\t%s <direction> <input file> [<output file>]\n\n"
"<direction> is -u if converting to UNIX format\n"
" -p or -d or -o if converting to PC-DOS-OS/2 format.\n\n"
"<input file> must be specified. <output file> is optional and if\n"
"not given then <input file> will be replaced by the converted file.\n"
"In any case the converted file will have the same time stamps as\n"
"the original but the length will probably be different.\n";
fprintf(stderr, use, n);
}