6 Replies - 475 Views - Last Post: 18 January 2013 - 05:06 AM Rate Topic: -----

#1 claudiud1992  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-January 13

Command line interpretor help C

Posted 16 January 2013 - 01:17 AM

Hy everyone, i`ve just about to finish a homework but i got in some trouble:
1. The "rm" isn`t working as is should(the -r and the last command isn`t working)
2. Need some help implementing uniq
3. Any improvements to this code, i think it has some errors, unknown ones.
4. Homework page : milestone 3
http://www.ciprianpu...011/teme/46.txt

and here is what i have done:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>
#include<fcntl.h>
#define BUFFER_SIZE 256
#define HISTORY_LENGTH 128

int exitCommand(char*cmd){
	return (!strcmp(cmd,"exit\n"));
}
void versionCommand()
{
	printf("v1.0000");
}
void helpCommand(){
	printf("1. The RM command. Parameters implemented are: -i, -r (-R), -v\n2. The UNIQ command. Parameters implemented are: -i, -d, -u\n3. The DIR command. No parameters need to be implemented.\n");
}

int main(int argc, char*argV[]) {
	int i;
	char *cmd=(char*)malloc(sizeof(char)*BUFFER_SIZE);
	char **history=(char **)malloc(HISTORY_LENGTH*sizeof(char*));
	//char history[HISTORY_LENGTH][BUFFER_SIZE];	
	int historylength=0;
	int current=0;
	int status,n;
	int fd[2];
	do{
		printf("$>"); 
		fgets(cmd,BUFFER_SIZE-1,stdin);
		history[historylength]=(char*)malloc(sizeof(char)*BUFFER_SIZE);
		strcpy(history[historylength],cmd);
		historylength++;
		current++;
		if(strcmp(cmd,"+\n")==0)
			{
			if(historylength>current+1)
				{current=current-0; free(history[historylength]); historylength--; printf("%s",history[current]); }
			else
				{current=historylength-2; free(history[historylength]); historylength--; }
			}
		else
		if(strcmp(cmd,"-\n")==0)
			{
			if(current>=2)
				{current=current-2; free(history[historylength]); historylength--; printf("%s",history[current]); }
			else
				{current=-1; free(history[historylength]); historylength--; }
			}
		else
		if(strcmp(cmd,"help\n")==0)
			{helpCommand();}
		else
		if(strcmp(cmd,"version\n")==0)
			{versionCommand();}
		else
		if(exitCommand(cmd))
			{break;}
		else
		{
		char arg1[10]="/bin/",arg2[10],arg3[10];
		char arg1b[10]="/bin/",arg2b[10],arg3b[10];
		char arg1c[10]="/bin/",arg2c[10],arg3c[10];
		int i=0,ind=0,j=5,k=0,l=0;
		int jb=5,kb=0,lb=0;
		int jc=5,kc=0,lc=0;
		char argFile[10]; int argFilei=0,indFile=5;
		while(i<strlen(cmd)-1)
			{
			if(cmd[i]==' ')
				{if(ind==0) ind=1; if(ind==2) ind=3; if(ind==8) ind=9;}
			else
			if(cmd[i]=='|')
				{ if(ind==1){ind=2; i++; indFile=6;} if(ind==3){ind=8; i++; indFile=7;} }
			else
			if(cmd[i]=='>')
				{if(indFile==5) ind=5; if(indFile==6) ind=4; if(indFile==7) ind=6; }
			else
				{
				if(ind==0) //first command's command
					{arg1[j]=cmd[i]; j++; arg2[k]=cmd[i]; k++;}
				if(ind==1) //first command's argument
					{arg3[l]=cmd[i]; l++;}
				if(ind==2) //second command's command
					{arg1b[jb]=cmd[i]; jb++; arg2b[kb]=cmd[i]; kb++;}
				if(ind==3) //second command's argument
					{arg3b[lb]=cmd[i]; lb++;}
				if(ind==4||ind==5) //file to output to
					{argFile[argFilei]=cmd[i]; argFilei++; }
				if(ind==8) //third command's command
					{arg1c[jc]=cmd[i]; jc++; arg2c[kc]=cmd[i]; kc++;}
				if(ind==9) //third command's argument
					{ arg3c[lc]=cmd[i]; lc++;}
				}
			i++;
			}
		arg1[j]='\0'; arg2[k]='\0'; arg3[l]='\0';
		arg1b[jb]='\0'; arg2b[kb]='\0'; arg3b[lb]='\0';
		arg1c[jc]='\0'; arg2c[kc]='\0'; arg3c[lc]='\0';
		argFile[argFilei]='\0';
		//printf("%s\n%s\n%s\n",arg1,arg2,arg3); printf("%s\n%s\n%s\n",arg1b,arg2b,arg3b); printf("%s\n%s\n%s\n",arg1c,arg2c,arg3c); printf("%s\n",argFile);

		int pid,pid2;
		if((pipe(fd))<0)
				{printf("pipe() error"); exit(1);}
		if(ind==0||ind==1||ind==5)
			{		
			if((pid=fork())<0)
				{printf("fork() error"); exit(1);}
			if(pid==0)
				{
if(ind==5) 
		{ int efde=open(argFile, O_RDWR|O_CREAT); dup2(efde,1); dup2(efde,2); close(efde); }
				if(arg3[0]=='\0') 
				execlp(arg1,arg2,NULL); 
				else 
				execlp(arg1,arg2,arg3,NULL);
				}
			//printf("com singura");
			wait(&status);
			}
		if(ind==2||ind==3||ind==4)
			{ 
			if((pid=fork())<0)
				{printf("fork() error"); exit(1);}
			if(pid==0)
				{
			close(fd[0]); 
dup2(fd[1],1); close(fd[0]); if(arg3[0]=='\0') execlp(arg2,arg2,NULL); else execlp(arg2,arg2,arg3,NULL); wait(&status); //printf("com1"); 
				}
			else
				{ if((pid2=fork())<0){ printf("fork() error"); exit(1);} if(pid2==0) { 
if(ind==4) 
		{int efde=open(argFile, O_RDWR|O_CREAT); dup2(efde,1); dup2(efde,2); close(efde); }
			//else{
	close(0); dup(fd[0]); close(fd[1]);  //}

if(arg3b[0]=='\0') execlp(arg2b,arg2b,NULL); else execlp(arg2b,arg2b,arg3b,NULL); wait(&status); //exit(0); //printf("com2"); 
				}
} 
			}
		if(ind!=4) {		
		char s[256]; close(fd[1]); wait(&status); while((n=read(fd[0],s,255))>0) { s[n]='\0'; printf("%s",s); } }
if(ind==8||ind==9||ind==7)
{
printf("Three commands\n");
if(pid=fork()<0) {printf("fork() error"); exit(1);}
if(pid==0)
{close(fd[0]); dup(fd[1]); close(fd[0]); if(arg3[0]=='\0') execlp(arg2,arg2,NULL); else execlp(arg2,arg2,arg3,NULL);}
else 
{if(pid2=fork()<0) {printf("fork() error"); exit(1);}
if(pid2==0)
{close(fd[0]); dup(fd[1]); close(fd[0]); if(arg3b[0]=='\0') execlp(arg2b,arg2b,NULL); else execlp(arg2b,arg2b,arg3b,NULL);}
else
{
//close(0); dup(fd[0]); close(fd[1]); 
int pid3;
pid3=fork();
if(pid3==0)
if(arg3c[0]=='\0') execlp(arg3c,arg3c,NULL); else execlp(arg2c,arg2c,arg3c,NULL);}
}		 
}}
		
	wait(&status);}while(1);
	free(cmd);
	for(i=0;i<historylength;i++)
		free(history[i]);
	free(history);
	return 0;
}



Thanks and looking forward for some solutions

Is This A Good Question/Topic? 0
  • +

Replies To: Command line interpretor help C

#2 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,472
  • Joined: 20-March 10

Re: Command line interpretor help C

Posted 16 January 2013 - 04:22 AM

Well,

what is unistd.h ??

your main is also too cluttered it should really just be a message pump
pumping the user for commands...

all the processing should be done in functions.

Snoopy.
Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,315
  • Joined: 21-June 11

Re: Command line interpretor help C

Posted 16 January 2013 - 05:57 AM

View Postsnoopy11, on 16 January 2013 - 12:22 PM, said:

what is unistd.h ??


It's a POSIX header that declares various functions that the OP is using including the exec family of functions.
Was This Post Helpful? 1
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,480
  • Joined: 05-May 12

Re: Command line interpretor help C

Posted 16 January 2013 - 06:05 AM

Also, my reading of the assignment is that you are suppose to implement those 3 commands internally within your program. You are cheating and calling execlp and and having the operating system to run the commands for you.

This post has been edited by Skydiver: 16 January 2013 - 06:05 AM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5936
  • View blog
  • Posts: 12,862
  • Joined: 16-October 07

Re: Command line interpretor help C

Posted 16 January 2013 - 07:55 AM

This makes a little more sense if you peek at milestones 1 and 2 of assignment: http://www.ciprianpu...vt/so2011/teme/

Obviously, we're starting at 2 here and nothing's been done for 3 at all. What we do have makes a ton of assumptions and only deals with a finite number of pipes. This is unlikely to be valid for any reasonable test.

I would note that there is no reason to use malloc at all in this program...

I'd have a few structs and maybe even some linked lists for this, in which case a malloc would make sense.
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Command line interpretor help C

Posted 16 January 2013 - 11:28 PM

That formatting is horrendous, my recommendation is fixing that before you do anything else.
Was This Post Helpful? 1
  • +
  • -

#7 undefined behaviour  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 36
  • Joined: 17-January 13

Re: Command line interpretor help C

Posted 18 January 2013 - 05:06 AM

Which book are you reading?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1