1

I am new to C programming language and i am trying to do an exercise that i have set myself.

What I am wanting to do is to be able to read in a command that the user writes and then execute it. I haven't written any code for this yet and i am really unsure on how to do it.

This is basically what i am wanting to do:

display a user prompt (for the user to enter a command e.g. /bin/ls -al) read and process the user input

I am currently using MINIX to try and create something and change the OS.

Thanks

user3411748
  • 55
  • 1
  • 1
  • 6
  • Please specify your question and post what you have already tried. Sounds like you want to develop a shell for MINIX? So you need `printf`, `scanf`, `fork` and `execve`. – code monkey Nov 04 '14 at 15:18
  • Yes i do want to develop a shell for MINIX. I want to try and use one of the functions: getline, getdelim and strtok. I haven't currently tried anything as i am unsure as how to do this – user3411748 Nov 04 '14 at 15:25
  • I am Just wanting some sort of guide line on where to start and how to start with the getline function – user3411748 Nov 04 '14 at 15:32

3 Answers3

0

I will give you a direction:

use gets to read a line: http://www.cplusplus.com/reference/cstdio/gets/

you can display with printf

and use system to execute the call: http://www.tutorialspoint.com/c_standard_library/c_function_system.htm

read a little bit about this function get yourself familiar with them.

antonpuz
  • 3,256
  • 4
  • 25
  • 48
0

A Shell executes commands in a new process. That's how it works in general:

while(1) {
    // print shell prompt
    printf("%s", "@> ");
    // read user command - you can use scanf, fgets or whatever you want
    fgets(buffer, 80, stdin);
    // create a new process - the command is executed in the new child process
    pid = fork();
    if (pid == 0) {
        // child process
        // parse buffer and execute the command using execve
        execv(...);
    } else if (pid > 0) {
        // parent process
        // wait until child has finished
    } else {
        // error
    }
}
code monkey
  • 2,094
  • 3
  • 23
  • 26
0

This is my code i have so far:

include

int main(void) {
    char *line = NULL;  
    size_t linecap = 0; 
    ssize_t linelen;    

    while ((linelen = getline(&line, &linecap, stdin)) > 0){
        printf("%s\n", line);
    }

}

This will obviously keep executing and print out a line until i press CTRL-D. What sort of code would i use to now execute the command that the user types in?

user3411748
  • 55
  • 1
  • 1
  • 6