Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <stdlib.h>
- #include <string.h>
- char **parse_cmdline( const char *cmdlin );
- int main(int argc,char **argv){
- int check_for_ending = 0;
- char string[2];
- string[0] = '$';
- string[1] = ' ';
- char new_line[1];
- new_line[0] = '\n';
- while(1){
- write(1,string,2);
- //printf("$ ");
- int t=1,i=0;
- char buffer[1];
- char *suffer = malloc(t*sizeof(char));
- //int size=0;
- /* int vladi = getline(&suffer,&size,stdin);
- if( feof(stdin) ) {
- break;
- }
- suffer[vladi-1]='\0';
- */
- while(1){
- int g = read(STDIN_FILENO,buffer,1);
- if(g==0){
- check_for_ending = 1;
- break;
- }
- if(buffer[0]=='\n'){
- suffer[i] = '\0';
- break;
- }
- suffer[i] = buffer[0];
- i++;
- if(i==t)
- {
- t=t+1;
- suffer = realloc(suffer,t*sizeof(char));
- }
- }
- if(check_for_ending){
- //write(STDIN_FILENO,new_line,1);
- break;
- }
- char **masivche = parse_cmdline(suffer);
- /*char *pointer = masivche[0];
- char *pointer1 = masivche[1];
- char *pointer2 = masivche[2];
- char *pointer3 = masivche[3];
- write(STDIN_FILENO,pointer,strlen(masivche[0]));
- write(STDIN_FILENO,pointer1,strlen(masivche[1]));
- write(STDIN_FILENO,pointer2,strlen(masivche[2]));
- return 0;*/
- char *pointer = masivche[0];
- if(pointer==NULL){continue;}
- pid_t pid = fork();
- if(pid < 0){
- perror("fork");
- continue;
- }
- if(pid == 0){
- int p = execv(masivche[0],masivche);
- if(p < 0){
- perror(masivche[0]);
- exit(0);
- }
- exit(0);
- }else{
- waitpid(pid, 0 ,0);
- }
- free(masivche[0]);
- free(masivche);
- }
- return 0;
- }
- char **parse_cmdline( const char *cmdlin ){
- char *string = malloc(strlen(cmdlin)*sizeof(char));
- strcpy(string,cmdlin);
- int op=0;
- char **array_of_commands = NULL;
- char *token = strtok(string, " ");
- while(token != NULL )
- {
- op++;
- array_of_commands = realloc(array_of_commands,(op+1)*sizeof(char*));
- array_of_commands[op-1] = token;
- token = strtok(NULL, " ");
- }
- array_of_commands[op]=NULL;
- return array_of_commands;
- /*int i=2,j,y=0,help=0,k=2,array_counter=0;
- char **array_of_commands = malloc(k*sizeof(char*));
- char *command = malloc(i*sizeof(char*));
- for(j=0;;j++){
- if(cmdlin[j]!=' '){break;}
- }
- for(;;j++){
- if(cmdlin[j]=='\0'){break;}
- while(cmdlin[j]!=' '){
- command[y] = cmdlin[j];
- j++;y++;
- if(y == i){
- i*=2;
- command = realloc(command,i*sizeof(char));
- }
- help=1;
- }
- if(help == 1){
- command[y]='\0';
- y=0;
- array_of_commands[array_counter] = command;
- array_counter++;
- if(array_counter + 1 == k){
- k*=2;
- array_of_commands = realloc(array_of_commands,k*sizeof(char*));
- }
- }
- help = 0;
- }*/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement