Skip to content
Snippets Groups Projects
Select Git revision
  • 31965a48a64436ce2be47e7989201cd8f135b604
  • master default
2 results

utils.c

Blame
  • Forked from sce213ta / mysh-1
    10 commits behind the upstream repository.
    utils.c 584 B
    #include "utils.h"
    
    #include <stdlib.h>
    #include <string.h>
    
    void mysh_parse_command(const char* command,
                            int *argc, char*** argv)
    {
      const int kMaxArgc = 512;
      *argv = (char**)malloc(kMaxArgc);
    
      char buf[4096];
      strcpy(buf, command);
    
      char *tok = strtok(buf, " \n\t");
    
      int ti = 0;
    
      while (tok != NULL) {
        (*argv)[ti] = (char*)malloc(strlen(tok));
        strcpy((*argv)[ti], tok);
    
        ++ti;
    
        tok = strtok(NULL, " \n\t");
      }
    
      *argc = ti;
    
      if (*argc == 0) {
        *argc = 1;
        (*argv)[0] = (char*)malloc(1);
        (*argv)[0][0] = '\0';
      }
    }