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.ciprianpungila.com/uvt/so2011/teme/46.txt
and here is what i have done:
Thanks and looking forward for some solutions
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.ciprianpungila.com/uvt/so2011/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