📄 main.c
字号:
/*
* main.c: svnpath - Edit system path for Inno Setup Windows installer.
*
* USAGE:
* svnpath --help
*
* ====================================================================
* Copyright (c) 2000-2004 CollabNet. All rights reserved.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at http://subversion.tigris.org/license-1.html.
* If newer versions of this license are posted there, you may use a
* newer version instead, at your option.
*
* This software consists of voluntary contributions made by many
* individuals. For exact contribution history, see the revision
* history and logs, available at http://subversion.tigris.org/.
* ====================================================================
* Compiling with MinGW (use version 2.x with gcc 3.2 or better):
* Make sure that MinGW/bin is in your path and type:
* windres.exe -i svnpath.rc -I rc -o svnpath.res -O coff
* gcc -s -Os -Wall -mwindows -march=i386 -o svnpath.exe svnpath.res main.c
* Compiling with MS Visual C (use VC 5.x.):
* Make a new Win32 Console Application project with the name svnpath
* and add this file to your project.
* NOTE: Do not even think about using something newer than VC 5.x. This is
* an installation program and the required runtime files are newer
* than some of the targed OS's (Win 2000 and older).
* Compiling with the free Borland compiler bcc55:
* Make sure that the bcc bin directory is in your path and type:
* bcc32.exe -WC -O1 -fp -esvnpath main.c
*
* NOTES:
* * Some Win32 API equivalents are used in stead of the standard C functions
* in order to reduce executable size (when compiled with VC).
* This functions as: lstrcpy, lstrcat.
* * Keep away from Cygwin and pre MinGW 2.x. This app must run on all Win32
* OS's independed of any extra dll's.
*/
/* ==================================================================== */
/*** Includes. ***/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <io.h>
#include <sys\stat.h>
/*** Constants ***/
#define BUFSIZE 4000
/*** Global variables ***/
static char g_AuExBatFile[17] = "C:\\Autoexec.bat";
static char g_AuExSvnFile[17] = "C:\\Autoexec.svn";
char g_cSvnLineRem1[80]; /* Look at the svn_set_auexlines routine */
char g_cSvnLineRem2[80]; /* for setting the values */
char g_cSvnLinePath[256]; /* */
/*** Prototypes ***/
int svn_add9x (char cPath[255]);
int svn_addnt (char cPath[BUFSIZE]);
void svn_error_msg(char cMsg[255]);
int svn_os_is_nt();
int svn_print_help();
int svn_read_regval (HKEY hKey, char cValue[10], char cKey[BUFSIZE],
char *pcPathCur[BUFSIZE], DWORD *lpType);
int svn_remove9x (char cPath[255]);
int svn_removent (char cPath[255]);
int svn_run_cmd (char cAction[10], char cPath[255]);
int svn_set_auexlines (char cPath[255]);
int svn_svnpath_exists (char cPath[255]);
/*** Main. ***/
/*
* Initial program flow
*/
int
main (int argc, char *argv[])
{
int counter=0, iCmdArgError=1, iRetVal=1;
char cMsg[150];
switch (argc)
{
case 1: /* missing arguments */
lstrcpy ( cMsg, "Missing arguments.");
svn_error_msg(cMsg);
iRetVal = 65;
iCmdArgError=0;
break;
case 2: /* help */
if (! strcmp(argv[1], "--help") || ! strcmp(argv[1], "-h"))
{
iRetVal=svn_print_help();
iCmdArgError=0;
}
break;
case 3: /* add|remove path */
if (! strcmp(argv[1], "add") || ! strcmp(argv[1], "remove"))
{
iRetVal=svn_run_cmd(argv[1], argv[2]);
iCmdArgError=0;
}
break;
default:
iRetVal = 1;
}
if (iCmdArgError)
{
/* It's still hope to run a command when another program (IS) has
* started svnpath, so we will try to resolve it. */
lstrcpy ( cMsg, "Argument Error: Wrong arguments\n\n");
lstrcat ( cMsg, "This program received the following arguments:");
for (counter=1; counter<argc; counter++)
{
lstrcat ( cMsg, "\n '");
lstrcat ( cMsg, argv[counter]);
lstrcat ( cMsg, "'");
}
if ((!strcmp(argv[1], "add") || !strcmp(argv[1], "remove")) && (argc > 3))
{
iRetVal=svn_run_cmd(argv[1], argv[2]);
iCmdArgError=0;
}
else
{
svn_error_msg(cMsg);
iRetVal = 1;
}
}
return (iRetVal);
}
/*** svn_add9x ***/
/*
* Adding the path to the %PATH% environment in Autoexec.bat for Win9x
*/
int
svn_add9x (char cPath[255])
{
char cSvnCnt[1024];
int iAutoBatRo=0;
FILE *FH_AUBAT;
/* Fill up cSvnPath with the svn contents of Autoexec.bat */
svn_set_auexlines(cPath);
lstrcpy (cSvnCnt, g_cSvnLineRem1);
lstrcat (cSvnCnt, g_cSvnLineRem2);
lstrcat (cSvnCnt, g_cSvnLinePath);
/* Make a backup of Autoexec.bat to Autoexec.svn if it exists, write the
* svn stuff to Autoexec.bat */
if( _access(g_AuExBatFile, 0 ) != -1)
{
/* The file exists, so we make sure that we have write permission before
* we continue*/
if((_access(g_AuExBatFile, 2)) == -1)
{
_chmod(g_AuExBatFile, _S_IWRITE);
iAutoBatRo=1;
}
/* Make the backup */
CopyFileA(g_AuExBatFile, g_AuExSvnFile, FALSE);
}
/* Write the svn stuff to the file */
FH_AUBAT = fopen(g_AuExBatFile, "a+t");
fputs(cSvnCnt, FH_AUBAT);
fclose(FH_AUBAT);
/* Turn back to Read only if that was the original state */
if (iAutoBatRo)
{
_chmod(g_AuExBatFile, _S_IREAD);
}
return 0;
}
/*** svn_addnt ***/
/*
* Adding the path to the %PATH% environment in the registry on Win-NT's
*/
int
svn_addnt (char cPathSvn[255])
{
long lRet;
char cPathTmp[BUFSIZE];
HKEY hKey;
char cKey[BUFSIZE], cPathNew[BUFSIZE], cPathCur[BUFSIZE];
DWORD dwBufLen, lpType;
char *pcPathCur[BUFSIZE];
dwBufLen=BUFSIZE;
*pcPathCur=cPathCur;
lstrcpy (cPathTmp, cPathSvn);
if (svn_svnpath_exists(cPathTmp))
{
exit (1);
}
lstrcpy(cKey, "SYSTEM\\CurrentControlSet\\");
lstrcat(cKey, "Control\\Session Manager\\Environment");
/* Get value, value type and current path from HKLM and try to append
* the svnpath to it */
svn_read_regval (HKEY_LOCAL_MACHINE, "Path", cKey, &*pcPathCur, &lpType);
/* Reopen the key for writing */
lRet = RegCreateKeyEx(
HKEY_LOCAL_MACHINE, cKey, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
&hKey, &dwBufLen);
/* Add the subversion path to the path */
lstrcpy(cPathNew, cPathCur);
lstrcat(cPathNew, ";");
lstrcat(cPathNew, cPathSvn);
lRet = RegSetValueExA(hKey, "Path", 0, lpType,
(BYTE*)cPathNew, strlen(cPathNew)+1);
RegCloseKey(hKey);
/* If it went wrong to do it with HKLM, then try HKCU */
if (lRet != 0)
{
strcpy (cPathCur, "");
lRet = svn_read_regval(HKEY_CURRENT_USER, "Path",
"Environment", &*pcPathCur, &lpType);
/* Current Path may be empty */
cPathNew[0] = 0;
if (strlen(cPathCur))
{
lstrcpy(cPathNew, cPathCur);
lstrcat(cPathNew, ";");
}
else
lpType = REG_EXPAND_SZ;
lstrcat(cPathNew, cPathSvn);
/* Reopen the key for writing */
lRet = RegCreateKeyEx(
HKEY_CURRENT_USER, "Environment", 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
&hKey, &dwBufLen);
lRet = RegSetValueExA(hKey, "Path", 0, lpType,
(LPBYTE)cPathNew, strlen(cPathNew)+1);
RegCloseKey(hKey);
}
if (lRet != 0)
{
return (1);
}
else
{
long lRet;
/* Tell the system about the new path */
SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
(LPARAM) "Environment", SMTO_ABORTIFHUNG,
5000, &lRet);
return (0);
}
}
/*** svn_error_msg ***/
/*
* Displays a message box with a error message
*/
void
svn_error_msg(char cMsg[150])
{
long lRet;
long lMsgBoxFlag=MB_YESNO+MB_ICONWARNING+MB_SETFOREGROUND+MB_TOPMOST;
lstrcat(cMsg, "\n\nDo you want to read the help for svnpath?");
lRet=MessageBox(0, cMsg, "svnpath - Error" , lMsgBoxFlag);
if (lRet==IDYES)
{
svn_print_help();
}
}
/*** svn_os_is_nt ***/
/*
* Determing if the OS type is Windows NT or not. Returns 1 if true
*/
int
svn_os_is_nt()
{
/* NOTE: Use OSVERSIONINFO and not OSVERSIONINFOEX, older VC's have bogus
* headers */
int iRetVal=0;
OSVERSIONINFO osvi;
BOOL bOsVersionInfoEx;
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
{
osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )
{
exit (1);
}
}
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
iRetVal=1;
}
return (iRetVal);
}
/*** svn_print_help ***/
/*
* Printing out help on the console
*/
int
svn_print_help()
{
char cMsgBoxCaption[80];
char cMsgBoxMsg[1024];
long lMsgBoxFlag=MB_OK+MB_ICONINFORMATION+MB_SETFOREGROUND;
lstrcpy(cMsgBoxCaption, "Help for svnpath");
lstrcpy(cMsgBoxMsg, "svnpath - Add/remove a path on the system's PATH environment variable\n\n");
lstrcat(cMsgBoxMsg, "usage:\tsvnpath add|remove \"Path\"\n");
lstrcat(cMsgBoxMsg, "\tsvnpath -h|--help\n\n");
lstrcat(cMsgBoxMsg, "Example:\tsvnpath add \"C:\\Path\\to\\svn.exe\"\n\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -