home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / MAIN.C < prev    next >
C/C++ Source or Header  |  1993-01-02  |  3KB  |  103 lines

  1.  
  2. /*
  3.  * This program can serve as a template for a routine that searches for
  4.  * keywords in CONFIG.SYS and writes out a new version with a given token.
  5.  * It assumes you're booting off drive C, it will make the change on EVERY
  6.  * matching line found and generally needs to be tuned for your requirements.
  7.  
  8.  *  Copyright (C) 1991 IBM Corporation
  9.  *
  10.  *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  11.  *      sample code created by IBM Corporation. This sample code is not
  12.  *      part of any standard or IBM product and is provided to you solely
  13.  *      for  the purpose of assisting you in the development of your
  14.  *      applications.  The code is provided "AS IS", without
  15.  *      warranty of any kind.  IBM shall not be liable for any damages
  16.  *      arising out of your use of the sample code, even if they have been
  17.  *      advised of the possibility of such damages.                                                    *
  18.  */
  19. #include <stdio.h>
  20. #include <string.h>
  21.  
  22. #define KEY_LEN 32
  23. #define LINE_LEN 256
  24.  
  25. typedef unsigned short BOOL;
  26. #define TRUE   1
  27. #define FALSE  0
  28.  
  29. main( int argc, char **argv )
  30. {
  31.     FILE    *fOld;
  32.     FILE    *fNew;
  33.     BOOL    bDone;
  34.     char    pszKeyword[ KEY_LEN ];
  35.     char    pszLine[ LINE_LEN ];
  36.     short   sSearchLen;
  37.  
  38.     if ( argc != 3 )
  39.     {
  40.         printf("Please enter CONFIG.SYS keyword and token\n" );
  41.         return( -1 );
  42.     }
  43.  
  44.     sprintf( pszKeyword, "SET %s", (char *)argv[1] );
  45.     sSearchLen = strlen( pszKeyword );
  46.     printf("Searching for '%s' in CONFIG.SYS\n", pszKeyword );
  47.  
  48.     fOld = fopen("C:\\CONFIG.SYS","r" );
  49.     if ( ferror( fOld ) )
  50.     {
  51.         printf("Error opening C:\\CONFIG.SYS");
  52.         return( -2 );
  53.     }
  54.  
  55.     fNew = fopen("C:\\CONFIG.NEW","w" );
  56.     if ( ferror( fNew ) )
  57.     {
  58.         printf("Error opening C:\\CONFIG.NEW");
  59.         return( -3 );
  60.     }
  61.  
  62.     bDone = FALSE;
  63.     while ( !bDone )
  64.     {
  65.         if ( fgets( pszLine, LINE_LEN, fOld ) != NULL )
  66.         {
  67.             /* got a line of text */
  68.             if ( strncmp( pszLine, pszKeyword, sSearchLen ) == 0 )
  69.             {
  70.                 /* we matched the keyword, so now scan the string for token */
  71.                 if ( strcmp( pszLine, (char *)argv[2] ) == 0 )
  72.                 {
  73.                     printf("Token already on line.\n");
  74.                 }
  75.                 else
  76.                 {
  77.                     printf("Adding token to line\n");
  78.                     pszLine[ strlen(pszLine) - 1 ] = '\0';  /* nuke \n */
  79.                     strcat( pszLine, (char *)argv[2] );
  80.                 }
  81.                 printf( "%s\n", pszLine );
  82.                 
  83.             }
  84.             if ( fputs( pszLine, fNew ) <= 0 )
  85.             {
  86.                 printf("Error during write of CONFIG.NEW\n" );
  87.                 bDone = TRUE;
  88.             }
  89.         }
  90.         else
  91.         {
  92.             /* could be EOF or file error */
  93.             if ( ferror( fOld ) )
  94.                 printf("Error during read of CONFIG.SYS\n" );
  95.             bDone = TRUE;
  96.         }
  97.     }
  98.     fclose( fOld );
  99.     fclose( fNew );
  100.     return( 0 );
  101. }
  102.  
  103.