home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / perl / os2.c < prev    next >
C/C++ Source or Header  |  1991-06-16  |  7KB  |  299 lines

  1. /* $RCSfile: os2.c,v $$Revision: 4.0.1.1 $$Date: 91/06/07 11:23:06 $
  2.  *
  3.  *    (C) Copyright 1989, 1990 Diomidis Spinellis.
  4.  *
  5.  *    You may distribute under the terms of either the GNU General Public
  6.  *    License or the Artistic License, as specified in the README file.
  7.  *
  8.  * $Log:    os2.c,v $
  9.  * Revision 4.0.1.1  91/06/07  11:23:06  lwall
  10.  * patch4: new copyright notice
  11.  *
  12.  * Revision 4.0  91/03/20  01:36:21  lwall
  13.  * 4.0 baseline.
  14.  *
  15.  * Revision 3.0.1.2  90/11/10  01:42:38  lwall
  16.  * patch38: more msdos/os2 upgrades
  17.  *
  18.  * Revision 3.0.1.1  90/10/15  17:49:55  lwall
  19.  * patch29: Initial revision
  20.  *
  21.  * Revision 3.0.1.1  90/03/27  16:10:41  lwall
  22.  * patch16: MSDOS support
  23.  *
  24.  * Revision 1.1  90/03/18  20:32:01  dds
  25.  * Initial revision
  26.  *
  27.  */
  28.  
  29. #define INCL_DOS
  30. #define INCL_NOPM
  31. #include <os2.h>
  32.  
  33. /*
  34.  * Various Unix compatibility functions for OS/2
  35.  */
  36.  
  37. #include <stdio.h>
  38. #include <errno.h>
  39. #include <process.h>
  40.  
  41. #include "EXTERN.h"
  42. #include "perl.h"
  43.  
  44.  
  45. /* dummies */
  46.  
  47. int ioctl(int handle, unsigned int function, char *data)
  48. { return -1; }
  49.  
  50. int userinit()
  51. { return -1; }
  52.  
  53. int syscall()
  54. { return -1; }
  55.  
  56.  
  57. /* extended chdir() */
  58.  
  59. int chdir(char *path)
  60. {
  61.   if ( path[0] != 0 && path[1] == ':' )
  62.     if ( DosSelectDisk(toupper(path[0]) - '@') )
  63.       return -1;
  64.  
  65.   return DosChDir(path, 0L);
  66. }
  67.  
  68.  
  69. /* priorities */
  70.  
  71. int setpriority(int class, int pid, int val)
  72. {
  73.   int flag = 0;
  74.  
  75.   if ( pid < 0 )
  76.   {
  77.     flag++;
  78.     pid = -pid;
  79.   }
  80.  
  81.   return DosSetPrty(flag ? PRTYS_PROCESSTREE : PRTYS_PROCESS, class, val, pid);
  82. }
  83.  
  84. int getpriority(int which /* ignored */, int pid)
  85. {
  86.   USHORT val;
  87.  
  88.   if ( DosGetPrty(PRTYS_PROCESS, &val, pid) )
  89.     return -1;
  90.   else
  91.     return val;
  92. }
  93.  
  94.  
  95. /* get parent process id */
  96.  
  97. int getppid(void)
  98. {
  99.   PIDINFO pi;
  100.  
  101.   DosGetPID(&pi);
  102.   return pi.pidParent;
  103. }
  104.  
  105.  
  106. /* wait for specific pid */
  107.  
  108. int wait4pid(int pid, int *status, int flags)
  109. {
  110.   RESULTCODES res;
  111.   int endpid, rc;
  112.  
  113.   if ( DosCwait(DCWA_PROCESS, flags ? DCWW_NOWAIT : DCWW_WAIT,
  114.                 &res, &endpid, pid) )
  115.     return -1;
  116.  
  117.   *status = res.codeResult;
  118.   return endpid;
  119. }
  120.  
  121.  
  122. /* kill */
  123.  
  124. int kill(int pid, int sig)
  125. {
  126.   int flag = 0;
  127.  
  128.   if ( pid < 0 )
  129.   {
  130.     flag++;
  131.     pid = -pid;
  132.   }
  133.  
  134.   switch ( sig & 3 )
  135.   {
  136.  
  137.   case 0:
  138.     DosKillProcess(flag ? DKP_PROCESSTREE : DKP_PROCESS, pid);
  139.     break;
  140.  
  141.   case 1: /* FLAG A */
  142.     DosFlagProcess(pid, flag ? FLGP_SUBTREE : FLGP_PID, PFLG_A, 0);
  143.     break;
  144.  
  145.   case 2: /* FLAG B */
  146.     DosFlagProcess(pid, flag ? FLGP_SUBTREE : FLGP_PID, PFLG_B, 0);
  147.     break;
  148.  
  149.   case 3: /* FLAG C */
  150.     DosFlagProcess(pid, flag ? FLGP_SUBTREE : FLGP_PID, PFLG_C, 0);
  151.     break;
  152.  
  153.   }
  154. }
  155.  
  156.  
  157. /* Sleep function. */
  158. void
  159. sleep(unsigned len)
  160. {
  161.    DosSleep(len * 1000L);
  162. }
  163.  
  164. /* Just pretend that everyone is a superuser */
  165.  
  166. int setuid()
  167. { return 0; }
  168.  
  169. int setgid()
  170. { return 0; }
  171.  
  172. int getuid(void)
  173. { return 0; }
  174.  
  175. int geteuid(void)
  176. { return 0; }
  177.  
  178. int getgid(void)
  179. { return 0; }
  180.  
  181. int getegid(void)
  182. { return 0; }
  183.  
  184. /*
  185.  * The following code is based on the do_exec and do_aexec functions
  186.  * in file doio.c
  187.  */
  188. int
  189. do_aspawn(really,arglast)
  190. STR *really;
  191. int *arglast;
  192. {
  193.     register STR **st = stack->ary_array;
  194.     register int sp = arglast[1];
  195.     register int items = arglast[2] - sp;
  196.     register char **a;
  197.     char **argv;
  198.     char *tmps;
  199.     int status;
  200.  
  201.     if (items) {
  202.     New(1101,argv, items+1, char*);
  203.     a = argv;
  204.     for (st += ++sp; items > 0; items--,st++) {
  205.         if (*st)
  206.         *a++ = str_get(*st);
  207.         else
  208.         *a++ = "";
  209.     }
  210.     *a = Nullch;
  211.     if (really && *(tmps = str_get(really)))
  212.         status = spawnvp(P_WAIT,tmps,argv);
  213.     else
  214.         status = spawnvp(P_WAIT,argv[0],argv);
  215.     Safefree(argv);
  216.     }
  217.     return status;
  218. }
  219.  
  220. char *getenv(char *name);
  221.  
  222. int
  223. do_spawn(cmd)
  224. char *cmd;
  225. {
  226.     register char **a;
  227.     register char *s;
  228.     char **argv;
  229.     char flags[10];
  230.     int status;
  231.     char *shell, *cmd2;
  232.  
  233.     /* save an extra exec if possible */
  234.     if ((shell = getenv("COMSPEC")) == 0)
  235.     shell = "C:\\OS2\\CMD.EXE";
  236.  
  237.     /* see if there are shell metacharacters in it */
  238.     if (strchr(cmd, '>') || strchr(cmd, '<') || strchr(cmd, '|')
  239.         || strchr(cmd, '&') || strchr(cmd, '^'))
  240.       doshell:
  241.         return spawnl(P_WAIT,shell,shell,"/C",cmd,(char*)0);
  242.  
  243.     New(1102,argv, strlen(cmd) / 2 + 2, char*);
  244.  
  245.     New(1103,cmd2, strlen(cmd) + 1, char);
  246.     strcpy(cmd2, cmd);
  247.     a = argv;
  248.     for (s = cmd2; *s;) {
  249.     while (*s && isspace(*s)) s++;
  250.     if (*s)
  251.         *(a++) = s;
  252.     while (*s && !isspace(*s)) s++;
  253.     if (*s)
  254.         *s++ = '\0';
  255.     }
  256.     *a = Nullch;
  257.     if (argv[0])
  258.     if ((status = spawnvp(P_WAIT,argv[0],argv)) == -1) {
  259.         Safefree(argv);
  260.         Safefree(cmd2);
  261.         goto doshell;
  262.     }
  263.     Safefree(cmd2);
  264.     Safefree(argv);
  265.     return status;
  266. }
  267.  
  268. usage(char *myname)
  269. {
  270. #ifdef MSDOS
  271.   printf("\nUsage: %s [-acdnpPsSvw] [-0[octal]] [-Dnumber] [-i[extension]] [-Idirectory]"
  272. #else
  273.   printf("\nUsage: %s [-acdnpPsSuUvw] [-Dnumber] [-i[extension]] [-Idirectory]"
  274. #endif
  275.          "\n            [-e \"command\"] [-x[directory]] [filename] [arguments]\n", myname);
  276.  
  277.   printf("\n  -a  autosplit mode with -n or -p"
  278.          "\n  -c  syntaxcheck only"
  279.          "\n  -d  run scripts under debugger"
  280.          "\n  -n  assume 'while (<>) { ...script... }' loop arround your script"
  281.          "\n  -p  assume loop like -n but print line also like sed"
  282.          "\n  -P  run script through C preprocessor befor compilation"
  283.          "\n  -s  enable some switch parsing for switches after script name"
  284.          "\n  -S  look for the script using PATH environment variable");
  285. #ifndef MSDOS
  286.   printf("\n  -u  dump core after compiling the script"
  287.          "\n  -U  allow unsafe operations");
  288. #endif
  289.   printf("\n  -v  print version number and patchlevel of perl"
  290.          "\n  -w  turn warnings on for compilation of your script\n"
  291.          "\n  -0[octal]       specify record separator (0, if no argument)"
  292.          "\n  -Dnumber        set debugging flags (argument is a bit mask)"
  293.          "\n  -i[extension]   edit <> files in place (make backup if extension supplied)"
  294.          "\n  -Idirectory     specify include directory in conjunction with -P"
  295.          "\n  -e command      one line of script, multiple -e options are allowed"
  296.          "\n                  [filename] can be ommitted, when -e is used"
  297.          "\n  -x[directory]   strip off text before #!perl line and perhaps cd to directory\n");
  298. }
  299.