From fe8d423208cbb6e9fc4957824926f289d419aebc Mon Sep 17 00:00:00 2001 From: Scott Gasch Date: Wed, 1 Jun 2016 19:18:42 -0700 Subject: [PATCH] Initial checkin. --- myecho/myecho.c | 204 +++++++++++++++++++++++++++++++++++++ version/version.c | 249 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 453 insertions(+) create mode 100644 myecho/myecho.c create mode 100644 version/version.c diff --git a/myecho/myecho.c b/myecho/myecho.c new file mode 100644 index 0000000..c4657a2 --- /dev/null +++ b/myecho/myecho.c @@ -0,0 +1,204 @@ +/*++ + + >>> This code is released to the public domain. No warranty + whatsoever is provided, use it at your own risk. <<< + +Module Name: + + myecho.c + +Abstract: + + An "echo" replacement that can do color text. + + Invoke like this: + + myecho.exe [-fg color] [-bg color] "string to echo" + + color can be: blue, red, green, yellow, cyan, violet, pink, black, white + brightblue, brightred, brightgreen, brightyellow, + brightviolet, brightpink, brightblack, gray, grey, or + brightwhite + +Author: + + Scott Gasch (scott@wannabe.guru.org) 22 Aug 2002 + +Revision History: + +--*/ + +#include +#include +#include + +WORD +MakeColor(BOOL fBg, char *sz) +/*++ + +Routine description: + + Based on a string, return a color WORD. + +Parameters: + + BOOL fBg, + char *sz + +Return value: + + WORD + +--*/ +{ + WORD w = 0; + + // + // bright + // + if (strstr(sz, "bright")) + { + w = FOREGROUND_INTENSITY; + } + + // + // colors + // + if (strstr(sz, "blue")) + { + w |= FOREGROUND_BLUE; + } + if (strstr(sz, "red")) + { + w |= FOREGROUND_RED; + } + if (strstr(sz, "green")) + { + w |= FOREGROUND_GREEN; + } + if (strstr(sz, "yellow")) + { + w |= (FOREGROUND_RED | FOREGROUND_GREEN); + } + if (strstr(sz, "cyan")) + { + w |= (FOREGROUND_BLUE | FOREGROUND_GREEN); + } + if ((strstr(sz, "violet")) || + (strstr(sz, "pink"))) + { + w |= (FOREGROUND_BLUE | FOREGROUND_RED); + } + if (strstr(sz, "white")) + { + w |= (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); + } + if (strstr(sz, "black")) + { + w &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); + } + if (strstr(sz, "gray") || strstr(sz, "grey")) + { + w &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); + w |= FOREGROUND_INTENSITY; + } + if (fBg) + { + w <<= 4; + } + + return(w); +} + + +int __cdecl +main(int argc, char *argv[]) +/*++ + +Routine description: + + Program entry point; see above for usage. + +Parameters: + + int argc, + char *argv[] + +Return value: + + int __cdecl + +--*/ +{ + CONSOLE_SCREEN_BUFFER_INFO csbiInfo; + HANDLE hStdOut; + WORD wAttr; + int x; + BOOL fNewline = TRUE; + + // + // open stdout + // + hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); + if (INVALID_HANDLE_VALUE == hStdOut) + { + fprintf(stderr, "Failed to open handle to stdout, error=%u.\n", + GetLastError()); + exit(1); + } + + // + // Save the current text colors. + // + if (0 == GetConsoleScreenBufferInfo(hStdOut, &csbiInfo)) + { + fprintf(stderr, "Failed to save current text attr, error=%u\n", + GetLastError()); + exit(1); + } + + // + // echo the string(s) + // + x = 1; + while (x < argc) + { + if (!_strcmpi(argv[x], "-fg")) + { + x++; + if (x < argc) + { + SetConsoleTextAttribute(hStdOut, MakeColor(0, argv[x])); + } + x++; + } + else if (!_strcmpi(argv[x], "-bg")) + { + x++; + if (x < argc) + { + SetConsoleTextAttribute(hStdOut, MakeColor(1, argv[x])); + } + x++; + } + else if (!_strcmpi(argv[x], "-n")) + { + fNewline = FALSE; + x++; + } + else + { + printf(argv[x]); + if (TRUE == fNewline) + { + printf("\n"); + } + x++; + } + } + + // + // restore the old text colors + // + (void)SetConsoleTextAttribute(hStdOut, csbiInfo.wAttributes); +} diff --git a/version/version.c b/version/version.c new file mode 100644 index 0000000..0338091 --- /dev/null +++ b/version/version.c @@ -0,0 +1,249 @@ +/*++ + + >>> This code is released to the public domain. No warranty + whatsoever is provided. Use it at your own risk. <<< + +Module Name: + + version.c + +Abstract: + + Determine the Windows version number running and set the + errorlevel appropriately. Intended for use with batch scripts. + +Author: + + Scott Gasch (scott@wannabe.guru.org) 22 Aug 2002 + +Revision History: + +--*/ + +#include +#include +#include + +// +// This code _should_ run on win9x/ME or any version of NT. Note: I have +// not personally tested it on 9x/ME though. It prints out a message on +// stdout about which version of the windows operating system is running +// and sets the errorlevel to one of the values below accordingly. Might +// be useful in your CMD/BAT script... +// +#define EXIT_WIN95 (1) +#define EXIT_WIN98 (2) +#define EXIT_WINME (3) +#define EXIT_CONSUMER_OTHER (4) +#define EXIT_NT351 (5) +#define EXIT_NT3OTHER (6) +#define EXIT_NT4 (7) +#define EXIT_NT4OTHER (8) +#define EXIT_WIN2K (9) +#define EXIT_WINXP (10) +#define EXIT_WIN_SERVER_2003 (11) +#define EXIT_NT5OTHER (12) +#define EXIT_VISTA (13) +#define EXIT_UNKNOWN (14) + +CHAR *g_szWindowsNames[] = +{ + "Not Used", + "Windows95", + "Windows98", + "WindowsME", + "Unknown Consumer Windows Release", + "Windows NT", + "Unknown Windows NT", + "Windows NT", + "Unknown Windows NT", + "Windows 2000", + "WindowsXP", + "Windows Server 2003", + "Unknown Windows NT", + "Longhorn Prerelease", + "Unknown Operating System" +}; + +typedef BOOL (*FUNCTION_PTR)(OSVERSIONINFO *); + + +BOOL +TryWinNT(DWORD *pdwMajor, DWORD *pdwMinor, DWORD *pdwPlatform) +/*++ + +Routine description: + + GetVersionEx is not present on downlevel versions of Windows. + Deal with this by using LoadLibrary/GetProcAddress. + +Parameters: + + DWORD *pdwMajor, + DWORD *pdwMinor, + DWORD *pdwPlatform + +Return value: + + BOOL + +--*/ +{ + FUNCTION_PTR p; + BOOL fRet = FALSE; + HMODULE h = NULL; + OSVERSIONINFOEXA osvi; + + h = LoadLibraryA("kernel32.dll"); + if (NULL == h) + { + fprintf(stderr, "Failed to load kernel32.dll, error=%u.\n", + GetLastError()); + goto end; + } + + p = (FUNCTION_PTR)GetProcAddress(h, "GetVersionExA"); + if (NULL == p) + { + fprintf(stderr, "GetProcAddress for GetVersionEx failed, error=%u.\n", + GetLastError()); + goto end; + } + + ZeroMemory(&osvi, sizeof(osvi)); + osvi.dwOSVersionInfoSize = sizeof(osvi); + fRet = (*p)((OSVERSIONINFO *)&osvi); + if (FALSE == fRet) + { + fprintf(stderr, "GetVersionEx failed, error=%u.\n", GetLastError()); + goto end; + } + *pdwMajor = osvi.dwMajorVersion; + *pdwMinor = osvi.dwMinorVersion; + *pdwPlatform = osvi.dwPlatformId; + + end: + if (NULL != h) + { + FreeLibrary(h); + } + + return(fRet); +} + + +int __cdecl +main(void) +/*++ + +Routine description: + + Program entry point. + +Parameters: + + void + +Return value: + + int __cdecl + +--*/ +{ + DWORD dwMajor, dwMinor, dwPlatform; + DWORD dwVersion; + int iRet = EXIT_UNKNOWN; + + // + // This call should be available on all versions of Windows + // + dwVersion = GetVersion(); + if ((dwVersion & 0x80000000) || + (FALSE == TryWinNT(&dwMajor, &dwMinor, &dwPlatform))) + { + dwMajor = (DWORD)(LOBYTE(LOWORD(dwVersion))); + dwMinor = (DWORD)(HIBYTE(LOWORD(dwVersion))); + dwPlatform = VER_PLATFORM_WIN32_WINDOWS; + } + + // + // Parse results + // + if (dwPlatform == VER_PLATFORM_WIN32_NT) + { + switch(dwMajor) + { + case 3: + iRet = EXIT_NT3OTHER; + if (51 == dwMinor) + { + iRet = EXIT_NT351; + } + goto end; + + case 4: + iRet = EXIT_NT4OTHER; + if (0 == dwMinor) + { + iRet = EXIT_NT4; + } + goto end; + + case 5: + iRet = EXIT_NT5OTHER; + if (0 == dwMinor) + { + iRet = EXIT_WIN2K; + } + else if (1 == dwMinor) + { + iRet = EXIT_WINXP; + } + else if (2 == dwMinor) + { + iRet = EXIT_WIN_SERVER_2003; + } + goto end; + + case 6: + iRet = EXIT_VISTA; + goto end; + + default: + goto end; + } + } + else if (dwPlatform == VER_PLATFORM_WIN32_WINDOWS) + { + switch(dwMajor) + { + case 4: + iRet = EXIT_CONSUMER_OTHER; + if (0 == dwMinor) + { + iRet = EXIT_WIN95; + } + else if (10 == dwMinor) + { + iRet = EXIT_WIN98; + } + else if (90 == dwMinor) + { + iRet = EXIT_WINME; + } + goto end; + + default: + goto end; + } + } + + end: + printf("%s %u.%u -- errorlevel is %d\n", + g_szWindowsNames[iRet], + dwMajor, + dwMinor, + iRet); + exit(iRet); +} + -- 2.46.1