0

I am using linux and I want to write a program in c that reads commands from user until I enter stop.For each command the main program will create a process A which will create another process B.The process B will execute the command entered by user.I want to make it work using exec but it only works for one word commands (ex:pwd , ls) , if i enter for example ls -l it says that there is no such file or directory.The code i wrote so far :

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

#define N 100

int main() {
    char input[N];
    scanf(" %[^\n]s",input); //reads from user inputs that can include space 
    while (strcmp(input,"stop") != 0) { // verifies if entered command == stop
        int a;
        a = fork();//creates the process A
        if (a == -1) {
            perror("fork imposibil!");
            exit(1);
        }
        if (a == 0) {
            printf("a\n");//i printed that to verify if my fork works
            int b;

            b = fork();//creates process B
            if (b == -1) {
                perror("fork imposibil!");
                exit(1);
            }
            if (b == 0) { // the part that makes me problems , i try to use exec to execute the command that is stored in input
                printf("b\n");
                if (execlp(input,"",(char*) 0) < 0) {
                    perror("Error exec");
                    exit(0);
                }
                exit(0);
            }
            wait(0);
            exit(0);
        }
        wait(0);

        scanf(" %[^\n]s",input);
    }
    return 0;
}

Can someone help me please ? P.S. I don't think that my code was so impossible to read but i hope it's better now

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
Damian Alex
  • 15
  • 1
  • 8
  • First of all learn how to write readable code, and then you might ask a question, because reading your code as it was, was almost imposible. For example, i didn't see the `exit()` after `perror()`, didn't see it at all, and I thought, Oops, he reports the errors, but still ignores them. And I think you should call `_exit(0)` instead of `exit(0)` from the child process. – Iharob Al Asimi Apr 17 '15 at 14:14

2 Answers2

2

From your example ls -l, the -l should be passed to the argument.

int execlp(const char *file, const char *arg, ...);

The citation from the help says

The function can be thought of as arg0, arg1, ..., argn. Together they describe a list of one or more pointers to null-terminated strings that represent the argument list available to the executed program. The first argument, by convention, should point to the filename associated with the file being executed. The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.

deimus
  • 9,565
  • 12
  • 63
  • 107
0

You are not passing arguments to the function execlp() the way it expects them to be.

The initial argument for these functions is the name of a file that is to be executed.

The const char *arg and subsequent ellipses in the execl(), execlp(), and execle() functions can be thought of as arg0, arg1, ..., argn. Together they describe a list of one or more pointers to null-terminated strings that represent the argument list available to the executed program. The first argument, by convention, should point to the filename associated with the file being executed. The list of arguments must be terminated by a NULL pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.

Have a look at this explanation on how to use execlp() function- I do not understand how execlp() works in Linux

I guess you would have to use a string splitter function with a delimiter " " to get the command line arguments like -l in your case ls -l.

strtok() provides this functionality in C.

#include <string.h>
#include <stdio.h>

int main(void)
{
    char input[] = "A bird came down the walk";
    printf("Parsing the input string '%s'\n", input);
    char *token = strtok(input, " ");
    while(token) {
        puts(token);
        token = strtok(NULL, " ");
    }

    printf("Contents of the input string now: '");
    for(size_t n = 0; n < sizeof input; ++n)
        input[n] ? printf("%c", input[n]) : printf("\\0");
    puts("'");
}
Community
  • 1
  • 1
Abhishek Jain
  • 3,562
  • 2
  • 26
  • 44