Jump to content

Recommended Posts

Posted

Boas precisava de ajuda com este codigo pois não estou a perceber pq que não executa os comandos eo execvp retorna sempre -1.

Alguma sugestão que possa ajudar ou mesmo rectificar o codigo.

Obrigado.

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>



void executar(char **args)
{
int status;
pid_t pid;
pid =fork();
int retorno;

   if(pid <0)
  {	   	
   printf("Fork Error, Sorry ");
   exit(1); 
  }	

           if(pid==0)
           { 
     execvp(*args, args);
     //printf("Erro: Comando nao executado\n");	 
     printf("%d\n", retorno=execvp(*args, args)); //para verificar o que esta a retornar  
             exit(1);         
           }

   while(wait(&status)!=pid);

}

int verificacao( char *cmd, char **args)
{
         while (*cmd!='\0')
     {
	   if(*cmd == ' ' || *cmd =='\t' || *cmd=='\n')
		{

            		  *cmd++ =NULL;
		}
            		  *args++ = cmd;

            if(*cmd !='\0'&& *cmd !='\t' && *cmd !='\n'){

	cmd++;}
     }
     *args=NULL;
}

main(void)
{
    char cmd[80];
    char *args[20]; 
    int i=0;


for (i;i==0;) // loop sem fim
         {
           printf(">> ");
      if (fgets(cmd,80,stdin) == NULL)
    {
              	  printf("\n");
                  exit(1);
    }
  verificacao(cmd, args);
	  if (args[0]=="terminar")
              exit (0);	
  executar(args);

         } 
}

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.