home *** CD-ROM | disk | FTP | other *** search
/ PC Active 2009 April / PCA224.ISO / Software / remotes / setupssh.exe / bin / switch.c < prev    next >
Encoding:
Text File  |  2002-08-02  |  3.1 KB  |  75 lines

  1. /*
  2.     SWITCH - Basic
  3.     Author: Mark Bradshaw
  4.     Email: mark@networksimplicity.com
  5.     
  6.     Switch attempts to make an intelligent choice as to what shell you need to
  7.     have invoked in order to properly carry out your command.  It is compiled
  8.     with gcc 2.95.3-5 under Cygwin (www.cygwin.com).
  9.  
  10.     When presented with no command (i.e. you just want a shell) it attempts to
  11.     located and execute cmd.exe (windows standard shell).  When given a command
  12.     to run (includes scp and sftp service) it switches to sh.exe as the correct
  13.     shell.  It determines that a command has been issued by checking the first
  14.     command line parameter.  It will be equal to "-c".
  15. */
  16.  
  17. int main (int argc, char *argv[ ]) {
  18.  
  19.     char command[255] = ""; 
  20.     int a,i; 
  21.  
  22.     strncpy(command, getenv ("COMSPEC"), sizeof(command)-1);
  23.     command[sizeof(command)-1] = '\0';
  24.  
  25. /*************************************************************************** 
  26. Set command equal to the default location of the windows shell if ComSpec is
  27. empty.  
  28. ***************************************************************************/
  29.     if ( ! strcmp(command, "") ) {
  30.         strncpy(command, getenv("SystemRoot"), sizeof(command) -1);
  31.         command[sizeof(command)-1] = '\0';
  32.         strncat(command, "/system32/cmd.exe", sizeof(command)-1-strlen(command));
  33.         }
  34.  
  35. /***************************************************************************
  36. There's a small problem that must be dealt with.  The cygwin "system" 
  37. command will take any backslashes (\) and interpret them as escape 
  38. characters.  Basically they'll disappear.  This will cause normal paths in
  39. ComSpec, like "c:\winnt\system32\cmd.exe", to become "c:winntsystem32cmd.exe".
  40. I switch all \'s to /'s.  Cygwin will take either.
  41. ***************************************************************************/
  42.     for (a=0; a < strlen(command); a++) 
  43.         if ( (char) command[a] == '\\' ) command[a] = '/';
  44.  
  45.         //add " /q" to the end of command to keep cmd.exe from repeating commands.
  46.         strncat(command, " /q", sizeof(command) - 1 - strlen(command));
  47.  
  48.     // If switch gets any command line arguments then switch to /bin/sh
  49.     if (argc>1 && !(strcmp(argv[1],"-c"))) {
  50.         // Empty command.
  51.         command[0]='\0';
  52.  
  53.         //Use /ssh/sh as the shell. 
  54.         strncat(command, "/bin/sh.exe -c \"", sizeof(command)-1);
  55.             
  56. /***************************************************************************
  57. Copy over the arguments allowing for up to 254 characters using strncat 
  58. instead of strcat to protect against buffer overflow.  Start at 2 since the
  59. "-c" isn't needed.
  60. ***************************************************************************/
  61.         for(i=2; i < argc; i++){
  62.             strncat(command, argv[i], sizeof(command)-1-strlen(command));
  63.             strncat(command, " ", sizeof(command)-1-strlen(command));
  64.               }
  65.         // Finish up with a final quotation mark.
  66.         strncat(command, "\"", sizeof(command)-1);
  67.         }
  68.  
  69.     // User can specify -p on the command line to print out the command.  Used for debugging.
  70.     if (argc>1 && !(strcmp(argv[1],"-p"))) printf("Command to execute: %s\n",command);
  71.  
  72.     // Use system to run the shell with whatever arguments it needs.
  73.     system(command);
  74.     return(0);
  75.     }