Ruby  2.0.0p247(2013-06-27revision41674)
missing/os2.c
Go to the documentation of this file.
00001 /* os/2 compatibility functions -- follows Ruby's license */
00002 
00003 #include "ruby.h"
00004 #include <stdio.h>
00005 #include <stdlib.h>
00006 #include <fcntl.h>
00007 #include <process.h>
00008 #include <limits.h>
00009 #include <errno.h>
00010 
00011 #define INCL_DOS
00012 #include <os2.h>
00013 
00014 int
00015 chown(char *path, int owner, int group)
00016 {
00017         return 0;
00018 }
00019 
00020 #if 0
00021 int
00022 link(char *from, char *to)
00023 {
00024         return -1;
00025 }
00026 #endif
00027 
00028 #if defined(EMX_REPLACE_GETCWD) && (EMX_REPLACE_GETCWD)  \
00029  || defined(EMX_REPLACE_CHDIR)  && (EMX_REPLACE_CHDIR)
00030 #include <unistd.h>
00031 
00032 #if defined(EMX_REPLACE_GETCWD) && (EMX_REPLACE_GETCWD)
00033 /* to handle the drive letter and DBCS characters within a given path */
00034 char *
00035 getcwd(char *path, size_t len)
00036 {
00037     return _getcwd2(path, (int)len);
00038 }
00039 #endif
00040 
00041 #if defined(EMX_REPLACE_CHDIR) && (EMX_REPLACE_CHDIR)
00042 /* to handle the drive letter and DBCS characters within a given path */
00043 int
00044 chdir(__const__ char *path)
00045 {
00046     return _chdir2(path);
00047 }
00048 #endif
00049 #endif
00050 
00051 typedef char* CHARP;
00052 
00053 int
00054 do_spawn(cmd)
00055 char *cmd;
00056 {
00057     register char **a;
00058     register char *s;
00059     char **argv;
00060     char *shell, *sw, *cmd2;
00061     int status;
00062 
00063     if ((shell = getenv("RUBYSHELL")) != NULL && *shell != '\0') {
00064         s = shell;
00065         do
00066             *s = isupper(*s) ? tolower(*s) : *s;
00067         while (*++s);
00068         if (strstr(shell, "cmd") || strstr(shell, "4os2"))
00069             sw = "/c";
00070         else
00071             sw = "-c";
00072     } else if ((shell = getenv("SHELL")) != NULL && *shell != '\0') {
00073         s = shell;
00074         do
00075             *s = isupper(*s) ? tolower(*s) : *s;
00076         while (*++s);
00077         if (strstr(shell, "cmd") || strstr(shell, "4os2"))
00078             sw = "/c";
00079         else
00080             sw = "-c";
00081     } else if ((shell = getenv("COMSPEC")) != NULL && *shell != '\0') {
00082         s = shell;
00083         do
00084             *s = isupper(*s) ? tolower(*s) : *s;
00085         while (*++s);
00086         if (strstr(shell, "cmd") || strstr(shell, "4os2"))
00087             sw = "/c";
00088         else
00089             sw = "-c";
00090     }
00091     /* see if there are shell metacharacters in it */
00092     /*SUPPRESS 530*/
00093     /*    for (s = cmd; *s && isalpha(*s); s++) ;
00094     if (*s == '=')
00095     goto doshell; */
00096     for (s = cmd; *s; s++) {
00097         if (*sw == '-' && *s != ' ' &&
00098             !isalpha(*s) && index("$&*(){}[]'\";\\|?<>~`\n",*s)) {
00099             if (*s == '\n' && !s[1]) {
00100                 *s = '\0';
00101                 break;
00102             }
00103             goto doshell;
00104         } else if (*sw == '/' && *s != ' ' &&
00105             !isalpha(*s) && index("^()<>|&\n",*s)) {
00106             if (*s == '\n' && !s[1]) {
00107                 *s = '\0';
00108                 break;
00109             }
00110           doshell:
00111             status = spawnlp(P_WAIT,shell,shell,sw,cmd,(char*)NULL);
00112             return status;
00113         }
00114     }
00115     argv = ALLOC_N(CHARP,(strlen(cmd) / 2 + 2));
00116     cmd2 = ALLOC_N(char, (strlen(cmd) + 1));
00117     strcpy(cmd2, cmd);
00118     a = argv;
00119     for (s = cmd2; *s;) {
00120         while (*s && isspace(*s)) s++;
00121         if (*s)
00122             *(a++) = s;
00123         while (*s && !isspace(*s)) s++;
00124         if (*s)
00125             *s++ = '\0';
00126     }
00127     *a = NULL;
00128     if (argv[0]) {
00129         if ((status = spawnvp(P_WAIT, argv[0], argv)) == -1) {
00130             free(argv);
00131             free(cmd2);
00132             return -1;
00133         }
00134     }
00135     free(cmd2);
00136     free(argv);
00137     return status;
00138 }
00139