home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / SWALIBAS.ZIP / SW_SHELL.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-09  |  2.5 KB  |  108 lines

  1. /* sw_shell.c - maybe invoke a shell to execute a script.
  2.    Copyright (C) 1990 by Thorsten Ohl, td12@ddagsi3.bitnet
  3.  
  4.    This file is part of SWAPLIB (the library), a library for efficient
  5.    execution of child processes under MS-DOS.
  6.  
  7.    The library is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 1, or (at your option)
  10.    any later version.
  11.  
  12.    The library is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.  
  17.    You should have received a copy of the GNU General Public License
  18.    along with the library; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.    $Header: e:/gnu/swaplib/RCS/sw_shell.c'v 0.9 90/09/09 21:44:10 tho Stable $
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27.  
  28. #include "swaplib.h"
  29.  
  30. extern void *xmalloc (size_t size);
  31.  
  32. char *
  33. swap_invoke_shell (char *cmd, char ***argvp)
  34. {
  35.   char *p = NULL;
  36.   char *suffix;
  37.   char *dash_c = "";
  38.  
  39.   if (!cmd || !argvp)
  40.     return NULL;
  41.  
  42.   suffix = strrchr (cmd, '.');
  43.  
  44.   if (!suffix)
  45.     /* Strange..., let the caller handle this!  (Later we might
  46.        want to support the `#!' construction.)  */
  47.     return NULL;
  48.   else
  49.     suffix++;
  50.  
  51.   if (strcmp (suffix, "bat") == 0)
  52.     {
  53.       p = getenv ("COMSPEC");
  54.       dash_c = "/c";
  55.       swap_back_slashify (cmd);
  56.     }
  57.   else if (strcmp (suffix, "sh") == 0)
  58.     {
  59.       p = getenv ("SHELL");
  60.       dash_c = "-c";
  61.     }
  62.  
  63.   if (p)
  64.     {
  65.       int i;
  66.       int argc = 0;
  67.       char **ap = *argvp;
  68.  
  69.       char **argv;
  70.       char *shell = (char *) xmalloc (strlen (p) + 1);
  71.  
  72.       strcpy (shell, p);
  73.  
  74.       while (*ap++)
  75.     argc++;
  76.  
  77.       argv = (char **) xmalloc ((argc + 3) * sizeof (char *));
  78.  
  79.       /*  Shift right by two.  */
  80.       for (i = 1; i < argc; i ++)
  81.     argv[i + 2] = (*argvp)[i];
  82.  
  83.       /* Leave ARGV[0] intact, but insert the script's name
  84.      into the arguments.  */
  85.  
  86.       argv[0] = (*argvp)[0];
  87.       argv[1] = (char *) xmalloc (3);
  88.       strcpy (argv[1], dash_c);
  89.       argv[2] = cmd;
  90.       argv[argc + 2] = NULL;
  91.  
  92.       *argvp = argv;
  93.  
  94.       return shell;
  95.     }
  96.   else
  97.     return NULL;
  98. }
  99.  
  100.  
  101. /* 
  102.  * Local Variables:
  103.  * mode:C
  104.  * ChangeLog:ChangeLog
  105.  * compile-command:make
  106.  * End:
  107.  */
  108.