setupfil.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 1,382 行 · 第 1/4 页

C
1,382
字号
/****************************************************************************
*
*                            Open Watcom Project
*
*    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
*  ========================================================================
*
*    This file contains Original Code and/or Modifications of Original
*    Code as defined in and that are subject to the Sybase Open Watcom
*    Public License version 1.0 (the 'License'). You may not use this file
*    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
*    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
*    provided with the Original Code and Modifications, and is also
*    available at www.sybase.com/developer/opensource.
*
*    The Original Code and all software distributed under the License are
*    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
*    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
*    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
*    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
*    NON-INFRINGEMENT. Please see the License for the specific language
*    governing rights and limitations under the License.
*
*  ========================================================================
*
* Description:  Routines to modify registry and configuration files.
*
****************************************************************************/


#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#if !defined( __UNIX__ )
    #include <dos.h>
#endif
#include <ctype.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#if defined( __UNIX__ )
    #include <time.h>
#endif
#if defined( __WINDOWS__ ) || defined( __NT__ )
    #include <windows.h>
#endif
#if defined( __OS2__ )
    #define INCL_DOSMISC
    #include <os2.h>
#endif
#include "gui.h"
#include "guiutil.h"
#include "setup.h"
#include "setupinf.h"
#include "genvbl.h"
#include "gendlg.h"
#include "utils.h"

extern int GetOptionVarValue( vhandle var_handle, bool is_minimal );
extern bool CheckForceDLLInstall( char *name );

#if !defined( __UNIX__ )
static bool             ModEnv( int );
static bool             ModOS2Config( char *, char * );
#endif
static bool             ModAuto( char *, char * );
static bool             ModConfig( char *, char * );

// NT doesn't have an AUTOEXEC to modify (but we still need ReplaceVars)

static char             new_var[MAXENVVAR + 1];
static char             new_val[MAXENVVAR + 1];
static char             envbuf[MAXENVVAR + 1];
extern vhandle          UnInstall;

#if defined( __NT__ )
    struct reg_location {
        HKEY    key;
        bool    key_is_open;
        bool    modify;
    } RegLocation[NUM_REG_LOCATIONS];
#endif

bool GetOldConfigFileDir( char *newauto, char *drive_path )
/*********************************************************/
{
    char                        drive[_MAX_DRIVE];

    if( VarGetIntVal( UnInstall ) ) {
        _splitpath( drive_path, drive, NULL, NULL, NULL );
        if( drive[0] == '\x0' ) {
            _fullpath( newauto, drive_path, _MAX_PATH );
            _splitpath( newauto, drive, NULL, NULL, NULL );
        }
        _makepath( newauto, drive, NULL, NULL, NULL );
    } else {
        strcpy( newauto, GetVariableStrVal( "DstDir" ) );
    }

    return TRUE;
}

extern void NoDupPaths( char *new_value, char *old_value, bool add, char delim )
/******************************************************************************/
{
    char        *semi;
    char        *dup;
    char        *p;
    char        *look;
    int         len;
    char        *new_curr;

    new_curr = new_value;
    for( ;; ) {
        semi = strchr( new_curr, delim );
        if( semi != NULL ) {
            *semi = '\0';
        }
        look = old_value;
        len = strlen( new_curr );
        while( ( dup = stristr( look, new_curr ) ) ) {
            if( dup[len] == delim ) {
                if( dup == old_value || dup[-1] == delim ) {
                    memmove( dup, dup + len + 1, strlen( dup + len + 1 ) + 1 );
                } else {
                    look = dup + len;
                }
            } else if( dup[len] == '\0' || dup[len] == '\n' ) {
                if( dup == old_value ) {
                    dup[0] = dup[len];
                    dup[1] = '\0';
                } else if( dup[-1] == delim ) {
                    memmove( dup - 1, dup + len, strlen( dup + len ) + 1 );
                } else {
                    look = dup + len;
                }
            } else {
                look = dup + len;
            }
        }
        if( semi == NULL ) break;
        *semi = delim;
        new_curr = semi + 1;
    }
    p = strchr( old_value, '\n' );
    if( p != NULL && p[1] == '\0' ) *p = '\0';
    if( add ) {
        if( *old_value != '\0' ) {
            len = strlen( old_value );
            if( len > 1 && old_value[len - 1] == delim ) {
                --len;  // don't duplicate delimiter
            }
            old_value[len] = delim;
            old_value[len + 1] = '\0';
        }
    } else {
        new_value[0] = '\0';
    }
}

#ifndef __AXP__

#ifdef __OS2__
extern short GetBootDrive(void)
{
    UCHAR       DataBuf[10];
    unsigned long       drive;
    APIRET      rc;

    rc = DosQuerySysInfo( QSV_BOOT_DRIVE, QSV_BOOT_DRIVE, DataBuf, sizeof(DataBuf) );
    if( rc != 0 ) {
        printf("DosQuerySysInfo error: return code = %ld ",rc);
    }
    drive = *(unsigned long *)DataBuf;
    return( (int)drive );
}
#elif defined( __UNIX__ ) || defined( __NT__ )
extern short GetBootDrive( void )
{
    return( 0 );
}
#else
extern short GetBootDrive( void )
{
    union REGS  r;

    r.w.ax = 0x3305;
    intdos( &r, &r );
    return( r.h.dl );
}
#endif

char                    OrigAutoExec[] = "?:\\AUTOEXEC.BAT";
char                    OrigConfig[] = "?:\\CONFIG.SYS";

static void BackupName( char *filename );
static void ReplaceExt( char *filename, char *new_ext );

extern bool ModifyAutoExec( void )
/********************************/
{
    int                 num_auto, num_cfg;
    int                 boot_drive, mod_type;
#ifndef __OS2__
    char                newauto[_MAX_PATH];
#endif
    char                newcfg[_MAX_PATH];
    FILE                *fp;

    num_auto = SimNumAutoExec();
    num_cfg = SimNumConfig();
    if( num_auto == 0 && num_cfg == 0 ) {
        return( TRUE );
    }
#ifdef __OS2__
    SetVariableByName( "AutoText", GetVariableStrVal( "IDS_MODIFY_CONFIG" ) );
#else
    SetVariableByName( "AutoText", GetVariableStrVal( "IDS_MODIFY_AUTOEXEC" ) );
#endif
    if( DoDialog( "Modify" ) == DLG_CAN ) {
        return( FALSE );
    }
    if( GetVariableIntVal( "ModNow" ) == 1 ) {
        mod_type = MOD_IN_PLACE;
    } else {
        mod_type = MOD_LATER;
    }

    if( GetVariableIntVal( "IsWin95" ) != 0 ) {
        boot_drive = 0;
    } else {
        boot_drive = GetBootDrive();
    }
    if( boot_drive == 0 ) {
#ifdef __NT__
        if( GetDriveType( "C:\\" ) == DRIVE_FIXED ) {
            OrigAutoExec[0] = 'C';   // assume C if it is a hard drive
        } else {
            // otherwise guess it is the same as the windows system directory
            char *sys_drv;
            sys_drv = GetVariableStrVal( "WinSystemDir" );
            boot_drive = toupper( sys_drv[0] ) - 'A' + 1;
            OrigAutoExec[0] = 'A' + boot_drive - 1;
        }
#else
        OrigAutoExec[0] = 'C';   // assume C
#endif
    } else {
        OrigAutoExec[0] = 'A' + boot_drive - 1;
    }
    OrigConfig[0] = OrigAutoExec[0];

    SetVariableByName( "FileToFind", "CONFIG.SYS" );
    while( access( OrigConfig, F_OK ) != 0 ) {
        SetVariableByName( "CfgDir", OrigConfig );
        if( DoDialog( "LocCfg" ) == DLG_CAN ) {
            MsgBox( NULL, "IDS_CANTFINDCONFIGSYS", GUI_OK );
            return( FALSE );
        }
        if( GetVariableIntVal( "CfgFileCreate" ) != 0 ) {
            fp = fopen( OrigConfig, "wt" );
            if( fp == NULL ) {
                MsgBox( NULL, "IDS_CANTCREATEFILE", GUI_OK, OrigConfig );
                return( FALSE );
            }
            fclose( fp );
        }
        strcpy( newcfg, GetVariableStrVal("CfgDir") );
        OrigConfig[0] = newcfg[0];
        OrigAutoExec[0] = OrigConfig[0];
    }
#ifndef __OS2__
    SetVariableByName( "FileToFind", "AUTOEXEC.BAT" );
    while( access( OrigAutoExec, F_OK ) != 0 ) {
        SetVariableByName( "CfgDir", OrigAutoExec );
        if( DoDialog( "LocCfg" ) == DLG_CAN ) {
            MsgBox( NULL, "IDS_CANTFINDAUTOEXEC", GUI_OK );
            return( FALSE );
        }
        if( GetVariableIntVal( "CfgFileCreate" ) != 0 ) {
            fp = fopen( OrigAutoExec, "wt" );
            if( fp == NULL ) {
                MsgBox( NULL, "IDS_CANTCREATEFILE", GUI_OK, OrigAutoExec );
                return( FALSE );
            }
            fclose( fp );
        }
        strcpy( newcfg, GetVariableStrVal("CfgDir") );
        OrigAutoExec[0] = newcfg[0];
    }
#endif

    if( mod_type == MOD_IN_PLACE ) {
        // copy current files to AUTOEXEC.BAK and CONFIG.BAK

#ifndef __OS2__
        strcpy( newauto, OrigAutoExec );
        BackupName( newauto );
#endif
        strcpy( newcfg, OrigConfig );
        BackupName( newcfg );

#ifdef __OS2__
        MsgBox( NULL, "IDS_COPYCONFIGSYS", GUI_OK, newcfg );
#else
        MsgBox( NULL, "IDS_COPYAUTOEXEC", GUI_OK, newauto, newcfg );
#endif

#ifndef __OS2__
        if( DoCopyFile( OrigAutoExec, newauto, FALSE ) != CFE_NOERROR ) {
            MsgBox( NULL, "IDS_ERRORBACKAUTO", GUI_OK );
        } else {
            if( !ModAuto( newauto, OrigAutoExec ) ) {
                return( FALSE );
            }
        }
        if( DoCopyFile( OrigConfig, newcfg, FALSE ) != CFE_NOERROR ) {
            MsgBox( NULL, "IDS_ERRORBACKCONFIG", GUI_OK );
        } else {
            if( !ModConfig( newcfg, OrigConfig ) ) {
                return( FALSE );
            }
        }
#else
        if( DoCopyFile( OrigConfig, newcfg, FALSE ) != CFE_NOERROR ) {
            MsgBox( NULL, "IDS_ERRORBACKCONFIG", GUI_OK );
        } else {
            if( !ModOS2Config( newcfg, OrigConfig ) ) {
                return( FALSE );
            }
            MsgBox( NULL, "IDS_OS2CONFIGSYS", GUI_OK );
        }
#endif
        // indicate config files were modified if and only if we got this far
        ConfigModified = TRUE;
    } else {
        // place modifications in AUTOEXEC.NEW and CONFIG.NEW
#ifndef __OS2__
        GetOldConfigFileDir( newauto, OrigAutoExec );
        strcat( newauto, &OrigAutoExec[2] );
        #if defined(__NT__)
            ReplaceExt( newauto, "W95" );
        #else
            ReplaceExt( newauto, "DOS" );

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?