int main(void) { int res,ret; char cmd[MAXCMD]; struct cmdlist cmds; struct rlimit r; r.rlim_cur = 50; r.rlim_max = 10000; setrlimit(RLIMIT_FSIZE,&r); /* setting up the initial values of the structure */ /* this should be done every time when new object is created */ setupnewcommand(&cmds); /* Setting up file size limit for this process and for the child processes */ /* main loop of the shell */ /* this loop proceses command wirtten by user in the following steps: */ /* 1. Displaying command prompt 2. Reading command from the std input 3. Parsing this command 4. Executing parsed commands 5. Dealocating used memory */ while(1){ printprompt(); if(readcmd(cmd, MAXCMD) == RESERROR) continue; res = parsecmd(cmd, MAXCMD, &cmds); printparsedcmds(&cmds); ret = executecmds(&cmds); dealocate(&cmds); } return 0; }
/* --- execute a shell command --- */ int executeshellcmd(Shellcmd *shellcmd) { printshellcmd(shellcmd); Cmd *cmds; char **cmd0; cmds = shellcmd->the_cmds; cmd0 = cmds->cmd; if(strcmp(*cmd0,"exit") == 0) { return 1; } int i = executecmds(cmds, shellcmd->rd_stdin, shellcmd->rd_stdout, shellcmd->background); if(i == -1) { printf("Command not found\n"); } return 0; }
int main(int argc,char *argv[]) { int i,j,len,p1=0,p2=0,flag=0; char *filename = argv[1]; FILE *fp; fp = fopen(filename,"r"); fscanf(fp,"%d",&len); char **A,**B; A = (char **)malloc(len*sizeof(char *)); B = (char **)malloc(len*sizeof(char *)); for(i=0;i<len;i++) { A[i] = (char *)malloc(80*sizeof(char)); B[i] = (char *)malloc(80*sizeof(char)); } char command[50],depend[50],env[50],option[50],fullcom[80],tmp[50]; for(i=0;i<len;i++) { flag = 0; fscanf(fp,"%s",command); fscanf(fp,"%s",depend); fscanf(fp,"%s",env); fscanf(fp,"\t%[^\n]s",option); if(strcmp(option,"-") == 0) { strcpy(option,""); } int dep = strcmp(depend,"-"); strcpy(fullcom,command); strcat(fullcom,"\t"); strcat(fullcom,env); strcat(fullcom,"\t"); strcat(fullcom,option); if( dep == 0) { if(p1<=p2) { strcpy(A[p1],fullcom); p1++; } else { strcpy(B[p2], fullcom); p2++; } } else { for(j=0;j<p1;j++) { sscanf(A[j],"%s",tmp); if( strcmp(tmp,depend) == 0 ) { flag = 1; strcpy(A[p1],fullcom); p1++; break; } } if(!flag) { strcpy(B[p2],fullcom); p2++; } } } fclose(fp); executecmds(A,B,p1,p2); return 0; }