Trying to read from file

349 Views Asked by At

I'm trying to create a program that runs commands from user input.
At the moment it works for multiple word commands but I'm trying to implement redirections.
I started with taking input from a file and it's not working but I'm not getting any error (I'm testing using the "wc -l < text.txt" command, the text.txt file is the the same dir as the program.)
Here is the code:
- input is the str with the user's input
- before coming to this method I already checked that it has a redirection on it

redirect(int proc, char * input){
    char * comm;
    if(proc == 1){ //in
        comm = strsep(&input, "<");
    }
    else{ //out
        comm = strsep(&input, ">");
    }

    int proc2 = check(input);
    if(proc2 == 0){ //only one redirection
        if(proc == 1){ //in
            input = trim(input);
            int fd = open(input, O_RDWR);
            close(0);
            dup2(fd, 0);
            close(fd);

            comm = trim(comm);
            char ** words = parse(comm);

            char str[105];
            strcpy(str, "/bin/");
            strcat(str, words[0]);
            shrink(str);
            if(!execvp(str, words)){    /*exec failed */
                exit(1);
            }
        }
        else{ //out

        }
    }
    else{ //more than one redirection/pipe

    }
}

edit
I need to use the execvp command to run the user input.
The user command "<" needs to change the stdin to be the file after it.
I changed the stdin to be the text.txt but I don't know how to pass it as an arg so the execvp can run it.

3

There are 3 best solutions below

0
On BEST ANSWER

After a lot of testing and research I found that the file that I was using did not have the permissions necessary for the execvp to read it from it.
I figure it out when I wrote the code for writing into a file and then try to read that newly created file and it worked (after adding the flags).
Here is the code:

redirect(int proc, char * input){
    char * comm;
    if(proc == 1){ //in
        comm = strsep(&input, "<");
    }
    else{ //out
        comm = strsep(&input, ">");
    }

    int proc2 = check(input);
    if(proc2 == 0){ //only one redirection
        if(proc == 1){ //in
            input = trim(input);
            int fd = open(input, O_RDWR);
            close(0);
            dup2(fd, 0);
            close(fd);

            comm = trim(comm);
            char ** words = parse(comm);

            if(!execvp(words[0], words)){   /*exec failed */
                exit(1);
            }
        }
        else{ //out
            input = trim(input);
            int fd = open(input, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
            dup2(fd, 1);
            close(fd);

            comm = trim(comm);
            char ** words = parse(comm);

            if(!execvp(words[0], words)){   /*exec failed */
                exit(1);
            }
        }
    }
    else{ //more than one redirection/pipe

    }
}

So I run the command "ls > text.txt" and it creates the text.txt file with the "ls" results in it and then run the "wc -l < text.txt" command and it returns the lines in the file.

1
On

If its just about execution of the user specified commands then to execute the shell commands using you can use the system() call. This function takes the command to be executed as argument and executes it on the command shell. You would not need to make any separate file.

you can take the command user wants to execute as string and then pass it to system() as argument to execute it. ie

 system("wc -l < text.txt");

System() works fine for both Linux and Windows.

References : Execute a Linux command in the c program

http://www.gnu.org/software/libc/manual/html_node/Running-a-Command.html

1
On

what types of commands you are trying to execute ? if your commands are dos commands, you can read the user inputs in a string variable , and when you want to execute the file . create a bat file then execute it by the following code

Process.Start("your file path ");