Trying to use execvp in C but it fails

I'm trying to execute execvp() in C (I'm creating a shell for "bash" command)
but after separating a given line from the user , it fails .

Here is the code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
		 int i;

	     char *line = malloc(BUFFER);
	     char *origLine = line;
	     fgets(line, 128, stdin);   // get a line from stdin


	     // get complete diagnostics on the given string

	     lineData info = runDiagnostics(line);

	     char command[20];
	     sscanf(line, "%20s ", command);
	     line = strchr(line, ' ');

	     printf("The Command is: %s\n", command);

	     int currentCount = 0;					// number of elements in the line
	     int *argumentsCount = &currentCount;   // pointer to that


	     // get the elements separated

	     char** arguments = separateLineGetElements(line,argumentsCount);


	     // here we call a method that would execute the commands


	     pid_t pid = fork();
	     if (pid < 0)

	    	 printf("Could not fork!");

	     else if (pid == 0)  // fork was successful
	     {
	 		if (execvp(*arguments,arguments)  < 0)       // execute the command
	 		{
				printf("ERROR: FORK failed\n");
				exit(1);
	 		}

	     }

	     else // parent
	     {
      	    int status = 0;
			pid = wait(&status);
			printf("Process %d returned with status %d.", pid, status);
	     }



		if (execvp(arguments[0],arguments)  < 0)       // execute the command
		{
					printf("ERROR: exec failed\n");
					exit(1);
		} 


The fork() fails and also when I try to execute execvp(arguments[0],arguments) it also fails .

Any idea where did I go wrong ?

Thanks .
David
Last edited on
¿What does perror() say?
Okay , I fixed this for the moment .

My question now is : when I want to execute a command (FYI , I'm building a shell for the bash command) , after I supply the name of the program that I want to run , with its arguments : if I need to write from a file , or write to a file , do I need to open it manually , or is it enough to give execvp() the arguments , and execvp() would find the file by itself ?


10x
okay thank you very much .
Topic archived. No new replies allowed.