Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Wiki Markup
Command line arguments are passed as arguments to {{main()}}. In the following definition for {{main()}}  the array members {{argv\[0\]}} through {{argv\[argc-1\]}}  inclusive contain pointers to strings.

...

Code Block
int main(int argc, char *argv[]) {
  ... 
  char prog_name[128];
  strcpy(prog_name, argv[0]);
  ... 
}

Non-compliant Code Example 2

...

Code Block
int main(int argc, char *argv[]) {
  ... 
  char * prog_name = (char *)malloc(strlen(argv[0])+1);
  if (prog_name != NULL) {
    strcpy(prog_name, argv[1]);
  }
  else {
    /* Couldn't get the memory - recover */
  }
  ... 
}

Compliant Solution 2

The strlen() function should be used to determine the length of environmental variables so that adequate memory can be dynamically allocated:

Code Block
  char *editor;
  char *buff;

  editor = (char *)getenv("EDITOR");
  if (editor) {
    buff = (char *)malloc(strlen(editor)+1);
    strcpy(buff, editor);
  }

References