From b49853cb048db3bd893080ff8b81e6911add0338 Mon Sep 17 00:00:00 2001 From: Kyle K Date: Sat, 5 Feb 2011 05:19:27 -0600 Subject: Initial commit --- strsep.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 strsep.c (limited to 'strsep.c') diff --git a/strsep.c b/strsep.c new file mode 100644 index 0000000..882a1e0 --- /dev/null +++ b/strsep.c @@ -0,0 +1,39 @@ +#include +#include +#include + +int main(int argc, char **argv) +{ + char *line = "ls -1 | wc -l | less"; + + size_t sz = strlen(line) + 1; + char *linecopy = malloc(sizeof(char) * sz); + strcpy(linecopy, line); + linecopy[sz-1] = '\0'; + + char *args[10]; + char **lineptr = &linecopy; + + char *tok = NULL; + const char arg_delim = ' '; + int i = 0; + + do + { + tok = strsep(lineptr, &arg_delim); + args[i] = tok; + i++; + } + while ((tok != NULL) && (i < 9)); + + if (i == 10) + args[9] = NULL; + + /* print out the piped string */ + int j; + for (j = 0; j < i; j++) + printf("string argument %d: \"%s\"\n", j, args[j]); + + return 0; +} + -- cgit v1.2.3