home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / CONVERT.ZIP / CONVERT.C next >
C/C++ Source or Header  |  1991-11-16  |  3KB  |  118 lines

  1. #include <stdio.h>
  2. #define INCL_NOPM
  3. #define INCL_BASE
  4. #include <os2.h>
  5. #include <ctype.h>
  6.  
  7. #ifndef MI_386
  8. #define DosQueryFileInfo DosQFileInfo
  9. #endif
  10.  
  11. FILE
  12.     *fIn,
  13.     *fOut;
  14. USHORT
  15.     usDir,
  16.     usRslt;
  17. #define FROM_UNIX    0
  18. #define TO_UNIX        1
  19. #define FILE_INFO_1    1
  20. char
  21.     *szOutName,
  22.     *szTempName = "c02453rt.9qz",
  23.     *szOpenMode[] = { "w", "wb" };
  24. FILESTATUS2
  25.     fsIn,
  26.     fsOut;
  27. void
  28.     usage(char *);
  29.  
  30. int main(int argc, char *argv[])
  31. {
  32.     int
  33.         c;
  34.     if (argc < 3 || argv[1][0] != '-')
  35.     {
  36.         usage(argv[0]);
  37.         return 1;
  38.     }
  39.     switch(argv[1][1])
  40.     {
  41. case 'u':
  42. case 'U':
  43.         usDir = TO_UNIX;
  44.         break;
  45. case 'o':
  46. case 'O':
  47. case 'd':
  48. case 'D':
  49. case 'p':
  50. case 'P':
  51.         usDir = FROM_UNIX;
  52.         break;
  53. default:
  54.         usage(argv[0]);
  55.         return 1;
  56.     }
  57.     if ((fIn = fopen(argv[2], "r")) == NULL)
  58.     {
  59.         fprintf(stderr, "Unable to open input file %s\n", argv[2]);
  60.         return 2;
  61.     }
  62.     if (DosQueryFileInfo(fileno(fIn), FILE_INFO_1, &fsIn, sizeof fsIn))
  63.     {
  64.         fclose(fIn);
  65.         fprintf(stderr, "Error fetching file information for %s\n", argv[2]);
  66.         return 3;
  67.     }
  68.     szOutName = argc > 3 ? argv[3] : szTempName;
  69.     if ((fOut = fopen(szOutName, szOpenMode[usDir])) == NULL)
  70.     {
  71.         fclose(fIn);
  72.         fprintf(stderr, "Unable to open output file %s\n", szOutName);
  73.         return 2;
  74.     }
  75.     while ((c = fgetc(fIn)) != EOF)
  76.         fputc(c, fOut);
  77.     fclose(fIn);
  78.     fclose(fOut);
  79.     fOut = fopen(szOutName, "r+b");
  80.     if (DosQueryFileInfo(fileno(fOut), FILE_INFO_1, &fsOut, sizeof fsOut))
  81.     {
  82.         fclose(fOut);
  83.         fprintf(stderr, "Error fetching file information for %s\n", szOutName);
  84.         return 3;
  85.     }
  86.     fsOut.fdateCreation = fsIn.fdateCreation;
  87.     fsOut.ftimeCreation = fsIn.ftimeCreation;
  88.     fsOut.fdateLastAccess = fsIn.fdateLastAccess;
  89.     fsOut.ftimeLastAccess = fsIn.ftimeLastAccess;
  90.     fsOut.fdateLastWrite = fsIn.fdateLastWrite;
  91.     fsOut.ftimeLastWrite = fsIn.ftimeLastWrite;
  92.     if (DosSetFileInfo(fileno(fOut), FILE_INFO_1, &fsOut, sizeof fsOut))
  93.     {
  94.         fclose(fOut);
  95.         fprintf(stderr, "Error setting file information for %s\n", szOutName);
  96.         return 3;
  97.     }
  98.     fclose(fOut);
  99.     if (argc < 3) // then it was an in-place conversion
  100.     {
  101.         unlink(argv[2]);
  102.         rename(szOutName, argv[2]);
  103.     }
  104. }
  105.  
  106. void usage(char *n)
  107. {
  108.     char use[] =
  109.         "Usage:\t\t%s <direction> <input file> [<output file>]\n\n"
  110.         "<direction> is -u if converting to UNIX format\n"
  111.         "               -p or -d or -o if converting to PC-DOS-OS/2 format.\n\n"
  112.         "<input file> must be specified.  <output file> is optional and if\n"
  113.         "not given then <input file> will be replaced by the converted file.\n"
  114.         "In any case the converted file will have the same time stamps as\n"
  115.         "the original but the length will probably be different.\n";
  116.     fprintf(stderr, use, n);
  117. }
  118.