summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2011-02-11 16:37:00 -0600
committerKamil Kaminski <kamilkss@gmail.com>2011-02-11 16:37:00 -0600
commit5b401722520386cfe3dd920fd59cc0d47e9f9503 (patch)
treecdc37d23437ec5d023e8e672208de625ad9ae15c
parentb49853cb048db3bd893080ff8b81e6911add0338 (diff)
downloadsandbox-5b401722520386cfe3dd920fd59cc0d47e9f9503.tar.gz
sandbox-5b401722520386cfe3dd920fd59cc0d47e9f9503.tar.bz2
sandbox-5b401722520386cfe3dd920fd59cc0d47e9f9503.zip
cleanups
-rw-r--r--Makefile2
-rw-r--r--depipe_strings.c3
-rw-r--r--pipe.c40
-rw-r--r--strpbrk.c57
4 files changed, 59 insertions, 43 deletions
diff --git a/Makefile b/Makefile
index 1b5de9a..273a07b 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-BINS = ascii class depipe_strings dup fpipe realloc strpbrk strsep tokenizer
+BINS = ascii class depipe_strings dup fpipe pipe realloc strpbrk strsep tokenizer
CC = gcc
DBGFLAGS = -g -O0
ifdef DEBUG
diff --git a/depipe_strings.c b/depipe_strings.c
index 597ade8..61115d7 100644
--- a/depipe_strings.c
+++ b/depipe_strings.c
@@ -15,7 +15,7 @@ char **depipe_string(const char *, int *);
int main(int argc, char **argv)
{
int count;
- char *line = " ls -1 | wc -l | cut -n1 | less | rtfm| bro ";
+ char *line = " ls -1 | wc -l | cut -b 6-7";
char **depiped = depipe_string(line, &count);
int j;
@@ -27,6 +27,7 @@ int main(int argc, char **argv)
return 0;
}
+/* chops and counts strings with arguments that are separated by pipes */
char **depipe_string(const char *line, int *count)
{
char *last = (char *) line;
diff --git a/pipe.c b/pipe.c
new file mode 100644
index 0000000..a5c86cc
--- /dev/null
+++ b/pipe.c
@@ -0,0 +1,40 @@
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define MAX_LINE 80
+#define PIPE_STDIN 0
+#define PIPE_STDOUT 1
+
+int main(int argc, char **argv)
+{
+ const char *string = {"A sample message."};
+ int ret, myPipe[2];
+ char buffer[MAX_LINE+1];
+
+ /* create the pipe */
+ ret = pipe(myPipe);
+
+ if (ret == 0)
+ {
+ /* write message into the pipe */
+ write(myPipe[PIPE_STDOUT], string, strlen(string));
+
+ /* read the message from the pipe */
+ ret = read(myPipe[PIPE_STDIN], buffer, MAX_LINE);
+
+ /* null terminate the string */
+ buffer[ret] = 0;
+
+ printf("%s\n", buffer);
+ }
+ else
+ {
+ perror("pipe");
+ exit(EXIT_FAILURE);
+ }
+
+ return 0;
+}
+
diff --git a/strpbrk.c b/strpbrk.c
index 4d614d7..a292a6e 100644
--- a/strpbrk.c
+++ b/strpbrk.c
@@ -2,20 +2,27 @@
#include <stdlib.h>
#include <string.h>
-char **redir_chop(const char *line)
+char **redir_chop(const char *, const char);
+
+/* chops a line into 2 string that are seperated by redir_delim */
+char **redir_chop(const char *line, const char redir_delim)
{
if (!line)
return NULL;
- const char *redir_delim = "><";
+ const char *delim = &redir_delim;
- char *pipe_redir = strpbrk(line, redir_delim);
+ char *pipe_redir = strpbrk(line, 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);
+ char **ret = (char **) malloc(sizeof(char *) * 2);
+ ret[0] = (char *) malloc(sizeof(char) * 256 * 2);
+ ret[1] = ret[0] + 256;
+
+ char *former = ret[0];
+ char *latter = ret[1];
strncpy(former, line, redir_delta);
former[redir_delta] ='\0';
@@ -31,50 +38,18 @@ char **redir_chop(const char *line)
;
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);
+ char **chopped = redir_chop(line, '>');
printf("former string: \"%s\"\n", chopped[0]);
- printf("latter string: \"%s\"\n", chopped[1]);
+ printf("latter string: \"%s\"\n", chopped[1]);
+
+ free(chopped);
return 0;
}