Skip to content

Instantly share code, notes, and snippets.

@alsamitech
Last active July 13, 2021 06:31
Show Gist options
  • Select an option

  • Save alsamitech/bd1c1859bdaf6b03459a6f0e23b6a76a to your computer and use it in GitHub Desktop.

Select an option

Save alsamitech/bd1c1859bdaf6b03459a6f0e23b6a76a to your computer and use it in GitHub Desktop.
atokl that isn't security and error checking hell
char** atokl(const char* const InC, const char* const delim, long unsigned int* len){
// eight as that provides room for small splitting operations
long unsigned int capacity=8;
char** tok=(char**)malloc(capacity*sizeof(char**));
if(!tok)return 0x0;
tok[0]=strtok(strdup(InC), delim);
{
long unsigned int i=1;
while(tok[i-1]!=NULL){
if(i+2>=capacity){
capacity*=2;
tok=(char**)realloc(tok, capacity*sizeof(char**));
if(!tok){
// something failed
free(tok[0]);
free(tok);
return 0x0;
}
}
tok[i++]=strtok(NULL, delim);
}
*len=i;
/*EBRACE*/}
return tok;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment