📄 cehelp.c
字号:
#include <cehelp.h>#define MAX_ENVIRONMENT 256static TCHAR s_szCurrentDir[MAX_PATH] = _T("\\windows");typedef struct { WCHAR *Name; WCHAR *Value;}t_Environment;static t_Environment s_EnvironmentBlock[MAX_ENVIRONMENT] = {0};static TCHAR* up(TCHAR *dir){ TCHAR *tail; if(!_tcscmp(dir, _T("\\"))) return dir; tail = _tcsrchr(dir, _T('\\')); if(tail == dir) { *(tail+1) = 0; }else { *tail = 0; } return dir;}static TCHAR* cd(TCHAR* dir, TCHAR* path){ TCHAR *tailpath; if(!*path) // "cd" --> nothing to do { return dir; } if(!_tcscmp(path, _T("\\")) || !_tcscmp(path, _T("/"))) // "cd \" --> path = \ { _tcscpy(dir,_T("\\")); return dir; } if((*path == _T('\\')) || (*path == _T('/'))) // "cd \X" --> path = \X { _tcscpy(dir,path); return dir; } if(!_tcscmp(path, _T(".."))) { return up(dir); // "cd .." --> go up one level } if(!_tcsncmp(path, _T("..\\"),3) || !_tcsncmp(path, _T("../"),3)) { path += 3; if(*path == _T('\\')) // "cd ..\\" --> not supported return NULL; return cd(up(dir),path); // "cd ..\X" --> go up one level and recurse on X } if(!_tcscmp(path, _T("."))) { return dir; // "cd ." --> nothing to do } if(!_tcsncmp(path, _T(".\\"),2) << !_tcsncmp(path, _T("./"),2)) { path += 2; if(*path == _T('\\')) return NULL; // "cd .\\" --> not supported return cd(dir,path); // "cd .\X" --> recurse on X } if((tailpath = _tcschr(path, _T('\\'))) || (tailpath = _tcschr(path, _T('/')))) { *tailpath = 0; tailpath++; if(*tailpath == _T('\\')) // "cd Y\\X" --> not supported return NULL; _tcscat(dir, path); return cd(dir, tailpath); // "cd Y\X" --> append Y on dir and recurse on X } if(_tcslen(dir) > 1) // "cd Y" --> append Y\ on dir { _tcscat(dir,_T("\\")); } _tcscat(dir,path); return dir;}DWORD PPcGetCurrentDirectory( DWORD nBufferLength, // size of directory buffer LPTSTR lpBuffer // directory buffer){ int len; _tcsncpy(lpBuffer, s_szCurrentDir, nBufferLength); len = _tcslen(s_szCurrentDir); return min((int)nBufferLength, len);}BOOL PPcSetCurrentDirectory( LPCTSTR lpPathName // new directory name){ TCHAR path[MAX_PATH]; TCHAR dir[MAX_PATH]; DWORD attr; _tcscpy(path, lpPathName); _tcscpy(dir, s_szCurrentDir); if(!cd(dir,path)) return FALSE; attr = GetFileAttributes(dir); if(attr == -1) return FALSE; if(GetFileAttributes(dir) & FILE_ATTRIBUTE_DIRECTORY ) { _tcscpy(s_szCurrentDir, dir); return TRUE; } return FALSE;}DWORD PPcGetFullPathName( LPCTSTR lpFileName, // file name DWORD nBufferLength, // size of path buffer LPTSTR lpBuffer, // path buffer LPTSTR *lpFilePart // address of file name in path){ DWORD len; if(lpFileName[0] == _T('\\')) { _tcscpy(lpBuffer, lpFileName); }else { _tcscpy(lpBuffer, s_szCurrentDir); _tcscat(lpBuffer, lpFileName); } len = _tcslen(s_szCurrentDir); *lpFilePart = lpBuffer+len; return len;}DWORD PPcGetEnvironmentVariable( LPCTSTR lpName, // environment variable name LPTSTR lpBuffer, // buffer for variable value DWORD nSize // size of buffer){ int i; DWORD nCopied; for(i=0;i< MAX_ENVIRONMENT;i++) { if(s_EnvironmentBlock[i].Name && ! wcsicmp(s_EnvironmentBlock[i].Name,lpName)) { nCopied = min(nSize, wcslen(s_EnvironmentBlock[i].Value)); wcsncpy(lpBuffer, s_EnvironmentBlock[i].Value, nCopied); return nCopied; } } return 0;}BOOL PPcSetEnvironmentVariable( LPCTSTR lpName, // environment variable name LPCTSTR lpValue // new value for variable){ int i; for(i=0;i< MAX_ENVIRONMENT;i++) { if(s_EnvironmentBlock[i].Name && !wcsicmp(s_EnvironmentBlock[i].Name,lpName)) { free(s_EnvironmentBlock[i].Name); s_EnvironmentBlock[i].Name = 0; free(s_EnvironmentBlock[i].Value); s_EnvironmentBlock[i].Value = 0; break; } } for(i=0;i< MAX_ENVIRONMENT;i++) { if(!s_EnvironmentBlock[i].Name) { s_EnvironmentBlock[i].Name = malloc((wcslen(lpName)+1)*sizeof(WCHAR)); s_EnvironmentBlock[i].Value = malloc((wcslen(lpValue)+1)*sizeof(WCHAR)); wcscpy(s_EnvironmentBlock[i].Name, lpName); wcscpy(s_EnvironmentBlock[i].Value, lpValue); return TRUE; } } return FALSE;}BOOL PPcFreeEnvironmentStrings( LPTSTR lpszEnvironmentBlock // environment strings){ free(lpszEnvironmentBlock); return TRUE;}LPWSTR PPcGetEnvironmentStrings(VOID){ LPWSTR lpszEnvironment; LPWSTR pString; int i; int nSize = 0; for(i=0;i<MAX_ENVIRONMENT;i++) { if(s_EnvironmentBlock[i].Name) { nSize+= wcslen(s_EnvironmentBlock[i].Name); } if(s_EnvironmentBlock[i].Value) { nSize+= wcslen(s_EnvironmentBlock[i].Name); nSize++; } nSize++; } nSize++; pString = lpszEnvironment = malloc(nSize*sizeof(WCHAR)); *lpszEnvironment = 0; for(i=0;i<MAX_ENVIRONMENT;i++) { if(s_EnvironmentBlock[i].Name) { wcscpy(pString,s_EnvironmentBlock[i].Name); pString += wcslen(s_EnvironmentBlock[i].Name); } if(s_EnvironmentBlock[i].Value) { wcscpy(pString,L"="); pString++; wcscpy(pString,s_EnvironmentBlock[i].Value); pString += wcslen(s_EnvironmentBlock[i].Value)+1; } } *pString = 0; return lpszEnvironment;}DWORD PPcGetFileType( HANDLE hFile // handle to file){ BY_HANDLE_FILE_INFORMATION fi; if(isatty(hFile)) { return FILE_TYPE_CHAR; } if(GetFileInformationByHandle(hFile, &fi)) { return FILE_TYPE_DISK; } return FILE_TYPE_UNKNOWN;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -