C.Unbay
C.Unbay

Reputation: 2826

generating main arguments without the parameter

In C programming language, is it possible to access int argc or char **argv without using the parameters? I know some of you might ask why this is needed, just for research purposes.

Is it possible to generate the cmd line arguments without using the main parameter variables ? For example, to illustrate some pseudo code, that i have in mind,

LPTSTR cmd  = GetCommandLine();
splitted = cmd.split(" ") //split from spaces
char **someArgv.pushForEach Splitted, length++

and you'd have a someArgv with the parameters and length as argc, this'd really help to know if possible to illustrate.

Upvotes: 0

Views: 357

Answers (2)

chux
chux

Reputation: 153338

If OP already has the command as a string, then:

  • Form a copy of the string

  • Parse it for argument count

  • Allocate for argv[]

  • Parse & tokenize copy for each argv[]

  • Call main()


#include <string.h>
#include <stdlib.h>
#include <stdio.h>

// Not standard, but commonly available
char *strdup(const char *s);

// Return length of token and adjust offset to the next one
// Adjust as needed
// Presently only ' ' are used to separate
// More advanced would have escape characters, other white-space, etc.
size_t tokenize(const char *s, size_t *offset) {
  // find following space
  size_t len = strcspn(s + *offset, " ");
  *offset += len;
  // find first non-space
  *offset += strspn(s + *offset, " ");
  return len;
}

int call_main(const char *cmd) {
  char *cmd2 = strdup(cmd);
  cmd2 += strspn(cmd2, " "); // skip leading spaces

  size_t offset = 0;
  int argc = 0;
  while (tokenize(cmd2, &offset) > 0) {
    argc++;
  }

  char **argv = malloc(sizeof *argv * ((unsigned)argc + 1u));
  offset = 0;
  for (int a = 0; a < argc; a++) {
    argv[a] = &cmd2[offset];
    size_t len = tokenize(cmd2, &offset);
    argv[a][len] = '\0';
  }
  argv[argc] = NULL;

  int retval = 0;
#if 0
  retval = main(argc, argv);
#else
  printf("argc:%d argv:", argc);
  for (int a = 0; a < argc; a++) {
    printf("%p \"%s\", ", argv[a], argv[a]);
  }
  printf("%p\n", argv[argc]);
#endif

  free(cmd2);
  free(argv);
  return retval;
}

Sample

int main() {
  call_main("  name 123  abc  456 ");
}

argc:4 argv:0x800062322 "name", 0x800062327 "123", 0x80006232c "abc", 0x800062331 "456", 0x0

Pedantic: The strings provided to main() should be modifiable. Avoid code like

argv[1] = "Hello";
....
main(argc, argv);

Upvotes: 1

Devolus
Devolus

Reputation: 22074

#include <stdio.h>

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

int callMain(void)
{
    char *argv[4];

    argv[0] = "binary";
    argv[1] = "param1";
    argv[2] = "param2";
    argv[3] = NULL;

    return main(3, argv);
}

int main(int argc, char *argv[])
{
    if (argc <= 1)
    {
        return callMain();
    }

    printf("ARGC: %u\n", argc);

    int i;
    for (i = 0; i < argc; i++)
        printf("ARG: %u - %s\n", i, argv[i]);

    return 0;
}

Upvotes: 0

Related Questions