All pastes #397502 Raw Edit

Someone

public text v1 · immutable
·published 2007-03-16 15:18 UTC
rendered paste body
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>


void parser(char* buf, char** args) ;
void execution(char** args) ;

main()
{
	char buf[1024];
	char* args[64];
	
	for(;;)
	{
		fflush(stdin);
		printf("Command:\n"); //&#960;&#945;&#943;&#961;&#957;&#949;&#953; &#949;&#957;&#964;&#959;&#955;&#942; &#945;&#960;&#972; &#967;&#961;&#942;&#963;&#964;&#951;
		if(fgets(buf, sizeof(buf)-1,stdin)==NULL)
		{
			printf("\n"); exit(0);
		}
		parser(buf,args);
		execution(args);
	}
}

void parser(char* buf, char** args)
{
	while (*buf != '\0')
	{
		while((*buf==' ')||(*buf=='\t')||(*buf=='\n'))
		{	
			*buf++ = '\0';
		}

		*args++=buf; 
		
		while ((*buf!='\0') && (*buf!=' ') && (*buf!='\t') && (*buf!='\n'))
		{
			buf++;
		}
	}
	
	*--args = NULL;
	
}

void execution(char** args)
{
	int pid, status ;

	if((pid=fork())<0)
	{
		perror("fork"); exit(1);
	}
	
	if(pid==0)
	{
		execvp(*args,args);
		perror("*args");
		exit(1);
	}
	
	while (wait(&status)!=pid);
	
}