home *** CD-ROM | disk | FTP | other *** search
- /*
- SH.C -- Command Shell for Windows
-
- Copyright (c) Andrew Schulman 1991, 1992
-
- From Chapter 4 of "Undocumented Windows" (Addison-Wesley 1992)
- by Andrew Schulman, Dave Maxey and Matt Pietrek
-
- Build using: WINIOBC SH (for Borland C++ v3.00)
- WINIOMS SH (for Microsoft C/SDK)
- */
-
- #include "windows.h"
- #include <stdlib.h>
- #include <string.h>
- #ifdef __BORLANDC__
- #define __MSC
- #include <dir.h>
- #else
- #include <direct.h>
- #endif
- #include <dos.h>
- #include "winio.h"
-
- int do_command(char *s);
- char *pwd(void);
-
- static char buf[2048] = {0};
- static char orig[2048] = {0};
- static char title[80] = "Command Shell";
- static unsigned instance = 0;
- static HWND hwnd_sh;
-
- int main()
- {
- winio_about("SH - A Command Shell for Windows"
- "\n\nFrom Chapter 4 of"
- "\n\"Undocumented Windows\" (Addison-Wesley, 1992)"
- "\nby Andrew Schulman, David Maxey and Matt Pietrek");
-
- hwnd_sh = winio_current();
- winio_settitle(hwnd_sh, title);
- winio_setfont(hwnd_sh, ANSI_FIXED_FONT);
-
- for (;;)
- {
- printf("%s>", pwd());
- gets(buf);
- if ((buf[0] == '\0') || (buf[0] == '\n'))
- continue;
- strcpy(orig, buf); // unmodified buffer
- strupr(buf);
- if (strcmp(buf, "EXIT") == 0)
- break;
- if (! do_command(buf))
- puts("Bad command or file name");
- }
-
- winio_close(hwnd_sh);
- return 0;
- }
-
- char *pwd(void)
- {
- static char buf[256];
- char *s;
- return ((s = getcwd(buf, 255)) != 0) ? s : "invalid";
- }
-
- // NOTE: running multiple instances, Windows takes care of maintaining
- // multiple pwd within each instance
- int do_chdrive(char *s)
- {
- #ifdef __BORLANDC__
- return (s[1] == ':') ? setdisk(s[0] - 'A') : 0;
- #else
- return (s[1] == ':') ? (_chdrive(1 + s[0] - 'A') == 0) : 0;
- #endif
- }
-
- int do_cd(char *s)
- {
- if (s[1] == ':')
- {
- // allow drives in here too
- if (! do_chdrive(s)) return 0;
- s += 2;
- }
- return (chdir(s) == 0);
- }
-
- void show_filename(struct find_t *pinfo)
- {
- if (pinfo->attrib & _A_SUBDIR)
- printf("%-13s\t<DIR>\n", pinfo->name);
- else
- printf("%-13s\t%9lu\n", pinfo->name, pinfo->size);
- }
-
- int do_dir(char *s)
- {
- struct find_t info;
- char wildcard[80];
- unsigned long bytes;
- unsigned attrib, files;
- strcpy(wildcard, s);
- if (! strchr(wildcard, '.'))
- strcat(wildcard, "*.*");
- attrib = _A_NORMAL | _A_SUBDIR | _A_RDONLY;
- if (_dos_findfirst(wildcard, attrib, &info) != 0)
- return 0;
- files = 1;
- bytes = info.size;
- winio_setpaint(hwnd_sh,FALSE);
- show_filename(&info);
- while (_dos_findnext(&info) == 0)
- {
- show_filename(&info);
- files++;
- bytes += info.size;
- }
- printf("%5u File(s)\t%lu bytes\n", files, bytes);
- winio_setpaint(hwnd_sh,TRUE);
- return 1;
- }
-
- int do_activate(char *s)
- {
- HWND hwnd = FindWindow(NULL, s);
- if (! hwnd) return FALSE;
- BringWindowToTop(hwnd);
- if (IsIconic(hwnd)) ShowWindow(hwnd, SW_RESTORE);
- return TRUE;
- }
-
- // distinguish between built-in that failed, and not built-in
- #define NOT_BUILTIN -1
-
- #define MATCH(str, func) if (strcmp(cmd, str) == 0) return func(args)
-
- int do_builtin(char *s)
- {
- char *cmd = strtok(s, " \t");
- char *args = strtok(0, "\t");
- if (strcmp(cmd, "ACTIVATE") == 0)
- return do_activate(&orig[args-cmd]); // unmodified string
- else if ((strlen(cmd) == 2) && (cmd[1] == ':')) // drive:
- return do_chdrive(cmd);
- else MATCH("CD", do_cd);
- else MATCH("DIR", do_dir);
- return NOT_BUILTIN;
- }
-
- int do_command(char *s)
- {
- int ret;
- if ((ret = do_builtin(s)) == NOT_BUILTIN)
- {
- unsigned retval;
- ret = ((retval = WinExec(orig, SW_SHOWNORMAL)) > 32);
- printf("%04X\n", retval);
- }
- return ret;
- }
-
-