Skip to content

Instantly share code, notes, and snippets.

@alsamitech
Created April 11, 2021 18:37
Show Gist options
  • Select an option

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

Select an option

Save alsamitech/83ad8591d50edca04020c9432635d358 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#ifndef __unix__
#error Please use unix or replace the unix syscalls with functions/syscalls available on your system. After you have done that, remove this error
#endif
#include <unistd.h>
#include <sys/types.h>
char** atokl(char* InC, char* delim, long unsigned int* len){
char** tok=(char**)malloc(2*sizeof(char*));
//printf("%p\n", tok);
tok[0]=strtok(strdup(InC), delim);
{
int i=1;
while(tok[i-1]!=NULL){
tok=(char**)realloc(tok, (i+1)*sizeof(char*));
tok[i]=strtok(NULL, delim);
//printf("%p\n", tok[i]);
i++;
}
*len=i;
/*EBRACE*/}
return tok;
}
typedef struct _arr{
char* arr;
size_t len;
}arr_t;
typedef char** strlist;
void printstrlist(strlist s){
for(size_t i=0;s[i]!=0x0;i++){
write(1, s[i], strlen(s[i]));
}
}
int main(void){
arr_t a;
a.arr=(char*)atokl("word1,word2,word3", ",", &a.len);
printstrlist((char**)a.arr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment