Hello and thank you for attention. I am writing my own shell and I have small problem with redirect output to file. For example user wrote: ls -l >> output. If I find ">>", I should redirect first part of command, I mean call effect ls -l to file "output". I try to do it in case 1 but to file is redirected just one lane and the program is stopped, there is not appear "Shell -> " and nothing is going on. Can you give some advice to solve that problem? Thanks.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
int *parse(char *linia, char **argv)
{
    int counter = -1;
    while (*linia != '\0') 
    {       
        while (*linia == ' ' || *linia == '\t' || *linia == '\n')
            *linia++ = '\0';  
        *argv++ = linia; 
        counter++;   
        while (*linia != '\0' && *linia != ' ' &&  *linia != '\t' && *linia != '\n') 
            linia++;            
    }
    *argv = '\0'; 
    return counter;
}
void execute(char **argv)
{
    pid_t pid;
    int status;
    if ((pid = fork()) < 0) 
    {     
        printf("*** ERROR ***\n");
        exit(1);
    }
    else if (pid == 0) 
    {          
        if (execvp(*argv, argv) < 0) 
        {    
            printf("*** ERROR ***\n");
            exit(1);
        }
    }
    else 
    {                                  
        while (wait(&status) != pid);
    }
}                
int specialChar(char *argv)
{
    int i=0;
    while(argv[i]!='\0')
    {
        if(argv[i]=='>' && argv[i+1]=='>')
            return 1;           
        else if(argv[i]=='&')
            return 2;           
        else if(argv[i]=='|')
            return 3;           
        i++;
    }
}
void  main()
{
    char command[20];
    char *argv[64];
    char **history = (char**)malloc(20*sizeof(char*));
    int counter1=-1;
    int counter2=0;
    for(counter2 = 0; counter2<20; counter2++)
    {
        history[counter2]=(char*)malloc(100*sizeof(char));
    }
    int start = 0;
    FILE *file;
    file=fopen("his", "w");
    if(!file)
        printf("ERROR"); 
    int i=0;
    while (1) 
    {       
        printf("Shell -> ");     
        gets(command);
        counter1++;
        strcpy(history[counter1],command);
        fopen("his", "w");
        if(counter1<20)
            for(i=0; i<=counter1; i++)
            {
                fprintf(file,"%s\n",history[i]);
            }    
        else
            for(i=counter1-20; i<counter1; i++)
            {
                fprintf(file,"%s\n",history[i]);
            }
        fflush(file);
        printf("\n"); 
        switch(specialChar(command))
        {       
            case 1:
            i = parse(command, argv);
            int file1 = open(argv[i], O_APPEND | O_WRONLY);
            dup2(file1,1)   ;  
            if (strcmp(argv[0], "exit") == 0) 
                  exit(0);       
            execute(argv); 
            close(file1);
            break;
            case 2:
            break;
            case 3:
            break;
            default:
            parse(command, argv);      
            if (strcmp(argv[0], "exit") == 0) 
                  exit(0);       
            execute(argv);  
            break;     
        }
        fclose(file);
        }
}
