Exemplo n.º 1
0
/*
** Executes the instructions with the redirections
*/
int		create_pipes_and_exec(char **ins, int i, t_list **env, int *old)
{
  int		toyos[2];
  pid_t		id;
  int		(*redir)();
  int		bt_in;

  if (!init_toyos(ins, i, toyos, old))
    return (0);
  if (ins[i + 1])
    create_pipes_and_exec(ins, i + 1, env, toyos);
  bt_in = exec_built_in(ins[i], env);
  if (!bt_in && !(redir = get_redirec(ins[i])))
    {
      if ((id = fork()) == 0)
	{
	  if (ins[i + 1] && (redir = get_redirec(ins[i + 1])))
	    my_dup2(1, toyos, old, redir);
	  if (i > 0 && (redir = get_redirec(ins[i - 1])))
	    my_dup2(0, toyos, old, redir);
	  execve_call(ins, i,env);
	}
      else
	{
	  if (!ins[i + 1])
	    toyos[0] = 0;
	  child_control_func(id, toyos, old);
	}
    }
  return (bt_in == -1);
}
Exemplo n.º 2
0
int		my_dup_fd(t_fd *fd)
{
  if (fd == NULL)
    return (1);
  if (fd->fd_0 != -1)
    my_dup2(fd->fd_0, 0);
  if (fd->fd_1 != -1)
    my_dup2(fd->fd_1, 1);
  if (fd->fd_redir[0] != -1)
    my_dup2(fd->fd_redir[0], 0);
  if (fd->fd_pipe[0] != -1)
    my_dup2(fd->fd_pipe[0], 0);
  if (fd->fd_pipe[1] != -1)
    my_dup2(fd->fd_pipe[1], 1);
  return (0);
}
Exemplo n.º 3
0
int main(int argc, char* argv[]) {

    int fd;

    fd = creat("4_test_file",
               S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
               S_IROTH | S_IWOTH);                     /* rw-rw-rw- */

    printf("the new create file fd is %d\n", fd);

    printf("use my_dup get a new fd is %d\n", my_dup(fd));

    printf("use my_dup2 get a new fd(100) is %d\n", my_dup2(fd, 100));

    printf("use dup get a new fd is %d\n", dup(fd));

    printf("use dup2 get a new fd(100) is %d\n", dup2(fd, 100));

    if (close(fd) == -1)
        errExit("close");

    remove("4_test_file");

    exit(EXIT_SUCCESS);
}
Exemplo n.º 4
0
Arquivo: pipes.c Projeto: 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);
}
Exemplo n.º 5
0
Arquivo: pipes.c Projeto: LeNiglo/42sh
void		end_pipe(t_param exe, int pipe_pre[2], t_shell *sh)
{
  int		pid;

  pid = my_fork();
  if (pid == 0)
    {
      my_dup2(pipe_pre[0], 0);
      my_execve(exe, sh);
    }
  free(pipe_pre);
}
Exemplo n.º 6
0
int main(int argc,char *argv[])
{
	int fd;
	fd = my_dup2(1,1023);
	printf("fd = %d\n",fd);

	write(1,"stdout",6);
	printf("\n");
	write(fd,"dup2_fd",7);
	printf("\n");

	exit(0);
}
Exemplo n.º 7
0
Arquivo: pipes.c Projeto: 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);
}
Exemplo n.º 8
0
int main(int argc, char *argv[]) {
  int fd, new_fd, flags, new_flags;

  if((argc != 2) || (strcmp(argv[1], "--help") == 0)) {
    fprintf(stdout, "Usage: %s file_descriptor\n", argv[0]);
  } else {
    new_fd = atoi(argv[1]);
  }

  flags = O_CREAT | O_RDWR;
  fd = open("foo", flags, S_IRUSR | S_IWUSR);
  if(fd == -1) {
    fprintf(stderr, "error on open\n");
    exit(EXIT_FAILURE);
  }

  new_fd = my_dup2(fd, new_fd);
  if(new_fd == -1) {
    fprintf(stderr, "error on my_dup2\n");
    exit(EXIT_FAILURE);
  }

  new_flags = fcntl(new_fd, F_GETFL);
  if(new_flags == -1) {
    fprintf(stderr, "error on fcntl\n");
    exit(EXIT_FAILURE);
  }

  fprintf(stdout, "new fd: %d, old fd: %d, new flags: %d, old flags: %d\n ", new_fd, fd, new_flags, flags);
  if((new_flags & O_ACCMODE) == O_RDWR) {
    fprintf(stdout, "new fd also has same access flags\n");
  }

  if(close(fd) == -1) {
    fprintf(stderr, "error on close\n");
    exit(EXIT_FAILURE);
  }

  if(close(new_fd) == -1) {
    fprintf(stderr, "error on close\n");
    exit(EXIT_FAILURE);
  }

  exit(EXIT_SUCCESS);
}
Exemplo n.º 9
0
Arquivo: dup2.c Projeto: klion26/APUE
int test_my_dup2(int argc,char ** argv)
{
	int fd;
	if(argc < 3)
		{
			printf("usage: excise oldfd newfd\n");
			return 0;
		}
	fd = my_dup2(atoi(argv[1]),atoi(argv[2]));
	if(-1 == fd)
		{
			printf("dup2 fd failed\n");
			return -1;
		}
	printf("return fd by my_dup2 is %d\n",fd);

	return 0;
}
Exemplo n.º 10
0
int
main()
{
    int fd = open("./3.2_implement_dup2.c", O_RDONLY);
    int fd2 = open("./3.2_implement_dup2.c", O_RDWR);
    print_fdfl(fd);
    print_fdfl(fd2);

    fd2 = my_dup2(fd, fd2);
    print_fdfl(fd);
    print_fdfl(fd2);

    // If success, fd and fd2 will be change.
    fcntl(fd, F_SETFL, O_APPEND);
    print_fdfl(fd);
    print_fdfl(fd2);

    return 0;
}
Exemplo n.º 11
0
int main (int argc, char *argv[]) {
  if (argc != 2) {
    usageErr("%s testfile\n", argv[0]);
  }

  int fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, S_IWUSR | S_IRUSR);
  if (fd == -1) { errExit("open testfile"); }

  assert(my_dup(-1) == -1);
  assert(my_dup(42) == -1);

  int dup = my_dup(fd);
  assert(dup != -1);
  assert(dup != fd);
  assert(get_offset(fd) == 0);
  assert(get_offset(dup) == 0);
  char text[] = "abc";
  size_t text_len = strlen(text);
  if (write(dup, text, text_len) != (ssize_t) text_len) { errExit("writing 'abc'"); }
  assert(get_offset(fd) == (off_t) text_len);
  assert(get_offset(dup) == (off_t) text_len);

  assert(my_dup2(42, 42) == -1);
  assert(my_dup2(STDOUT_FILENO, STDOUT_FILENO) == STDOUT_FILENO);
  assert(my_dup2(fd, -1) == -1);
  assert(my_dup2(fd, OPEN_MAX) == -1);
  assert(my_dup2(42, STDOUT_FILENO) == -1);

  dup = my_dup2(fd, STDOUT_FILENO);
  assert(dup == STDOUT_FILENO);
  char greeting[] = "Hello World!";
  size_t combined_len = text_len + strlen(greeting);
  fprintf(stderr, "this should be the only output of this program\n");
  printf(greeting);
  fflush(stdout);
  assert(get_offset(dup) == combined_len);
  assert(get_offset(fd)  == combined_len);

  exit(EXIT_SUCCESS);
}
Exemplo n.º 12
0
void pipex_open(pipex_t *pipex,
                const char *name,
                const char *working_dir,
                const char *command){
    char string[StringSize];
    int argc;
    char * ptr;
    char * argv[256];
    int from_child[2], to_child[2];
    wordexp_t p;
    int i,ret;

    pipex->pid=-1;
    pipex->io->name=name;
    pipex->quit_pending=FALSE;
    pipex->command=command;
    
    if(command==NULL){
        pipex->io->in_fd = STDIN_FILENO;
        pipex->io->out_fd = STDOUT_FILENO;

            // attach standard error to standard output
        
        my_dup2(STDOUT_FILENO,STDERR_FILENO);
        io_init(pipex->io);
    }else{
    
        // parse the command line and create the argument list
#if 0    
        if (strlen(command) >= StringSize) my_fatal("pipex_open(): buffer overflow\n");
        strcpy(string,command);
        argc = 0;
        
        for (ptr = strtok(string," "); ptr != NULL; ptr = strtok(NULL," ")) {
            argv[argc++] = ptr;
        }

        argv[argc] = NULL;
#else
	//printf("command=[%s]\n",command);
	//Buffer overflow alert
	ret=wordexp(command, &p, 0);
	if(ret!=0){
	  my_fatal("pipex_open(): %s: Unable to parse command.\n",command);
	}
	argc = p.we_wordc;
	if(argc>=256-2){
	  my_fatal("pipex_open(): %s: Too many arguments.\n",command);
	}
	for(i=0;i<argc;i++){
	  argv[i] = p.we_wordv[i];
	}
	//	int i;
	//for(i=0;i<argc;i++){
	//  printf("[%s]",argv[i]);
	//}
	//printf("\n");
	argv[argc] = NULL;
#endif        
      // create the pipes
        
        if (pipe(from_child) == -1) {
            my_fatal("pipex_open(): pipe(): %s\n",strerror(errno));
        }
        
        if (pipe(to_child) == -1) {
            my_fatal("pipex_open(): pipe(): %s\n",strerror(errno));
        }
        
            // create the child process 
        
        pipex->pid = fork();
        
        if (pipex->pid == -1) {
            
            my_fatal("pipex_open(): fork(): %s\n",strerror(errno));
            
        } else if (pipex->pid == 0) {
            
                // child 
            
                // close unused pipe descriptors to avoid deadlocks
            
            my_close(from_child[0]);
            my_close(to_child[1]);
            
                // attach the pipe to standard input
            
            my_dup2(to_child[0],STDIN_FILENO);
            my_close(to_child[0]);
            
                // attach the pipe to standard output
            
            my_dup2(from_child[1],STDOUT_FILENO);
            my_close(from_child[1]);
            
                // attach standard error to standard output
                // commenting this out gives error messages on the console
            
            my_dup2(STDOUT_FILENO,STDERR_FILENO); 
            
            if(chdir(working_dir)){
                printf("%s pipex_open(): %s: %s\n",
		       PIPEX_MAGIC,
		       working_dir,
		       strerror(errno));
		goto wait_for_eof;
            }
            
            // launch the new executable file

            execvp(argv[0],&argv[0]);
            
                // execvp() only returns when an error has occured

	    printf("%s pipex_open(): execvp(): %s: %s\n",
		   PIPEX_MAGIC,
		   argv[0],
		   strerror(errno));
	wait_for_eof:
	    while(fgets(string,StringSize,stdin));
	    exit(EXIT_SUCCESS);
            
        } else { // pid > 0
            
            ASSERT(pipex->pid>0);
            
                // parent 
            
                // close unused pipe descriptors to avoid deadlocks
            
            my_close(from_child[1]);
            my_close(to_child[0]);
            
                // fill in the pipex struct
            
            pipex->io->in_fd = from_child[0];
            pipex->io->out_fd = to_child[1];
            pipex->state|=PIPEX_ACTIVE; // can we test if this really TRUE?
            
            io_init(pipex->io);
        } 
    }
}
Exemplo n.º 13
0
void engine_open(engine_t * engine) {

   const char * dir, * command;
   char string[StringSize];
   int argc;
   char * ptr;
   char * argv[256];
   int from_engine[2], to_engine[2];
   pid_t pid;

   ASSERT(engine!=NULL);

   // init

   dir = option_get_string("EngineDir");
   my_log("POLYGLOT Dir \"%s\"\n",dir);

   command = option_get_string("EngineCommand");
   my_log("POLYGLOT Command \"%s\"\n",command);

   // parse the command line and create the argument list

   if (strlen(command) >= StringSize) my_fatal("engine_open(): buffer overflow\n");
   strcpy(string,command);

   argc = 0;

   for (ptr = strtok(string," "); ptr != NULL; ptr = strtok(NULL," ")) {
      argv[argc++] = ptr;
   }

   argv[argc] = NULL;

   // create the pipes

   if (pipe(from_engine) == -1) {
      my_fatal("engine_open(): pipe(): %s\n",strerror(errno));
   }

   if (pipe(to_engine) == -1) {
      my_fatal("engine_open(): pipe(): %s\n",strerror(errno));
   }

   // create the child process

   pid = fork();

   if (pid == -1) {

      my_fatal("engine_open(): fork(): %s\n",strerror(errno));

   } else if (pid == 0) {

      // child = engine

      // close unused pipe descriptors to avoid deadlocks

      my_close(from_engine[0]);
      my_close(to_engine[1]);

      // attach the pipe to standard input

      my_dup2(to_engine[0],STDIN_FILENO);
      my_close(to_engine[0]);

      // attach the pipe to standard output

      my_dup2(from_engine[1],STDOUT_FILENO);
      my_close(from_engine[1]);

      // attach standard error to standard output

      my_dup2(STDOUT_FILENO,STDERR_FILENO);

      // set a low priority

      if (option_get_bool("UseNice"))
      {
          my_log("POLYGLOT Adjust Engine Piority");
          nice(+option_get_int("NiceValue"));
      }

      // change the current directory

      if (dir[0] != '\0' && chdir(dir) == -1) {
         my_fatal("engine_open(): chdir(): %s\n",strerror(errno));
      }

      // launch the new executable file

      execvp(argv[0],&argv[0]);

      // execvp() only returns when an error has occured

      my_fatal("engine_open(): execvp(): %s\n",strerror(errno));

   } else { // pid > 0

      ASSERT(pid>0);

      // parent = PolyGlot

      // close unused pipe descriptors to avoid deadlocks

      my_close(from_engine[1]);
      my_close(to_engine[0]);

      // fill in the engine struct

      engine->io->in_fd = from_engine[0];
      engine->io->out_fd = to_engine[1];
      engine->io->name = "Engine";

      io_init(engine->io);
   }
}