summaryrefslogtreecommitdiffstats
path: root/strsep.c
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2011-02-05 05:19:27 -0600
committerKamil Kaminski <kamilkss@gmail.com>2011-02-05 05:19:27 -0600
commitb49853cb048db3bd893080ff8b81e6911add0338 (patch)
treefb85fa36fc929e6605f3bb4c73d932a668c6c459 /strsep.c
downloadsandbox-b49853cb048db3bd893080ff8b81e6911add0338.tar.gz
sandbox-b49853cb048db3bd893080ff8b81e6911add0338.tar.bz2
sandbox-b49853cb048db3bd893080ff8b81e6911add0338.zip
Initial commit
Diffstat (limited to 'strsep.c')
-rw-r--r--strsep.c39
1 files changed, 39 insertions, 0 deletions
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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+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;
+}
+