Here's my code:
/* myshell program */
#include<stdio.h>
#include <string.h>
#define MAX_LENGTH 40
#define TRUE 1
char cmd[MAX_LENGTH];
char args[MAX_LENGTH];
void prompt() {
printf("%s", "Shell: ");
scanf("%s", cmd);
char *line = NULL;
size_t linecap = 0;
ssize_t linelen;
if ((linelen = getline(&line, &linecap, stdin)) > 0) {
strcpy(args, line);
}
}
int main(char *envp[]) {
while (TRUE) {
prompt();
if (fork() == 0) {
execve(cmd, args, envp);
perror("execv");
}
}
}
Whenever I input (for example) /bin/ls, I get an error stating "Bad address". I have checked cmd and args and made sure that they receive correct values.
Check the below link: Arguments to main in C
The parameters for
main()
areint argc
and char*argv[]