home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / cvt304.zip / CVT.C next >
Text File  |  2002-08-24  |  25KB  |  901 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /* Convert one or more files from Unix format (LF) to DOS format (CR/LF)    */
  4. /*                                                                          */
  5. /* Note: Wildcards are supported and files are updated in place.            */
  6. /*                                                                          */
  7. /* This work is released into the public domain by the author.              */
  8. /*                                                                          */
  9. /*                                                                          */
  10. /* 08/02/92    Bob Withers           Program originally complete.           */
  11. /*                                                                          */
  12. /* 09/24/92    Bob Withers           Added search subdirs feature.          */
  13. /*                                                                          */
  14. /* 10/02/93    Bob Withers           Added multiple format conversion.      */
  15. /*                                                                          */
  16. /* 07/20/94    Bob Withers           Converted to Unix style dir routines   */
  17. /*                                   and tested code under Unix SVR3.2      */
  18. /*                                                                          */
  19. /* 08/23/94    Bob Withers           Added Ctrl-Z to tests for valid text   */
  20. /*                                   files.  Ctrl-Z at end of DOS files     */
  21. /*                                   was causing them to be treated as      */
  22. /*                                   binary.  Also, remove Ctrl-Z when      */
  23. /*                                   converting from DOS.                   */
  24. /*                                                                          */
  25. /* 12/26/95    Bob Withers           Added #ifdef checks for NEED_STRERROR  */
  26. /*                                   per Dale DePriest daled@cadence.com.   */
  27. /*                                   Added -8 switch to permit conversion   */
  28. /*                                   of files with char values > 127 per    */
  29. /*                                   Stefan Witzel switzel@gwdg.de.         */
  30. /*                                                                          */
  31. /* 02/19/96    Bob Withers           Force filenames to uppercase if not    */
  32. /*                                   running on Unix.  This will allow      */
  33. /*                                   all matches to be found under NT which */
  34. /*                                   treats file access as case insensitive */
  35. /*                                   but may return names in mixed case.    */
  36. /*                                                                          */
  37. /* 02/21/96    Bob Withers           Corrected error in recursive directory */
  38. /*                                   processing that prevented it from      */
  39. /*                                   working properly.  Fixed bug in alloc  */
  40. /*                                   of memory to store directory names.    */
  41. /*                                   Added general purpose Err() function   */
  42. /*                                   to handle display of terminal errors.  */
  43. /*                                                                          */
  44. /****************************************************************************/
  45.  
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <sys/types.h>
  50. #include <sys/stat.h>
  51.  
  52. #ifdef UNIX
  53. #include <unistd.h>
  54. #include <malloc.h>
  55. #include <dirent.h>
  56. #define READ_BIN                "r"
  57. #define WRITE_BIN               "w"
  58. #define PATH_SEP                '/'
  59. #define PATH_SEP_STR            "/"
  60. #define ANSI_C                  0
  61. #else
  62. #include "dirent.h"
  63. #include <ctype.h>
  64. #define READ_BIN                "rb"
  65. #define WRITE_BIN               "wb"
  66. #define PATH_SEP                '\\'
  67. #define PATH_SEP_STR            "\\"
  68. #define ANSI_C                  1
  69. #endif
  70.  
  71. #if ANSI_C
  72. #define USE_PROTO               1
  73. #define PROTO(x)                x
  74. #include <stdarg.h>
  75. #else
  76. #define USE_PROTO               0
  77. #define PROTO(x)                ()
  78. #include <varargs.h>
  79. #endif
  80.  
  81. #include "regexp.h"
  82.  
  83. #if USE_PROTO
  84. #include <stdlib.h>
  85. #else
  86. extern int          errno;
  87. #endif
  88.  
  89. #ifndef FILENAME_MAX
  90. #define FILENAME_MAX            128
  91. #endif
  92.  
  93. #define CTRL_Z                  0x1a
  94.  
  95. #define FILTYP_DOS              0
  96. #define FILTYP_MAC              1
  97. #define FILTYP_UNIX             2
  98. #define FILTYP_BINARY           3
  99. #define FILTYP_VAX              4
  100.  
  101. static int      ChkFileType     PROTO((FILE *pFile));
  102. static int      ConvertFiles    PROTO((char *pPath, int bOrigCall));
  103. static int      CopyFile        PROTO((char *pszSrcFile, char *pszDestFile));
  104. static void     Err             PROTO((int nLine, int nErrNo, char *pszFmt, ...));
  105. static int      ProcFileByType  PROTO((FILE *pInFile, char *pszInName,
  106.                                         char *pszShortName));
  107. static int      ProcessFile     PROTO((FILE *pInFile, int nFilTyp,
  108.                                         char *pszInName));
  109. static void     Usage           PROTO((void));
  110. static int      WildCardPath    PROTO((char *pPath, char *pszInName,
  111.                                         char *pszRegExp, struct stat *pST));
  112. static void     WriteChar       PROTO((int c, FILE *pOutFile));
  113.  
  114. #ifdef NEED_STRERROR
  115. static char *   strerror        PROTO((int nErr));
  116. #endif
  117.  
  118. static int              nSelCnt        = 0;
  119. static int              nFileCnt       = 0;
  120. static int              nCharLimit     = 127;
  121. static int              bSearchSubDirs = 0;
  122. static int              bChkType       = 0;
  123. static int              bToDos         = 0;
  124. static int              bToUnix        = 0;
  125. static int              bToMac         = 0;
  126. static int              bToVax         = 0;
  127.  
  128. #if USE_PROTO
  129. int main(int argc, char **argv)
  130. #else
  131. int main(argc, argv)
  132. int argc;
  133. char **argv;
  134. #endif
  135. {
  136.     register int      i, j;
  137.     auto     int      nFirstArg = 1;
  138.  
  139.     for (i = 1; i < argc; ++i)
  140.     {
  141.         if (!('-' == argv[i][0] || '/' == argv[i][0]))
  142.             break;
  143.  
  144.         ++nFirstArg;
  145.         for (j = 1; argv[i][j]; ++j)
  146.         {
  147.             switch (argv[i][j])
  148.             {
  149.                 case 's':
  150.                 case 'S':
  151.                     ++bSearchSubDirs;
  152.                     break;
  153.  
  154.                 case 'c':
  155.                 case 'C':
  156.                     ++bChkType;
  157.                     break;
  158.  
  159.                 case '8':
  160.                     nCharLimit = 255;
  161.                     break;
  162.  
  163.                 case 'm':
  164.                 case 'M':
  165.                     ++bToMac;
  166.                     break;
  167.  
  168.                 case 'd':
  169.                 case 'D':
  170.                     ++bToDos;
  171.                     break;
  172.  
  173.                 case 'u':
  174.                 case 'U':
  175.                     ++bToUnix;
  176.                     break;
  177.  
  178.                 case 'v':
  179.                 case 'V':
  180.                     ++bToVax;
  181.                     break;
  182.  
  183.                 default:
  184.                     printf("\nUnknown switch %c\n", argv[i][j]);
  185.                     break;
  186.             }
  187.         }
  188.     }
  189.  
  190.     if (argc - nFirstArg < 1)
  191.     {
  192.         Usage();
  193.         return(0);
  194.     }
  195.  
  196.     if (1 != (bChkType + bToMac + bToDos + bToUnix + bToVax))
  197.         Err(__LINE__, 0, "\nInvalid switch combination, only one of c,m,u,d,v\n");
  198.  
  199.     if (!bChkType)
  200.     {
  201.         printf("\nConvert files to ");
  202.         if (bToDos)
  203.             printf("Dos format\n");
  204.         else
  205.         {
  206.             if (bToUnix)
  207.                 printf("Unix format\n");
  208.             else
  209.             {
  210.                 if (bToVax)
  211.                     printf("Vax format\n");
  212.                 else
  213.                     printf("Mac format\n");
  214.             }
  215.         }
  216.     }
  217.  
  218.     for (i = nFirstArg; i < argc; ++i)
  219.     {
  220.         if (ConvertFiles(argv[i], 1))
  221.             exit(1);
  222.     }
  223.  
  224.     printf("\n\n%d file%s selected, %d converted.\n",
  225.            nSelCnt, 1 == nSelCnt ? "" : "s", nFileCnt);
  226.     return(0);
  227. }
  228.  
  229.  
  230. #define DIRNAME_ARRAY_INCR      10
  231.  
  232. #if USE_PROTO
  233. static int ConvertFiles(char *pPath, int bOrigCall)
  234. #else
  235. static int ConvertFiles(pPath, bOrigCall)
  236. char *pPath;
  237. int bOrigCall;
  238. #endif
  239. {
  240.     auto     FILE *             pInFile;
  241.     auto     char *             p;
  242.     auto     int                len;
  243.     auto     int                bSearchDir = 0;
  244.     static   regexp *           pRegExp;
  245.     static   struct stat        st;
  246.     static   char               szPath[FILENAME_MAX];
  247.     static   char               szInName[FILENAME_MAX];
  248.     static   char               szRegExp[FILENAME_MAX];
  249.  
  250.     strcpy(szInName, pPath);
  251.     bSearchDir = WildCardPath(pPath, szPath, szRegExp, &st);
  252.     if (bSearchDir)
  253.     {
  254.         auto     DIR *              pDir;
  255.         auto     struct dirent *    pDirEnt;
  256.         auto     int                nCurDirCnt = 0;
  257.         auto     int                nMaxDirCnt = 0;
  258.         auto     char **            pDirNames  = NULL;
  259.         static   int                nOrigPathLen;
  260.  
  261.         strcat(szPath, PATH_SEP_STR);
  262.         pDir = opendir(szPath);
  263.         if (NULL == pDir)
  264.             Err(__LINE__, 0, "\nError, unable to open directory %s\n", szPath);
  265.  
  266.         if (bOrigCall)
  267.         {
  268.             nOrigPathLen = strlen(szPath) - 1;
  269.             pRegExp = regcomp(szRegExp);
  270.             if (NULL == pRegExp)
  271.             {
  272.                 Err(__LINE__, 0, "\nError compiling regular expression %s\n",
  273.                         szRegExp);
  274.             }
  275.         }
  276.  
  277.         while (NULL != (pDirEnt = readdir(pDir)))
  278.         {
  279.             if (0 == strcmp(pDirEnt->d_name, ".")
  280.                             ||
  281.                 0 == strcmp(pDirEnt->d_name, ".."))
  282.             {
  283.                 continue;
  284.             }
  285.  
  286. #ifndef UNIX
  287.             strupr(pDirEnt->d_name);
  288. #endif
  289.  
  290.             strcpy(szInName, szPath);
  291.             strcat(szInName, pDirEnt->d_name);
  292.             if (0 != stat(szInName, &st))
  293.                 Err(__LINE__, errno, "\nstat failed on %s", szInName);
  294.  
  295.             if (st.st_mode & S_IFDIR)
  296.             {
  297.                 if (!bSearchSubDirs)
  298.                     continue;
  299.  
  300.                 if (nCurDirCnt >= nMaxDirCnt)
  301.                 {
  302.                     nMaxDirCnt += DIRNAME_ARRAY_INCR;
  303.                     len = nMaxDirCnt * sizeof(*pDirNames);
  304.                     if (NULL == pDirNames)
  305.                         pDirNames = malloc(len);
  306.                     else
  307.                         pDirNames = realloc(pDirNames, len);
  308.  
  309.                     if (NULL == pDirNames)
  310.                     {
  311.                         closedir(pDir);
  312.                         Err(__LINE__, 0, "Error - out of memory");
  313.                     }
  314.                 }
  315.  
  316.                 p = strdup(pDirEnt->d_name);
  317.                 if (p)
  318.                     pDirNames[nCurDirCnt++] = p;
  319.                 else
  320.                     Err(__LINE__, 0, "Out of memory");
  321.  
  322.                 continue;
  323.             }
  324.  
  325.             if (regexec(pRegExp, pDirEnt->d_name))
  326.             {
  327.                 p = szInName + nOrigPathLen + 1;
  328.                 if (!bChkType)
  329.                     printf("\nConverting %-16s\t", p);
  330.  
  331.                 pInFile = fopen(szInName, READ_BIN);
  332.                 if (NULL == pInFile)
  333.                 {
  334.                     closedir(pDir);
  335.                     Err(__LINE__, errno, "Error - unable to open %s", szInName);
  336.                 }
  337.  
  338.                 ProcFileByType(pInFile, szInName, p);
  339.             }
  340.         }
  341.  
  342.         closedir(pDir);
  343.         if (nCurDirCnt > 0)
  344.         {
  345.             p = strdup(szPath);
  346.             if (NULL == p)
  347.                 Err(__LINE__, 0, "Out of memory");
  348.  
  349.             for (len = 0; len < nCurDirCnt; ++len)
  350.             {
  351.                 strcpy(szInName, p);
  352.                 strcat(szInName, pDirNames[len]);
  353.                 free(pDirNames[len]);
  354.                 if (ConvertFiles(szInName, 0))
  355.                     return(1);
  356.             }
  357.  
  358.             free(p);
  359.         }
  360.  
  361.         if (bOrigCall)
  362.             free(pRegExp);
  363.  
  364.         if (pDirNames)
  365.             free(pDirNames);
  366.     }
  367.     else
  368.     {
  369.         pInFile = fopen(szInName, READ_BIN);
  370.         if (NULL == pInFile)
  371.             Err(__LINE__, errno, "Error, unable to open %s", szInName);
  372.  
  373.         p = strrchr(szInName, PATH_SEP);
  374.         if (NULL == p)
  375.             p = szInName;
  376.         else
  377.             ++p;
  378.  
  379.         if (!bChkType)
  380.             printf("\nConverting %-16s\t", p);
  381.  
  382.         ProcFileByType(pInFile, szInName, p);
  383.     }
  384.  
  385.     return(0);
  386. }
  387.  
  388.  
  389. #if USE_PROTO
  390. static int WildCardPath(char *pPath, char *pszInName,
  391.                         char *pszRegExp, struct stat *pST)
  392. #else
  393. static int WildCardPath(pPath, pszInName, pszRegExp, pST)
  394. char *pPath;
  395. char *pszInName;
  396. char *pszRegExp;
  397. struct stat *pST;
  398. #endif
  399. {
  400.     register char *             p;
  401.     auto     char               szWrk[2];
  402.  
  403.     szWrk[1] = '\0';
  404.     strcpy(pszInName, pPath);
  405.     if (0 == stat(pszInName, pST))
  406.     {
  407.         if (pST->st_mode & S_IFDIR)
  408.         {
  409.             strcpy(pszRegExp, ".*");
  410.             return(1);
  411.         }
  412.     }
  413.  
  414.     p = strrchr(pszInName, PATH_SEP);
  415.     if (NULL == p)
  416.     {
  417.         p = pszInName;
  418.         while (*p)
  419.         {
  420.             if ('?' == *p || '*' == *p)
  421.                 break;
  422.  
  423.             ++p;
  424.         }
  425.  
  426.         if (*p)
  427.         {
  428.             p = pPath;
  429.             strcpy(pszInName, ".");
  430.         }
  431.         else
  432.             return(0);
  433.     }
  434.     else
  435.     {
  436.         *p++ = '\0';
  437.     }
  438.  
  439.     strcpy(pszRegExp, "^");
  440.     while (*p)
  441.     {
  442.         if ('.' == *p)
  443.         {
  444.             strcat(pszRegExp, "\\.");
  445.         }
  446.         else
  447.         {
  448.             if ('*' == *p)
  449.             {
  450.                 strcat(pszRegExp, ".*");
  451.             }
  452.             else
  453.             {
  454. #ifdef UNIX
  455.                 szWrk[0] = *p;
  456. #else
  457.                 szWrk[0] = toupper(*p);
  458. #endif
  459.                 strcat(pszRegExp, szWrk);
  460.             }
  461.         }
  462.  
  463.         ++p;
  464.     }
  465.  
  466.     strcat(pszRegExp, "$");
  467.     return(1);
  468. }
  469.  
  470.  
  471. #if USE_PROTO
  472. static int ProcFileByType(FILE *pInFile, char *pszInName, char *pszShortName)
  473. #else
  474. static int ProcFileByType(pInFile, pszInName, pszShortName)
  475. FILE *pInFile;
  476. char *pszInName;
  477. char *pszShortName;
  478. #endif
  479. {
  480.     ++nSelCnt;
  481.     switch (ChkFileType(pInFile))
  482.     {
  483.         case FILTYP_MAC:
  484.             if (bChkType)
  485.             {
  486.                 fclose(pInFile);
  487.                 printf("\nFile %-16s\ttype is Mac", pszShortName);
  488.             }
  489.             else
  490.             {
  491.                 if (bToMac)
  492.                 {
  493.                     fclose(pInFile);
  494.                     printf("already Mac format");
  495.                 }
  496.                 else
  497.                 {
  498.                     ProcessFile(pInFile, FILTYP_MAC, pszInName);
  499.                     printf("from Mac");
  500.                 }
  501.             }
  502.  
  503.             break;
  504.  
  505.         case FILTYP_DOS:
  506.             if (bChkType)
  507.             {
  508.                 fclose(pInFile);
  509.                 printf("\nFile %-16s\ttype is Dos", pszShortName);
  510.             }
  511.             else
  512.             {
  513.                 if (bToDos)
  514.                 {
  515.                     fclose(pInFile);
  516.                     printf("already Dos format");
  517.                 }
  518.                 else
  519.                 {
  520.                     ProcessFile(pInFile, FILTYP_DOS, pszInName);
  521.                     printf("from Dos");
  522.                 }
  523.             }
  524.  
  525.             break;
  526.  
  527.         case FILTYP_UNIX:
  528.             if (bChkType)
  529.             {
  530.                 printf("\nFile %-16s\ttype is Unix", pszShortName);
  531.                 fclose(pInFile);
  532.             }
  533.             else
  534.             {
  535.                 if (bToUnix)
  536.                 {
  537.                     fclose(pInFile);
  538.                     printf("already Unix format");
  539.                 }
  540.                 else
  541.                 {
  542.                     ProcessFile(pInFile, FILTYP_UNIX, pszInName);
  543.                     printf("from Unix");
  544.                 }
  545.             }
  546.  
  547.             break;
  548.  
  549.         case FILTYP_VAX:
  550.             if (bChkType)
  551.             {
  552.                 printf("\nFile %-16s\ttype is Vax", pszShortName);
  553.                 fclose(pInFile);
  554.             }
  555.             else
  556.             {
  557.                 if (bToVax)
  558.                 {
  559.                     fclose(pInFile);
  560.                     printf("already Vax format");
  561.                 }
  562.                 else
  563.                 {
  564.                     ProcessFile(pInFile, FILTYP_VAX, pszInName);
  565.                     printf("from Vax");
  566.                 }
  567.             }
  568.  
  569.             break;
  570.  
  571.         default:
  572.             fclose(pInFile);
  573.             if (bChkType)
  574.                 printf("\nFile %-16s\ttype is Binary", pszShortName);
  575.             else
  576.                 printf("skipped, binary file");
  577.  
  578.             break;
  579.     }
  580.  
  581.     return(0);
  582. }
  583.  
  584.  
  585. #if USE_PROTO
  586. static int ProcessFile(FILE *pInFile, int nFilTyp, char *pszInName)
  587. #else
  588. static int ProcessFile(pInFile, nFilTyp, pszInName)
  589. FILE *pInFile;
  590. int nFilTyp;
  591. char *pszInName;
  592. #endif
  593. {
  594.     register int                c;
  595.     auto     FILE *             pOutFile;
  596.     auto     char               szOutName[L_tmpnam];
  597.  
  598.     tmpnam(szOutName);
  599.     pOutFile = fopen(szOutName, WRITE_BIN);
  600.     if (NULL == pOutFile)
  601.         Err(__LINE__, errno, "Error, unable to open temp file %s", szOutName);
  602.  
  603.     while (EOF != (c = fgetc(pInFile)))
  604.     {
  605.         switch (nFilTyp)
  606.         {
  607.             case FILTYP_DOS:
  608.                 if (bToUnix && ('\r' == c || CTRL_Z == c))
  609.                     continue;
  610.  
  611.                 if (bToMac && ('\n' == c || CTRL_Z == c))
  612.                     continue;
  613.  
  614.                 if (bToVax)
  615.                 {
  616.                     if (CTRL_Z == c)
  617.                         continue;
  618.  
  619.                     if ('\n' == c)
  620.                         WriteChar('\r', pOutFile);
  621.                 }
  622.  
  623.                 WriteChar(c, pOutFile);
  624.                 break;
  625.  
  626.             case FILTYP_MAC:
  627.                 if ('\r' == c)
  628.                 {
  629.                     c = '\n';
  630.                     if (bToVax)
  631.                     {
  632.                         WriteChar('\r', pOutFile);
  633.                         WriteChar('\r', pOutFile);
  634.                     }
  635.                 }
  636.  
  637.                 if ('\n' == c && bToDos)
  638.                     WriteChar('\r', pOutFile);
  639.  
  640.                 WriteChar(c, pOutFile);
  641.                 break;
  642.  
  643.             case FILTYP_UNIX:
  644.                 if ('\n' == c)
  645.                 {
  646.                     if (bToMac)
  647.                         c = '\r';
  648.                     else
  649.                     {
  650.                         if (bToDos)
  651.                             WriteChar('\r', pOutFile);
  652.                         else
  653.                         {
  654.                             if (bToVax)
  655.                             {
  656.                                 WriteChar('\r', pOutFile);
  657.                                 WriteChar('\r', pOutFile);
  658.                             }
  659.                         }
  660.                     }
  661.                 }
  662.  
  663.                 WriteChar(c, pOutFile);
  664.                 break;
  665.  
  666.             case FILTYP_VAX:
  667.                 if ('\r' == c)
  668.                     continue;
  669.  
  670.                 if ('\n' == c)
  671.                 {
  672.                     if (bToMac)
  673.                         c = '\r';
  674.                     else
  675.                     {
  676.                         if (bToDos)
  677.                             WriteChar('\r', pOutFile);
  678.                     }
  679.                 }
  680.  
  681.                 WriteChar(c, pOutFile);
  682.                 break;
  683.         }
  684.     }
  685.  
  686.     fclose(pInFile);
  687.     fclose(pOutFile);
  688.     if (CopyFile(szOutName, pszInName))
  689.         exit(1);
  690.  
  691.     unlink(szOutName);
  692.     ++nFileCnt;
  693.     return(0);
  694. }
  695.  
  696.  
  697. #if USE_PROTO
  698. static void WriteChar(int c, FILE *pOutFile)
  699. #else
  700. static void WriteChar(c, pOutFile)
  701. int c;
  702. FILE *pOutFile;
  703. #endif
  704. {
  705.     if (EOF == fputc(c, pOutFile))
  706.         Err(__LINE__, errno, "Error, can't write to temp output file\n");
  707.  
  708.     return;
  709. }
  710.  
  711.  
  712. #if USE_PROTO
  713. static int CopyFile(char *pszSrcFile, char *pszDestFile)
  714. #else
  715. static int CopyFile(pszSrcFile, pszDestFile)
  716. char *pszSrcFile;
  717. char *pszDestFile;
  718. #endif
  719. {
  720.     auto     size_t      Len;
  721.     auto     FILE *      pIn;
  722.     auto     FILE *      pOut;
  723.     static   char        Buf[4096];
  724.  
  725.     pIn = fopen(pszSrcFile, READ_BIN);
  726.     if (NULL == pIn)
  727.         Err(__LINE__, errno, "Error, unable to open file %s", pszSrcFile);
  728.  
  729.     pOut = fopen(pszDestFile, WRITE_BIN);
  730.     if (NULL == pOut)
  731.     {
  732.         fclose(pIn);
  733.         Err(__LINE__, errno, "Error, unable to open file %s", pszDestFile);
  734.     }
  735.  
  736.     Len = fread(Buf, sizeof(Buf[0]), sizeof(Buf), pIn);
  737.     while (Len > 0)
  738.     {
  739.         if (Len != fwrite(Buf, sizeof(Buf[0]), Len, pOut))
  740.         {
  741.             fclose(pIn);
  742.             fclose(pOut);
  743.             Err(__LINE__, errno, "Error writing output - disk full??");
  744.         }
  745.  
  746.         if (Len < sizeof(Buf))
  747.             break;
  748.  
  749.         Len = fread(Buf, sizeof(Buf[0]), sizeof(Buf), pIn);
  750.     }
  751.  
  752.     if (ferror(pIn))
  753.     {
  754.         perror("\nError while copying temp file ");
  755.         fclose(pIn);
  756.         fclose(pOut);
  757.         return(1);
  758.     }
  759.  
  760.     fclose(pIn);
  761.     fclose(pOut);
  762.     return(0);
  763. }
  764.  
  765.  
  766. #if USE_PROTO
  767. static int ChkFileType(FILE *pFile)
  768. #else
  769. static int ChkFileType(pFile)
  770. FILE *pFile;
  771. #endif
  772. {
  773.     auto     size_t             i;
  774.     auto     size_t             siz;
  775.     auto     int                nCR = 0;
  776.     auto     int                nLF = 0;
  777.     static   unsigned char      buf[1024];
  778.  
  779.     siz = fread(buf, 1, sizeof(buf), pFile);
  780.     fseek(pFile, 0L, SEEK_SET);
  781.     if (0 == siz)
  782.         return(FILTYP_BINARY);
  783.  
  784.     for (i = 0; i < siz; ++i)
  785.     {
  786.         switch (buf[i])
  787.         {
  788.             case '\r':
  789.                 ++nCR;
  790.                 break;
  791.  
  792.             case '\n':
  793.                 ++nLF;
  794.                 break;
  795.  
  796.             case '\t':
  797.             case 11:
  798.             case 12:
  799.             case CTRL_Z:
  800.                 break;
  801.  
  802.             default:
  803.                 if (buf[i] < ' ' || (int) buf[i] > nCharLimit)
  804.                     return(FILTYP_BINARY);
  805.  
  806.                 break;
  807.         }
  808.     }
  809.  
  810.     if (0 == nCR && nLF)
  811.         return(FILTYP_UNIX);
  812.  
  813.     if (0 == nLF && nCR)
  814.         return(FILTYP_MAC);
  815.  
  816.     if (nCR > nLF + 1)
  817.         return(FILTYP_VAX);
  818.  
  819.     return(FILTYP_DOS);
  820. }
  821.  
  822.  
  823. #if USE_PROTO
  824. static void Usage(void)
  825. #else
  826. static void Usage()
  827. #endif
  828. {
  829.     printf("\nCVT - Convert source file to Dos/Mac/Unix/Vax format V3.04");
  830. #ifdef UNIX
  831.     printf("\n      Bob Withers, 10/02/93\n");
  832. #else
  833.     printf("\n      Bob Withers, 10/02/93 [as of %s]\n", __DATE__);
  834. #endif
  835.     printf("\nUsage:    CVT [-s] -cdmuv file1 file2 ... fileN");
  836.     printf("\n          -s  ==> Search subdirectories");
  837.     printf("\n          -c  ==> Check filetypes and display");
  838.     printf("\n          -8  ==> Allow chars > 127 in converted files");
  839.     printf("\n          -d  ==> Convert to DOS format");
  840.     printf("\n          -m  ==> Convert to MAC format");
  841.     printf("\n          -u  ==> Convert to Unix format");
  842.     printf("\n          -v  ==> Convert to Vax format");
  843.     printf("\n          Select 1 of c,d,m,u,v only");
  844.     printf("\n          Wildcards are permitted.");
  845.     printf("\n          Files are overwritten with converted version.\n");
  846.     return;
  847. }
  848.  
  849.  
  850. #if USE_PROTO
  851. static void Err(int nLine, int nErrNo, char *pszFmt, ...)
  852. #else
  853. static void Err(va_alist)
  854. va_dcl
  855. #endif
  856. {
  857. #if USE_PROTO
  858.     auto     va_list            ap;
  859.  
  860.     va_start(ap, pszFmt);
  861. #else
  862.     auto     int                nLine;
  863.     auto     int                nErrNo;
  864.     auto     char *             pszFmt;
  865.     auto     va_list            ap;
  866.  
  867.     va_start(ap);
  868.     nLine = va_arg(ap, int);
  869.     nErrNo = va_arg(ap, int);
  870.     pszFmt = va_arg(ap, char *);
  871. #endif
  872.  
  873.     printf("\nLine %d: ", nLine);
  874.     vprintf(pszFmt, ap);
  875.     va_end(ap);
  876.     printf("\n");
  877.     if (nErrNo)
  878.         printf("Error %d: %s\n", nErrNo, strerror(nErrNo));
  879.  
  880.     exit(1);
  881. }
  882.  
  883.  
  884. #ifdef NEED_STRERROR
  885. #if USE_PROTO
  886. static char *strerror(int nErr)
  887. #else
  888. static char *strerror(nErr)
  889. int nErr;
  890. #endif
  891. {
  892.     extern   int        sys_nerr;
  893.     extern   char *     sys_errlist[];
  894.  
  895.     if (nErr < sys_nerr)
  896.         return(sys_errlist[nErr]);
  897.  
  898.     return("");
  899. }
  900. #endif
  901.