home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / SAFEMINX.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  6KB  |  199 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /* =======================================================================
  4.     SAFEMINX.c  Safe Multiple Includes.
  5.  
  6.                 A utility to prevent problems with multiple #include's of
  7.                 the same file. Which is a severe problem for some older
  8.                 compilers like Turbo C version 1.5 ...
  9.  
  10.                 95-10-26  v1.00  Initial version
  11.  
  12.  _____          This version is Public Domain.
  13.  /_|__|         A.Reitsma, Delft, Nederland.
  14. /  |  \ --------------------------------------------------------------- */
  15.  
  16. #include <stdlib.h>     /* for EXIT_SUCCESS               */
  17. #include <stdio.h>      /* for fprintf()/fopen()/fclose() */
  18. #include <string.h>     /* for strlen etc ...             */
  19. #include <ctype.h>      /* for isalpha()                  */
  20. #include "dirport.h"
  21.  
  22. #ifndef FILENAME_MAX
  23.   #define FILENAME_MAX  80
  24. #endif
  25. #ifndef FNAME_MAX
  26.   #define FNAME_MAX     8+3+1
  27. #endif
  28.  
  29. #define PROGRAM   "SAFEMINX"
  30. #define VERSION   "v1.00"
  31. #define AUTHOR    "A.Reitsma, Delft, The Netherlands."
  32. #define CREATED   "95-10-26"
  33. #define COPYRIGHT "Public Domain."
  34.  
  35. char * Syntax[] =
  36. {
  37.     PROGRAM "  " VERSION "  " CREATED "  " AUTHOR "  " COPYRIGHT "\n\n",
  38.     "Usage: " PROGRAM " <destination directory name>\n\n",
  39.     "The problem:\n",
  40.     "Some old compilers have severe problems with multiple #include's\n",
  41.     "of the same file. This program eliminates the problem by creating\n",
  42.     "'include-file' wrappers in the destination directory for the\n",
  43.     "include files in the current directory.\n",
  44.     "The wrappers could also be edited to include items which _should_\n",
  45.     "be in the include file according to the standard but aren't.\n",
  46.     "E.g.: FILENAME_MAX in stdio.h or CLOCKS_PER_SEC in time.h\n\n",
  47.     "Instructions:\n- create a new include file directory,\n",
  48.     "- change directory to the old include file directory,\n",
  49.     "- run this program with the new directory as parameter,\n",
  50.     "- change your compiler's settings to use the new directory.\n",
  51.     "(Or something to the same effect.)\n\n",
  52.     "Files are NEVER deleted or modified!\n\n",
  53.     "Problem permanently solved ...\n",
  54.     NULL
  55. };
  56.  
  57. enum Errors
  58. {
  59.     ERR_ARGS = 1,
  60.     ERR_MEM,
  61.     ERR_FOPEN,
  62.     ERR_NODIR,
  63.     ERR_INVDRIVE,
  64.     ERR_DIRCURRENT,
  65.     ERR_NOFILES,
  66. };
  67.  
  68. main( int argc, char * argv[] )
  69. {
  70.     char DirCurrent[ FILENAME_MAX ];
  71.     char DestName[ FILENAME_MAX ];
  72.     int DestDirLen;
  73.     DOSFileData Data;
  74.  
  75.     /* validate the arguments
  76.     */
  77.     if( argc < 2 )
  78.     {
  79.         int ix = 0;
  80.         do
  81.         {
  82.             fprintf( stdout, Syntax[ ix ]);
  83.         }while( NULL != Syntax[ ++ix ]);
  84.         return ERR_ARGS;
  85.     }
  86.  
  87.     /* obtain the name of the current directory
  88.     */
  89.     getcwd( DirCurrent, FILENAME_MAX );
  90.     strlwr( DirCurrent );
  91.  
  92.     /* validate the destination drive and directory
  93.     */
  94.     strcpy( DestName, argv[ 1 ]);
  95.     strlwr( DestName );
  96.     if( ':' == DestName[ 1 ] )
  97.     {
  98.         DestName[ 2 ] = '.';
  99.         DestName[ 3 ] = '\0';
  100.         if( 0 != chdir( DestName ))
  101.         {
  102.             fprintf( stderr, "\aInvalid destination drive\n" );
  103.             return ERR_INVDRIVE;
  104.         }
  105.         DestName[ 2 ] = argv[ 1 ][ 2 ];
  106.         DestName[ 3 ] = argv[ 1 ][ 3 ];
  107.     }
  108.     if( 0 != chdir( argv[ 1 ]) )
  109.     {
  110.         fprintf( stderr, "\aInvalid destination directory\n" );
  111.         return ERR_NODIR;
  112.     }
  113.  
  114.     chdir( DirCurrent ); /* just in case it was changed ... */
  115.  
  116.     if( 0 == strcmp( DirCurrent, DestName ))
  117.     {
  118.         fprintf( stderr, "\aDestination is current directory.\n" );
  119.         return ERR_DIRCURRENT;
  120.     }
  121.  
  122.     /* anything to do?
  123.     */
  124.     if( 0 != FIND_FIRST( "*.h", _A_NORMAL, &Data ))
  125.     {
  126.         fprintf( stderr, "\aNo .h files in the current directory\n" );
  127.         return ERR_NOFILES;
  128.     }
  129.  
  130.     /* Yes, something to do: prepare for action
  131.     */
  132.     fprintf( stdout, "Directory: \"%s\" ---\n", DirCurrent );
  133.     DestDirLen = strlen( argv[ 1 ]);
  134.     memcpy( DestName, argv[ 1 ], DestDirLen+1 );
  135.     if( '\\' != argv[ 1 ][ DestDirLen - 1 ] )
  136.     {
  137.         DestName[ DestDirLen ] = '\\' ;
  138.         DestDirLen++;
  139.     }
  140.  
  141.     do
  142.     {
  143.         char NameAlpha[ FNAME_MAX+1 ];
  144.         char * NamePtr = NameAlpha;
  145.         FILE * FileOut;
  146.  
  147.         strcpy( DestName+DestDirLen, ff_name( &Data ));
  148.  
  149.         FileOut = fopen( DestName, "r" );
  150.         if( NULL != FileOut )
  151.         {
  152.             fprintf( stdout, 
  153.                      "Not overwriting \"%s\". It already exists!\n",
  154.                      ff_name( &Data ));
  155.             continue;
  156.         }
  157.         FileOut = fopen( DestName, "w" );
  158.         if( NULL == FileOut )
  159.         {
  160.             fprintf( stdout, "\aFailure opening \"%s\".\n",
  161.                              ff_name( &Data ));
  162.             continue;
  163.         }
  164.  
  165.         fprintf( stdout, "Creating \"%s\"\n", DestName );
  166.  
  167.         strcpy( NameAlpha, ff_name( &Data ));
  168.         while( '\0' != *NamePtr )
  169.         {
  170.             if( !isalpha( *NamePtr ))
  171.                  *NamePtr = '_';
  172.             NamePtr ++ ;
  173.         }
  174.         fprintf( FileOut,       /* fixed text: */
  175.                 "/*  %s\n"
  176.                 "    Prevention of double inclusions.\n"
  177.                 "*/\n\n"
  178.                 "#ifndef SAFE__%s\n"
  179.                 "#define SAFE__%s\n\n"
  180.                 "#include <%s\\%s>    /* 'original' */\n"
  181.                 "\n#endif\n",
  182.                                 /* parameters: */
  183.                 ff_name( &Data ),
  184.                 NameAlpha,
  185.                 NameAlpha,
  186.                 DirCurrent,
  187.                 ff_name( &Data )
  188.                );
  189.  
  190.         fclose( FileOut );
  191.     }while( 0 == FIND_NEXT( &Data ));
  192.     FIND_END( &Data );
  193.  
  194.     fprintf( stdout, "--- Done ---\n" );
  195.     return EXIT_SUCCESS;
  196. }
  197.  
  198. /* === SAFEMINX.c end ================================================= */
  199.