Bad address C (command-line interpreter)

419 views Asked by At

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.

1

There are 1 answers

1
Gopi On

Check the below link: Arguments to main in C

The parameters for main() are int argc and char *argv[]

int main(int argc,char *argv[])