missing/os2.c


DEFINITIONS

This source file includes following functions.
  1. chown
  2. link
  3. do_spawn


   1  /* os/2 compatibility functions -- follows Ruby's lisence */
   2  
   3  #include "ruby.h"
   4  #include <stdio.h>
   5  #include <stdlib.h>
   6  #include <fcntl.h>
   7  #include <process.h>
   8  #include <limits.h>
   9  #include <errno.h>
  10  
  11  #define INCL_DOS
  12  #include <os2.h>
  13  
  14  int
  15  chown(char *path, int owner, int group)
  16  {
  17          return 0;
  18  }
  19  
  20  int
  21  link(char *from, char *to)
  22  {
  23          return -1;
  24  }
  25  
  26  typedef char* CHARP;
  27  
  28  int
  29  do_spawn(cmd)
  30  char *cmd;
  31  {
  32      register char **a;
  33      register char *s;
  34      char **argv;
  35      char *shell, *sw, *cmd2;
  36      int status;
  37  
  38      if ((shell = getenv("RUBYSHELL")) != NULL && *shell != '\0') {
  39          s = shell;
  40          do
  41              *s = isupper(*s) ? tolower(*s) : *s;
  42          while (*++s);
  43          if (strstr(shell, "cmd") || strstr(shell, "4os2"))
  44              sw = "/c";
  45          else
  46              sw = "-c";
  47      } else if ((shell = getenv("SHELL")) != NULL && *shell != '\0') {
  48          s = shell;
  49          do
  50              *s = isupper(*s) ? tolower(*s) : *s;
  51          while (*++s);
  52          if (strstr(shell, "cmd") || strstr(shell, "4os2"))
  53              sw = "/c";
  54          else
  55              sw = "-c";
  56      } else if ((shell = getenv("COMSPEC")) != NULL && *shell != '\0') {
  57          s = shell;
  58          do
  59              *s = isupper(*s) ? tolower(*s) : *s;
  60          while (*++s);
  61          if (strstr(shell, "cmd") || strstr(shell, "4os2"))
  62              sw = "/c";
  63          else
  64              sw = "-c";
  65      }
  66      /* see if there are shell metacharacters in it */
  67      /*SUPPRESS 530*/
  68      /*    for (s = cmd; *s && isalpha(*s); s++) ;
  69      if (*s == '=')
  70      goto doshell; */
  71      for (s = cmd; *s; s++) {
  72          if (*sw == '-' && *s != ' ' && 
  73              !isalpha(*s) && index("$&*(){}[]'\";\\|?<>~`\n",*s)) {
  74              if (*s == '\n' && !s[1]) {
  75                  *s = '\0';
  76                  break;
  77              }
  78              goto doshell;
  79          } else if (*sw == '/' && *s != ' ' && 
  80              !isalpha(*s) && index("^()<>|&\n",*s)) {
  81              if (*s == '\n' && !s[1]) {
  82                  *s = '\0';
  83                  break;
  84              }
  85            doshell:
  86              status = spawnlp(P_WAIT,shell,shell,sw,cmd,(char*)NULL);
  87              return status;
  88          }
  89      }
  90      argv = ALLOC_N(CHARP,(strlen(cmd) / 2 + 2));
  91      cmd2 = ALLOC_N(char, (strlen(cmd) + 1));
  92      strcpy(cmd2, cmd);
  93      a = argv;
  94      for (s = cmd2; *s;) {
  95          while (*s && isspace(*s)) s++;
  96          if (*s)
  97              *(a++) = s;
  98          while (*s && !isspace(*s)) s++;
  99          if (*s)
 100              *s++ = '\0';
 101      }
 102      *a = NULL;
 103      if (argv[0]) {
 104          if ((status = spawnvp(P_WAIT, argv[0], argv)) == -1) {
 105              free(argv);
 106              free(cmd2);
 107              return -1;
 108          }
 109      }
 110      free(cmd2);
 111      free(argv);
 112      return status;
 113  }