home *** CD-ROM | disk | FTP | other *** search
/ Mega CD-ROM 1 / megacd_rom_1.zip / megacd_rom_1 / MAGAZINE / MSJOURNA / MSJV6_4.ZIP / WINSTDIO.ZIP / SH.C < prev    next >
C/C++ Source or Header  |  1991-07-01  |  4KB  |  171 lines

  1. /*
  2. SH.C -- Command Shell for Windows
  3.  
  4. Microsoft C, Windows SDK:
  5. cl -c -AS -Gsw -Oais -Zpe sh.c winio.c wmhandlr.c
  6. link sh winio wmhandlr,sh,nul,/nod slibcew libw,winio.def
  7. rc winio.rc sh.exe
  8.  
  9. Borland C++:
  10. bcc -WS -G -O -Z -w-par -Hu sh.c winio.c wmhandlr.c
  11. rc winio.rc sh.exe
  12. */
  13.  
  14. #include "windows.h"
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #ifdef __BORLANDC__
  18. #define __MSC
  19. #include <dir.h>
  20. #else
  21. #include <direct.h>
  22. #endif
  23. #include <dos.h>
  24. #include "winio.h"
  25.  
  26. int do_command(char *s);
  27. char *pwd(void);
  28.  
  29. static char buf[2048] = {0};
  30. static char orig[2048] = {0};
  31. static char title[80] = "Command Shell";
  32. static unsigned instance = 0;
  33.  
  34. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, 
  35.     LPSTR lpCmdLine, int nCmdShow)
  36. {
  37.     if (hPrevInstance)
  38.         GetInstanceData(hPrevInstance, &instance, sizeof(instance));
  39.     if (++instance > 1)
  40.         sprintf(title, "Command Shell #%d", instance);
  41.     winio_settitle(title);
  42.     
  43.     if (! winio_init(hInstance, hPrevInstance, nCmdShow, 0))
  44.         return 1;
  45.     
  46.     winio_setfont(ANSI_FIXED_FONT);
  47.     
  48.     for (;;)
  49.     {
  50.         printf("%s>", pwd());
  51.         gets(buf);
  52.         if ((buf[0] == '\0') || (buf[0] == '\n')) 
  53.             continue;
  54.         strcpy(orig, buf);  // unmodified buffer
  55.         strupr(buf);
  56.         if (strcmp(buf, "EXIT") == 0) 
  57.             break;
  58.         if (! do_command(buf))
  59.             puts("Bad command or file name");
  60.     }
  61.     
  62.     winio_close();
  63.     return 0;
  64. }
  65.  
  66. char *pwd(void)
  67. {
  68.     static char buf[256];
  69.     char *s;
  70.     return (s = getcwd(buf, 255)) ? s : "invalid";
  71. }
  72.  
  73. // NOTE: running multiple instances, Windows takes care of maintaining
  74. // multiple pwd within each instance
  75. int do_chdrive(char *s)
  76. {
  77. #ifdef __BORLANDC__ 
  78.     return (s[1] == ':') ? setdisk(s[0] - 'A') : 0;
  79. #else
  80.     return (s[1] == ':') ? (_chdrive(1 + s[0] - 'A') == 0) : 0;
  81. #endif
  82. }
  83.  
  84. int do_cd(char *s)
  85. {
  86.     if (s[1] == ':')
  87.     {
  88.         // allow drives in here too
  89.         if (! do_chdrive(s)) return 0;
  90.         s += 2;
  91.     }
  92.     return (chdir(s) == 0);
  93. }
  94.  
  95. void show_filename(struct find_t *pinfo)
  96. {
  97.     if (pinfo->attrib & _A_SUBDIR)
  98.         printf("%-13s\t<DIR>\n", pinfo->name);
  99.     else
  100.         printf("%-13s\t%9lu\n", pinfo->name, pinfo->size);
  101. }
  102.  
  103. int do_dir(char *s)
  104. {
  105.     struct find_t info;
  106.     char wildcard[80];
  107.     unsigned long bytes;
  108.     unsigned attrib, files;
  109.     strcpy(wildcard, s);
  110.     if (! strchr(wildcard, '.'))
  111.         strcat(wildcard, "*.*");
  112.     attrib = _A_NORMAL | _A_SUBDIR | _A_RDONLY;
  113.     if (_dos_findfirst(wildcard, attrib, &info) != 0)
  114.         return 0;
  115.     files = 1;
  116.     bytes = info.size;
  117.     winio_setpaint(FALSE);
  118.     show_filename(&info);
  119.     while (_dos_findnext(&info) == 0)
  120.     {
  121.         show_filename(&info);
  122.         files++;
  123.         bytes += info.size;
  124.     }
  125.     printf("%5u File(s)\t%lu bytes\n", files, bytes);
  126.     winio_setpaint(TRUE);
  127.     return 1;
  128. }
  129.  
  130. int do_activate(char *s)
  131. {
  132.     HWND hwnd = FindWindow(NULL, s);
  133.     if (! hwnd) return FALSE;
  134.     BringWindowToTop(hwnd);
  135.     if (IsIconic(hwnd)) ShowWindow(hwnd, SW_RESTORE);
  136.     return TRUE;
  137. }
  138.  
  139. // distinguish between built-in that failed, and not built-in
  140. #define NOT_BUILTIN     -1
  141.  
  142. #define MATCH(str, func)  if (strcmp(cmd, str) == 0) return func(args)
  143.  
  144. int do_builtin(char *s)
  145. {
  146.     char *cmd = strtok(s, " \t");
  147.     char *args = strtok(0, "\t");
  148.     if (strcmp(cmd, "ACTIVATE") == 0)
  149.         return do_activate(&orig[args-cmd]);    // unmodified string
  150.     else if ((strlen(cmd) == 2) && (cmd[1] == ':'))     // drive:
  151.         return do_chdrive(cmd);
  152.     else MATCH("CD", do_cd);
  153.     else MATCH("DIR", do_dir);
  154.     return NOT_BUILTIN;
  155. }
  156.  
  157. int do_command(char *s)
  158. {
  159.     int ret; 
  160.     if ((ret = do_builtin(s)) == NOT_BUILTIN)
  161.     {
  162.         unsigned retval;
  163.         HWND focus = GetFocus();    // changed from MSJ version
  164.         ret = ((retval = WinExec(orig, SW_SHOWMINIMIZED)) > 32);
  165.         SetFocus(focus);            // changed from MSJ version
  166.         printf("%04X\n", retval);
  167.     }
  168.     return ret;
  169. }
  170.  
  171.