summaryrefslogtreecommitdiffstats
path: root/strpbrk.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 /strpbrk.c
downloadsandbox-b49853cb048db3bd893080ff8b81e6911add0338.tar.gz
sandbox-b49853cb048db3bd893080ff8b81e6911add0338.tar.bz2
sandbox-b49853cb048db3bd893080ff8b81e6911add0338.zip
Initial commit
Diffstat (limited to 'strpbrk.c')
-rw-r--r--strpbrk.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/strpbrk.c b/strpbrk.c
new file mode 100644
index 0000000..4d614d7
--- /dev/null
+++ b/strpbrk.c
@@ -0,0 +1,81 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+char **redir_chop(const char *line)
+{
+ if (!line)
+ return NULL;
+
+ const char *redir_delim = "><";
+
+ char *pipe_redir = strpbrk(line, redir_delim);
+ if (pipe_redir == NULL)
+ return NULL;
+
+ size_t redir_delta = pipe_redir - line;
+ char *former = (char *) malloc(sizeof(char) * 100);
+ char *latter = (char *) malloc(sizeof(char) * 100);
+
+ strncpy(former, line, redir_delta);
+ former[redir_delta] ='\0';
+
+ /* delete spaces at the end of former string */
+ int i = strlen(former) - 1;
+ while (former[i] == ' ')
+ former[i--] = '\0';
+
+ /* skip spaces in latter string and then copy over */
+ int j = redir_delta;
+ while (line[++j] == ' ')
+ ;
+ strncpy(latter, line+j, (line + strlen(line)) - pipe_redir);
+
+ char **ret = (char **) malloc(sizeof(char *) * 2);
+ ret[0] = (char *) former;
+ ret[1] = (char *) latter;
+
+ return ret;
+}
+
+int main(int argc, char *argv[])
+{
+ const char *line = "ls -1 -al > filename.txt";
+
+#if 0
+ const char *redir_delim = "><";
+
+ char *pipe_redir = strpbrk(line, redir_delim);
+ size_t redir_delta = pipe_redir - line;
+ char *former = (char *) malloc(sizeof(char) * 100);
+ char *latter = (char *) malloc(sizeof(char) * 100);
+
+ strncpy(former, line, redir_delta);
+ former[redir_delta] ='\0';
+
+ /* delete spaces at the end of former string */
+ int i = strlen(former) - 1;
+ while (former[i] == ' ')
+ former[i--] = '\0';
+
+ /* skip spaces in latter string and then copy over */
+ int j = redir_delta;
+ while (line[++j] == ' ')
+ ;
+ strncpy(latter, line+j, (line + strlen(line)) - pipe_redir);
+
+ printf("former string: \"%s\"\n", former);
+ printf("latter string: \"%s\"\n", latter);
+
+ free(former);
+ free(latter);
+#endif
+
+ char **chopped = redir_chop(line);
+
+ printf("former string: \"%s\"\n", chopped[0]);
+ printf("latter string: \"%s\"\n", chopped[1]);
+
+ return 0;
+}
+