Esempio n. 1
0
int	exec_pipe(int i, char **command)
{
  int	pid;
  int	fd[2];

  if (command[i + 1])
    {
      if (pipe(fd) == -1 || (pid = fork()) == -1)
	return (0);
      if (pid != 0)
	{
	  close(fd[1]);
	  if (dup2(fd[0], 0) == -1)
	    return (0);
	  waitpid(pid, 0, 0);
	  if (my_pipe(i + 1, command) == 0)
	    return (0);
	  close(fd[0]);
	}
      else if (father(fd, command, i) == 0)
	return (0);
    }
  if (execlp(command[i], command[i], NULL) == -1)
    return (0);
  return (1);
}
Esempio n. 2
0
int main(int argc, char** argv) {
    if(argc != 3 ) {
        printf("Usage:./a.out cmd1 cmd2\n");
        return -1;
    }
    my_pipe(argv[1],argv[2]);
    return 0;
}
Esempio n. 3
0
File: pipes.c Progetto: LeNiglo/42sh
int		*first_pipe(t_param exe, t_shell *sh)
{
  int		*pipe_next;
  int		pid;

  pipe_next = malloc(2 * sizeof(int));
  my_pipe(pipe_next);
  pid = my_fork();
  if (pid == 0)
    {
      close(pipe_next[0]);
      my_dup2(pipe_next[1], 1);
      my_execve(exe, sh);
    }
  close(pipe_next[1]);
  return (pipe_next);
}
Esempio n. 4
0
File: pipes.c Progetto: LeNiglo/42sh
int		*between_pipe(t_param exe, int pipe_pre[2], t_shell *sh)
{
  int		*pipe_next;
  int		pid;

  pipe_next = malloc(2 * sizeof(int));
  my_pipe(pipe_next);
  pid = my_fork();
  if (pid == 0)
    {
      close(pipe_next[0]);
      my_dup2(pipe_next[1], 1);
      my_dup2(pipe_pre[0], 0);
      my_execve(exe, sh);
    }
  close(pipe_next[1]);
  free(pipe_pre);
  return (pipe_next);
}
Esempio n. 5
0
File: check.c Progetto: fave-r/42Sh
int		check_fn(t_tree *tree, int in, int out, t_env_var *env)
{
  if (!tree)
    return (-1);
  else if (strcmp(tree->data, ">") == 0)
    return (redir_right(tree, in, out, env));
  else if (strcmp(tree->data, ">>") == 0)
    return (doble_right(tree, in, out, env));
  else if (strcmp(tree->data, "<") == 0)
    return (redir_left(tree, in, out, env));
  else if (strcmp(tree->data, "&&") == 0)
    return (my_and(tree, in, out, env));
  else if (strcmp(tree->data, "||") == 0)
    return (my_or(tree, in, out, env));
  else if (strcmp(tree->data, ";") == 0)
    return (my_semi_col(tree, in, out, env));
  else if (strcmp(tree->data, "<<") == 0)
    return (doble_left(tree, in, out, env));
  else if (strcmp(tree->data, "|") == 0)
    return (my_pipe(tree, in, out, env));
  else
    return (my_exec(tree->data, in, out, env));
}
Esempio n. 6
0
int	simple(char **tabeulo)
{
  int	status;

  if (tabeulo[1][0] == '>')
    {
      if (redir_droite(tabeulo) == 0)
	return (0);
    }
  else if (tabeulo[1][0] == '<')
    {
      if (redir_gauche(tabeulo) == 0)
	return (0);
    }
  else if (tabeulo[1][0] == '|')
    {
      if (fork())
	wait(&status);
      else if (my_pipe(0, tabeulo) == 0)
	return (0);
    }
  return (1);
}
Esempio n. 7
0
FILE *
pt_popen(const char *cmd, const char *mode)
{ FILE *fptr = NULL;
  PROCESS_INFORMATION piProcInfo;
  STARTUPINFOW siStartInfo;
  int success, redirect_error = 0;
  wchar_t *wcmd = NULL;
  wchar_t *err2out;
  pipe_context *pc;

  size_t utf8len = utf8_strlen(cmd, strlen(cmd));
  if ( !(wcmd = malloc((utf8len+1)*sizeof(wchar_t))) )
  { return NULL;
  }
  utf8towcs(wcmd, cmd);

  if ( !(pc=allocPipeContext()) )
    goto finito;
  if ( !mode || !*mode )
    goto finito;
  pc->mode = *mode;
  if ( pc->mode != 'r' && pc->mode != 'w' )
    goto finito;

  /*
   * Shall we redirect stderr to stdout ? */
  if ( (err2out=wcsstr(wcmd, L"2>&1")) != NULL)
  { /* this option doesn't apply to win32 shells, so we clear it out! */
     wcsncpy(err2out, L"    ", 4);
     redirect_error = 1;
  }

  /*
   * Create the Pipes... */
  if (my_pipe(pc->in)  == -1 ||
      my_pipe(pc->out) == -1)
    goto finito;
  if ( !redirect_error )
  { if ( my_pipe(pc->err) == -1)
      goto finito;
  }

  /*
   * Now create the child process */
  ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
  siStartInfo.cb           = sizeof(STARTUPINFO);
  siStartInfo.hStdInput    = pc->in[0];
  siStartInfo.hStdOutput   = pc->out[1];
  if ( redirect_error )
    siStartInfo.hStdError  = pc->out[1];
  else
    siStartInfo.hStdError  = pc->err[1];
  siStartInfo.dwFlags      = STARTF_USESTDHANDLES;

  success = CreateProcessW(NULL,
			   wcmd,	// command line
			   NULL,	// process security attributes
			   NULL,	// primary thread security attributes
			   TRUE,	// handles are inherited
			   CREATE_NO_WINDOW,  // creation flags: without window (?)
			   NULL,	// use parent's environment
			   NULL,	// use parent's current directory
			   &siStartInfo, // STARTUPINFO pointer
			   &piProcInfo); // receives PROCESS_INFORMATION

  if ( !success )
    goto finito;

  CloseHandle(piProcInfo.hThread);
  CloseHandle(piProcInfo.hProcess);

  /*
   * These handles listen to the Child process */
  CloseHandle(pc->in[0]);  pc->in[0]  = INVALID_HANDLE_VALUE;
  CloseHandle(pc->out[1]); pc->out[1] = INVALID_HANDLE_VALUE;
  if ( pc->err[1] != INVALID_HANDLE_VALUE )
  { CloseHandle(pc->err[1]);
    pc->err[1] = INVALID_HANDLE_VALUE;
  }

  if ( pc->mode == 'r' )
    fptr = _fdopen(_open_osfhandle((intptr_t)pc->out[0],_O_BINARY),"r");
  else
    fptr = _fdopen(_open_osfhandle((intptr_t)pc->in[1],_O_BINARY),"w");

finito:
  if ( fptr )
  { pc->fd = fptr;
    linkPipeContext(pc);
  } else
  { if ( pc )
      discardPipeContext(pc);
  }
  if ( wcmd )
    free(wcmd);

  return fptr;
}
Esempio n. 8
0
/*----------------------------------------------------------------------------
  Replacement for 'popen()' under WIN32.
  NOTE: if cmd contains '2>&1', we connect the standard error file handle
    to the standard output file handle.
----------------------------------------------------------------------------*/
FILE * pt_popen(const char *cmd, const char *mode)
{
  FILE *fptr = (FILE *)0;
  PROCESS_INFORMATION piProcInfo;
  STARTUPINFO siStartInfo;
  int success, umlenkung;

  my_pipein[0]   = INVALID_HANDLE_VALUE;
  my_pipein[1]   = INVALID_HANDLE_VALUE;
  my_pipeout[0]  = INVALID_HANDLE_VALUE;
  my_pipeout[1]  = INVALID_HANDLE_VALUE;
  my_pipeerr[0]  = INVALID_HANDLE_VALUE;
  my_pipeerr[1]  = INVALID_HANDLE_VALUE;

  if (!mode || !*mode)
    goto finito;

  my_popenmode = *mode;
  if (my_popenmode != 'r' && my_popenmode != 'w')
    goto finito;

  /*
   * Shall we redirect stderr to stdout ? */
  umlenkung = strstr("2>&1",(char *)cmd) != 0;

  /*
   * Create the Pipes... */
  if (my_pipe(my_pipein)  == -1 ||
      my_pipe(my_pipeout) == -1)
    goto finito;
  if (!umlenkung && my_pipe(my_pipeerr) == -1)
    goto finito;

  /*
   * Now create the child process */
  ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
  siStartInfo.cb           = sizeof(STARTUPINFO);
  siStartInfo.hStdInput    = my_pipein[0];
  siStartInfo.hStdOutput   = my_pipeout[1];
  if (umlenkung)
    siStartInfo.hStdError  = my_pipeout[1];
  else
    siStartInfo.hStdError  = my_pipeerr[1];
  siStartInfo.dwFlags    = STARTF_USESTDHANDLES;

  success = CreateProcess(NULL,
     (LPTSTR)cmd,       // command line
     NULL,              // process security attributes
     NULL,              // primary thread security attributes
     TRUE,              // handles are inherited
     DETACHED_PROCESS,  // creation flags: without window (?)
     NULL,              // use parent's environment
     NULL,              // use parent's current directory
     &siStartInfo,      // STARTUPINFO pointer
     &piProcInfo);      // receives PROCESS_INFORMATION

  if (!success)
    goto finito;

  /*
   * These handles listen to the child process */
  CloseHandle(my_pipein[0]);  my_pipein[0]  = INVALID_HANDLE_VALUE;
  CloseHandle(my_pipeout[1]); my_pipeout[1] = INVALID_HANDLE_VALUE;
  CloseHandle(my_pipeerr[1]); my_pipeerr[1] = INVALID_HANDLE_VALUE;

  if (my_popenmode == 'r')
    fptr = _fdopen(_open_osfhandle((long)my_pipeout[0],_O_BINARY),"r");
  else
    fptr = _fdopen(_open_osfhandle((long)my_pipein[1],_O_BINARY),"w");

finito:
  if (!fptr)
  {
    if (my_pipein[0]  != INVALID_HANDLE_VALUE)
      CloseHandle(my_pipein[0]);
    if (my_pipein[1]  != INVALID_HANDLE_VALUE)
      CloseHandle(my_pipein[1]);
    if (my_pipeout[0] != INVALID_HANDLE_VALUE)
      CloseHandle(my_pipeout[0]);
    if (my_pipeout[1] != INVALID_HANDLE_VALUE)
      CloseHandle(my_pipeout[1]);
    if (my_pipeerr[0] != INVALID_HANDLE_VALUE)
      CloseHandle(my_pipeerr[0]);
    if (my_pipeerr[1] != INVALID_HANDLE_VALUE)
      CloseHandle(my_pipeerr[1]);
  }
  return fptr;
}
void dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
    int     n = 0;
    int     maxfdp1 = 0;
    const   int     on = 1;
    char    sendline[MAXLINE] = {0};
    char    recvline[MAXLINE] = {0};
    fd_set  rset;
    socklen_t   len;
    struct  sockaddr    *preply_addr = NULL;

    preply_addr = my_malloc(servlen);

    my_setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));

    my_pipe(pipefd);
//    printf("pipefd[0] = %d\n", pipefd[0]); //+

    maxfdp1 = max(sockfd, pipefd[0]) + 1;

    FD_ZERO(&rset);

    my_signal(SIGALRM, recvfrom_alarm);

    while (my_fgets(sendline, MAXLINE, fp) != NULL )
    {
        my_sendto(sockfd, sendline, strlen(sendline),
                  0, pservaddr, servlen);

        alarm(3);

        while (1)
        {
            FD_SET(sockfd, &rset);
            FD_SET(pipefd[0], &rset);

            if ( (n = select(maxfdp1, &rset, NULL, NULL, NULL)) < 0 )
            {
                if (errno == EINTR)
                {
                    DEBUG;
                    printf("select errno = EINTR\n");    //+
                    continue;
                }
                else
                {
                    err_sys("select error");
                }
            }
//                printf("kankan!\n");
            if ( FD_ISSET(sockfd, &rset) )
            {
                len = servlen;
                n = my_recvfrom(sockfd, recvline, MAXLINE,
                                0, preply_addr, &len);
                recvline[n] = 0;
                printf("from %s: %s",
                       my_sock_ntop(preply_addr, len),
                       recvline);
            }

            if ( FD_ISSET(pipefd[0], &rset) )
            {
//                   printf("read 上面\n");
                my_read(pipefd[0], &n, 1);   //timer expired
//                   printf("read 下面!\n");
                break;
            }
        }
    }

    free(preply_addr);

}
Esempio n. 10
0
/* Fonction: executer_commande
 * Entrees: aucune
 *
 * Sortie: aucune
 *
 * Execute une commande
 */
void executer_commande() {
	
	pid_t pid;
	
	/* on commence par regarder s'il y a des pipes */
	int nb_pipes = number_pipes();
	if (nb_pipes > 0) {
		my_pipe(nb_pipes);
	}
	
	/* on exécute les commandes internes (pas de fork) */
	/* pas de pipes, donc la position de la commande est 0 */
	else if (internal_command(0) == 0)
	{	
		pid = fork(); /* le fork permet de lancer une commande en continuant d'executer le shell */
		if (pid == 0) /* fils */
		{
			
			/* on commence par vérifier si on veut faire une redirection */
			int i = 0; /* recherche du nombre d'éléments */
			while (commande[i] != NULL)
				i++; 
		
			if (commande[i-1][0] == '>') {
				/* ouverture d'un fichier de destination */
				int file = open(&commande[i-1][1], O_WRONLY | O_CREAT | O_TRUNC, 0666);
						/* O_WRONLY : on va seulement écrire dans le fichier
						 * O_CREAT : créé le fichier s'il n'existe pas
						 * O_TRUNC : le fichier est tronqué à une longueur nulle s'il existe
						 * 0666 : tout le monde peut lire et écrire dans ce fichier */
				
				if (file == -1) {
					printf("Erreur, impossible de créer le fichier");
					exit(1);
				}
			
				dup2(file, 1); /* remplace la sortie standard par un fichier de sortie */
				close(file); /* fermeture du fichier de destination */
				
				/* il faut supprimer le dernier argument pour effectuer la commande externe */
				commande[i-1] = NULL;
			}
			
			/* on exécute ensuite les commandes externes */
			const char* path = get_path(commande[0]); /* on récupère le chemin */
			if (strcmp(path, "echec") != 0) { /* si on réussit à récupérer le path */
				stockCommande();
				execv(path, commande);
			}
			else
				my_exit(commande);
				
		}
		else if(pid<0)
			printf("Erreur, fork a échoué.\n");
		else /* père */
		{
			int status;
			waitpid(pid, &status, 0); /* attente bloquante */
		}
	}
	
}