home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / mc454src.zip / mc-4.5.4.src / os2emx / src / mysystem.c < prev    next >
C/C++ Source or Header  |  1999-01-04  |  3KB  |  108 lines

  1. #include <config.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <process.h>
  5. #include "src/fs.h"
  6. #include "src/util.h"
  7. #include "src/dialog.h"
  8.  
  9. static int 
  10. os_startp (const char *shell, const char *command, const char *parm) 
  11. {  
  12.    if (parm) { 
  13.          char *argv[256],*par=strdup(parm),*p,i=0;
  14.          argv[i++]=strdup(shell); 
  15.          argv[i++]=strdup("/c"); 
  16.          argv[i++]=strdup(command); 
  17.          p=strtok(par," ");
  18.          while(p) {
  19.              argv[i++]=strdup(p);
  20.              p=strtok(0," ");
  21.          }
  22.          argv[i]=0; 
  23.          spawnvp (P_WAIT,shell,argv);
  24.          i=0;
  25.          while(argv[i])free(argv[i++]);
  26.          free(par);
  27.     } else {
  28.          spawnlp (P_WAIT, 
  29.               (char *) shell, 
  30.               (char *) shell, 
  31.               "/c", 
  32.               (char *) command, 
  33.               (char *) 0);
  34.     }
  35.     return 0;
  36. }
  37.  
  38. static int 
  39. ux_startp (int flags, const char *shell, const char *command) 
  40. {  
  41.   if (flags & EXECUTE_AS_SHELL)
  42.         spawnl  (P_WAIT, shell, shell, "-c", command, (char *) 0);
  43.   else
  44.         spawnlp (P_WAIT, shell, shell, command, (char *) 0);
  45.   return 0;
  46. }
  47.  
  48. int my_system (int as_shell_command, const char *Shell, const char *command)
  49. {
  50.    char *sh;            /* This is the shell -- always! */
  51.    char *cmd;           /* This is the command (only the command) */
  52.    char *parm;          /* This is the parameter (can be more than one) */
  53.    register int length, i;
  54.    char temp[4096];     /* That's enough! */
  55.    sh = getenv ("COMSPEC"); 
  56.    if (strcmp(sh, Shell)) {
  57.       /* unix shell */
  58.       cmd  = (char *) Shell;
  59.       parm = (char *) command;
  60.       return ux_startp(as_shell_command,Shell,command);
  61.    } else {
  62.       /* look into the command and take out the program */
  63.       if (command) {
  64.          strcpy(temp, command);
  65.          length = strlen(command);
  66.          for (i=length-1; i>=0; i--) {
  67.             if (command[i] == ' ') {
  68.                temp[i] = (char) 0;
  69.                length--;
  70.             } else 
  71.                 break;
  72.          }
  73.          if (i==-1) {
  74.             /* only blanks */
  75.             return -1;
  76.          }
  77.          if (parm = strchr(temp, (char) ' ')) {
  78.             *parm = (char) 0;
  79.             parm++;
  80.          }
  81.          cmd  = (char *) temp;
  82.          length=strlen(cmd);
  83.          for(i=0;i<length;i++)if(cmd[i]=='/')cmd[i]='\\';
  84.       } else {
  85.          /* command is NULL */
  86.          cmd = parm = NULL;
  87.       }
  88.    }
  89.    return os_startp(sh, cmd, parm);
  90. }
  91.  
  92.  
  93. static char tmp_buf[256];
  94. static int  tmp_cnt;
  95.  
  96. char *tmpnam_ext(char *sfx)
  97. /*-------------------------*/
  98. { char    *p=getenv("TMP"); 
  99.   unsigned t=time(0)<<8|(0xFF&tmp_cnt++);  
  100.   if(p){ strcpy(tmp_buf,p); 
  101.          p=tmp_buf;
  102.          while(*p) { if(*p=='/')*p='\\'; p++; } 
  103.          if(*(p-1)!='\\'){ *p++='\\'; *p=0; }
  104.        }
  105.   else p=tmp_buf;
  106.   sprintf(p,"%08x%s",t,sfx);
  107.   return tmp_buf;
  108. }