#include <stdio.h>
#include <string.h>
char * mystrtok (char * s, char * delim, size_t * olds)
{
char * token;
char * temp = (char *)*olds;
//char * olds = NULL;
if (s == NULL)
s = temp;
/* Scan leading delimiters. */
s += strspn (s, delim);
if (*s == '/0') {
temp = s;
return NULL;
}
/* Find the end of the token. */
token = s;
s = strpbrk (token, delim);
if (s == NULL)
/* This token finishes the string. */
temp = (char *) __rawmemchr (token, '/0');
else {
/* Terminate the token and make OLDS point past it. */
*s = '/0';
temp = s + 1;
}
* olds = (size_t)temp;
return token;
}
int main(void)
{
char a[] = "123 454 345";
char * arg[10];
char * t;
size_t olds = 0;
int n, i;
t = mystrtok(a, " /t", & olds);
i = 0;
while (1) {
if (t == NULL)
break;
arg[i] = strdup(t);
printf("%s/n", arg[i]);
i++;
t = mystrtok(NULL, " /t", & olds);
}
return (0);
}
#include <string.h>
char * mystrtok (char * s, char * delim, size_t * olds)
{
char * token;
char * temp = (char *)*olds;
//char * olds = NULL;
if (s == NULL)
s = temp;
/* Scan leading delimiters. */
s += strspn (s, delim);
if (*s == '/0') {
temp = s;
return NULL;
}
/* Find the end of the token. */
token = s;
s = strpbrk (token, delim);
if (s == NULL)
/* This token finishes the string. */
temp = (char *) __rawmemchr (token, '/0');
else {
/* Terminate the token and make OLDS point past it. */
*s = '/0';
temp = s + 1;
}
* olds = (size_t)temp;
return token;
}
int main(void)
{
char a[] = "123 454 345";
char * arg[10];
char * t;
size_t olds = 0;
int n, i;
t = mystrtok(a, " /t", & olds);
i = 0;
while (1) {
if (t == NULL)
break;
arg[i] = strdup(t);
printf("%s/n", arg[i]);
i++;
t = mystrtok(NULL, " /t", & olds);
}
return (0);
}