Example #1
0
File: bosh.c Project: Madx-com/BOSC
/* --- execute a shell command --- */
int executeshellcmd(Shellcmd *shellcmd)
{
  printshellcmd(shellcmd);
  
  Cmd *cmds;
  char **cmd0;

  cmds = shellcmd->the_cmds;
  cmd0 = cmds->cmd;

  if(strcmp(*cmd0,"exit") == 0)
  {
    return 1;
  }

  int i = executecmds(cmds, shellcmd->rd_stdin, shellcmd->rd_stdout, shellcmd->background);

  if(i == -1)
  {
	printf("Command not found\n");
  }

  return 0;
}
Example #2
0
File: bosh.c Project: Jelb/BOSC
/* --- execute a shell command --- */
int executeshellcmd (Shellcmd *shellcmd)
{
  printshellcmd(shellcmd);

  return 0;
}
Example #3
0
File: bosh.c Project: swungify/BOSC
/* --- execute a shell command --- */
int executeshellcmd (Shellcmd *shellcmd) {

  char *cmd = shellcmd->the_cmds->cmd[0];    
  Cmd *next = shellcmd->the_cmds->next;  
  
  if (next) {   //If there is more than one command

    char *nextName = next->cmd[0];
    if(!next->next) { //If there isn't a third command

      int pipefd[2];
      pipe(pipefd);

      int pid1 = fork();
      if(pid1 == 0) {

        close(pipefd[1]);
        int ret = dup2(pipefd[0], 0);
        if(ret < 0) perror("dup2");
        if(shellcmd->rd_stdout) {

            close(1);
            int fid = open(shellcmd->rd_stdout, O_WRONLY | O_CREAT);
            int ret = dup(fid);
        }

        if(shellcmd->rd_stdin) {
          close(0);
          int fid = open(shellcmd->rd_stdin, O_RDONLY);
          int ret = dup(fid);
        }
        int executionResult = execvp(cmd, shellcmd->the_cmds->cmd); 
        if(executionResult = -1)
        {
          printf("Command '%s' not found\n", cmd);
          return 0;
        }       
      }
      
      int pid2 = fork();
      if(pid2 == 0) {

        int ret = dup2(pipefd[1], 1);
        if(ret < 0) perror("dup2");
        int executionResult = execvp(nextName, next->cmd);
        if(executionResult = -1)
        {
          printf("Command '%s' not found\n", nextName);
          return 0;
        }

      }

      close(pipefd[0]);
      close(pipefd[1]);

      if (shellcmd->background){
          //printf("I'm not gonna wait\n");

      }
      else{
          //printf("I will wait!\n");
          int status;
          waitpid(pid1, &status, 0);
          waitpid(pid2, &status, 0);
      }
      
    }
    else {    //We don't handle more than two commands at the same time
      
      printf("Too many pipes\nTry again\n"); 
      return 0;
    }
  }
  else {
  
    int pid = fork();
    if(pid == 0) {

      if(shellcmd->rd_stdout) {

        close(1);
        int fid = open(shellcmd->rd_stdout, O_WRONLY | O_CREAT);
        int ret = dup(fid);
      }
      
      if(shellcmd->rd_stdin) {

        close(0);
        int fid = open(shellcmd->rd_stdin, O_RDONLY);
        int ret = dup(fid);
      }
     // printf("Execution without piping\n");
      int executionResult = execvp(cmd, shellcmd->the_cmds->cmd);
      if(executionResult = -1)
      {
        printf("Command '%s' not found\n", cmd);
        return 0;
      }    

    }
  if (shellcmd->background){  //True if it's a background process
          //printf("I'm not gonna wait\n");
      }
      else{
          //printf("I will wait!\n");
          int status;
          waitpid(pid, &status, 0);
      }
  }   
  
  printshellcmd(shellcmd);
  return 0;
}