Пример #1
0
int redireccion(char *cmd){

  if(*cmd =='\0')
    return 1;

  char *s;

  s = strstr(cmd, "&&");  

  if(s == NULL){
    if (*cmd =='\0')
      return 1;
    else {
      return ejecutar(cmd); 
    }
  }
  else {
    char *buffer=malloc(sizeof(char) * 1024); 
    strncpy ( buffer, cmd, s - cmd);
    if(ejecutar(buffer) == 1){
      char *cmd2=s+2;
      return redireccion (cmd2);
    }
    else{
      printf("Error process failed \n");
      return 1;
    }
  }
}
Пример #2
0
int main(int argc, char *argv[]){
  char *cmd;
  char **varg;
  int estado=1;
  while(estado){ 
    printf("Shellsita $ "); 
    cmd=leer_cmd();
    estado=redireccion(cmd);
    free(cmd);
  }
  return 0;

}
Пример #3
0
main (){
  pthread_t history;
  char buffer[BUFFER_SIZE];
  strcpy(prompt,"#");
  prepararSenales();

  while (1){
    argumentos[0] = "\n"; //inicializar el arreglo para que no haya errores de punteros
    argumentos[1] = "\n";
    printf("[%s-%s] %s ", getlogin(), miPwd(), prompt);
    fgets(buffer, BUFFER_SIZE, stdin);	
    separa(buffer,argumentos);  

    pthread_create(&history, NULL, (void *)setHistorial, buffer);
    if (!strcmp(argumentos[0],"salir")){
      printf("Saliendo de shell...\n");	
      exit(0);
    }
    else if (!strcmp(argumentos[0], "historial")){    
      mostrarHistorial();
    }      
    else if(!strcmp(argumentos[0],"cd")){
        chdir(argumentos[1]);
	if(argumentos[1]==0) {
	char *str = getenv( "HOME" );
	if ( chdir ( str ) < 0 ) {
		fprintf( stderr , "cd: ERROR\n");
	   }
	}
    }    
    else if (!strcmp(argumentos[0], "mipid")){
      printf("Mi id de Proceso es %d\n",(int)getpid()); 
    }
    else {
      if(fork() == 0){
	redireccion();	
	if(execvp(argumentos[0], argumentos)<0 )
	  {
	    if (argumentos[0] == "\n"){}
	    else{
	      printf ("ese comando no existe\n");
	    }
	    exit(0);//matar al hijo
	  }	
      }
      /* el padre espera al hijo */
      pthread_join(history, NULL);
      wait(NULL); 	
    }
  }
}