execvp problem?

I've spent quite a few hours looking over the code, but cannot find the source of the problem. All I know is that execvp doesn't find the program. I can specify the program using absolute links but otherwise it doesn't work. According to the man pages, execvp is suppose to search the path env variable, which clearly is there (echo $PATH shows it). I tested it with "pwd" and "ls" which don't work. "/bin/echo" does work though.

I'm probably looking at the wrong place. Thanks for any help you may provide.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>

#define count 20
#define start 5

typedef enum boolean { false, true } bool;

char** parse(char* line, int length) {

	//brute force!
	int sizeofarray = 1;
	char **array = (char**) malloc(sizeof(char*) * sizeofarray * start);
	char *token = (char*) strtok(line, " ");
	array[0] = token;
	int i = 1;

	while (token != NULL) {

		token = (char*) strtok(NULL, " \n");
		array[i] = token;
		i++;
		if (i >= start*sizeofarray) {

			sizeofarray++;
			array = realloc(array, sizeof(char*) * sizeofarray * start);

		}

	}

	return array;

}

int main(int argc, char **argv) {

	bool quit = false;
	size_t size = sizeof(char) * count;
	char *command = (char*) malloc(size);

	while (!quit) {

		printf("\nVincent's Shell> ");
		int size = getline(&command, &size, stdin);
		if (!strcmp(command, "quit\n") || !strcmp(command, "exit\n")) {

			quit = true;

		} else if (size > 0) {

			char** output = parse(command, size);
			pid_t pid = fork();
			if (pid < 0) {

				printf("Could not fork!");

			} else if (pid == 0) {
				//child
				execvp(*output, output);
				//printf("That's not good!  I haven't transformed!\n");
				printf("%s\n", (char*) strerror(errno));
				exit(1);

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

			}
		
		}

	}

	return 0;

}
That's because pwd and ls are shell subcommands. They don't exist as standalone executables...

[edit] Oops! I'm wrong! Dumb answer!
Last edited on
On my system...


> which ls
alias ls='ls -F'
        /bin/ls
> which pwd
/bin/pwd


execvp requires that the argv array be terminated with a null pointer. I don't think you are doing that.

The problem is that when you type "ls<ENTER>" the name of the executable you are trying to run
is "ls<ENTER>" instead of just "ls". You need to strip the newline.
Hmm... sorry for the brainless answer. You're right, of course. :-\
OH!

You're absolutely right. I added the newline to the delimiters and now it works like a charm!

Thank you!
Topic archived. No new replies allowed.