summaryrefslogtreecommitdiffstats
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
downloadsandbox-b49853cb048db3bd893080ff8b81e6911add0338.tar.gz
sandbox-b49853cb048db3bd893080ff8b81e6911add0338.tar.bz2
sandbox-b49853cb048db3bd893080ff8b81e6911add0338.zip
Initial commit
-rw-r--r--Makefile20
-rw-r--r--ascii.c30
-rw-r--r--class.c53
-rw-r--r--depipe_strings.c89
-rw-r--r--dup.c61
-rw-r--r--fpipe.c59
-rw-r--r--realloc.c77
-rw-r--r--strpbrk.c81
-rw-r--r--strsep.c39
-rw-r--r--tokenizer.c33
10 files changed, 542 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..1b5de9a
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,20 @@
+BINS = ascii class depipe_strings dup fpipe realloc strpbrk strsep tokenizer
+CC = gcc
+DBGFLAGS = -g -O0
+ifdef DEBUG
+ CFLAGS = $(DBGFLAGS) -D DEBUG -std=gnu99 -Wall -pedantic
+else
+ CFLAGS = -O2 -std=gnu99 -Wall -pedantic
+endif
+LDFLAGS = -lm
+
+all: $(BINS)
+
+$(BINS): %: %.c
+ $(CC) $(CFLAGS) $^ -o $@
+
+
+.PHONY: clean
+
+clean:
+ rm -f ./$(BINS)
diff --git a/ascii.c b/ascii.c
new file mode 100644
index 0000000..eb24ba5
--- /dev/null
+++ b/ascii.c
@@ -0,0 +1,30 @@
+#include <stdio.h>
+
+int main(int argc, char *argv[])
+{
+ printf("\tASCII Table\n");
+
+ int i, j;
+
+ for (j = 0, i = 1; i < 128 + 1; i++)
+ {
+ if (!(j < 32) && j != 127)
+ printf("%3d %c", j, j);
+ else
+ printf("%3d ", j);
+
+ j += 32;
+
+ if (i % 4 == 0)
+ {
+ j = (i / 4);
+ printf("\n");
+ }
+ else
+ printf("\t");
+ }
+ puts("");
+
+ return 0;
+}
+
diff --git a/class.c b/class.c
new file mode 100644
index 0000000..dda044a
--- /dev/null
+++ b/class.c
@@ -0,0 +1,53 @@
+#include <stdio.h>
+
+void printS(char *str)
+{
+ puts(str);
+}
+
+void printF(float *f)
+{
+ printf("%f\n", f);
+}
+
+struct GLFrame
+{
+ int x, y, z;
+ void (*func1)(char *);
+ void (*func2)(struct GLFrame *, int *, int *, int *);
+};
+
+void modPos(struct GLFrame *frame, int *x, int *y, int *z)
+{
+ frame->x = *x;
+ frame->y = *y;
+ frame->z = *z;
+}
+
+struct GLFrame frame = {
+ .x = 5,
+ .y = 6,
+ .z = 7,
+ .func1 = printS,
+ .func2 = modPos
+};
+
+int main(void)
+{
+#if 0
+ float x = 1.0f;
+ printf("x in hex: 0x%016x\n", x);
+#endif
+
+ int x, y, z;
+ x = y = z = 3;
+ /* i want to modify position vector, xyz */
+ frame.func2(&frame, &x, &y, &z);
+
+ printf("x is: %d\ny is: %d\nz is: %d\n", frame.x, frame.y, frame.z);
+
+ puts("Hello");
+
+ return 0;
+}
+
diff --git a/depipe_strings.c b/depipe_strings.c
new file mode 100644
index 0000000..597ade8
--- /dev/null
+++ b/depipe_strings.c
@@ -0,0 +1,89 @@
+/* depipe_strings.c
+ *
+ * Depipe apps into strings
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <wait.h>
+
+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 **depiped = depipe_string(line, &count);
+
+ int j;
+ for (j = 0; j < count; j++)
+ printf("app %d: %s\n", j+1, depiped[j]);
+
+ free(depiped);
+
+ return 0;
+}
+
+char **depipe_string(const char *line, int *count)
+{
+ char *last = (char *) line;
+ char *curr = NULL;
+
+ int i;
+ int num_apps = 10;
+ int fname = 100;
+ char **piped_apps = (char **) malloc(sizeof(char *) * num_apps);
+ piped_apps[0] = malloc(sizeof(char) * num_apps * fname);
+
+ /* construct m-dimensional array from 1 block of memory */
+ for (i = 1; i < num_apps; i++)
+ piped_apps[i] = piped_apps[0] + (i * fname);
+
+ *count = 0;
+ i = 0;
+ int j = 0;
+ size_t line_delta = 0;
+
+ while ( (curr = strchr(last, '|')) )
+ {
+ /* skip white space */
+ while (last[0] == ' ')
+ last++;
+
+ line_delta = curr - last;
+ strncpy(piped_apps[i], last, line_delta);
+ piped_apps[i][line_delta] = '\0';
+
+ /* delete whitespace at the tail */
+ j = line_delta - 1;
+ while (piped_apps[i][j] == ' ')
+ piped_apps[i][j--] = '\0';
+
+ last = curr + 1;
+ i++;
+ }
+
+ /* copy over last item */
+
+ /* skip white space */
+ while (last[0] == ' ')
+ last++;
+
+ line_delta = line + strlen(line) - (last-1);
+ strncpy(piped_apps[i], last, line_delta);
+ piped_apps[i][line_delta] = '\0';
+
+ /* delete whitespace at the tail */
+ j = strlen(piped_apps[i]) - 1;
+ while (piped_apps[i][j] == ' ')
+ piped_apps[i][j--] = '\0';
+
+ i++;
+
+ *count = i;
+ return piped_apps;
+}
+
diff --git a/dup.c b/dup.c
new file mode 100644
index 0000000..e70b804
--- /dev/null
+++ b/dup.c
@@ -0,0 +1,61 @@
+/* dup.c
+ *
+ * Child piping input to parent
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <wait.h>
+
+extern char **environ;
+
+int main(int argc, char **argv)
+{
+ pid_t pid;
+ int stat_loc;
+
+ int pfds[2];
+ int ret;
+
+ if (pipe(pfds) == 0)
+ {
+ pid = fork();
+ if (pid == -1)
+ {
+ perror("fork");
+ exit(EXIT_FAILURE);
+ }
+
+ if (!pid)
+ {
+ close(1); /* break the link to stdout */
+ dup2(pfds[1], 1); /* redirect stdout to pfds[1] */
+ close(pfds[0]); /* child will not be reading anything from out pipe */
+
+ execlp("ls", "ls", "-1", NULL);
+ }
+ else
+ {
+ close(0); /* break the link to stdin */
+ dup2(pfds[0], 0); /* redirect stdout to pfds[0] */
+ close(pfds[1]); /* parent will not be writing anything into our pipe */
+
+ ret = waitpid(pid, &stat_loc, WCONTINUED);
+ execlp("wc", "wc", "-l", NULL);
+ }
+ }
+ else
+ {
+ perror("pipe");
+ exit(EXIT_FAILURE);
+ }
+
+ close(pfds[0]);
+ close(pfds[1]);
+
+ return 0;
+}
+
diff --git a/fpipe.c b/fpipe.c
new file mode 100644
index 0000000..4ec9866
--- /dev/null
+++ b/fpipe.c
@@ -0,0 +1,59 @@
+/* fpipe.c
+ *
+ * Parent writing to child
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <wait.h>
+#define MAX_LINE 80
+
+int main(int argc, char **argv)
+{
+ pid_t pid;
+ int stat_loc;
+
+ int pfds[2];
+ int ret;
+
+ char buf[MAX_LINE+1];
+ const char *testbuf = { "I'm a parent and I'm writing to you" };
+
+ if (pipe(pfds) == 0)
+ {
+ pid = fork();
+ if (pid == -1)
+ {
+ perror("fork");
+ exit(EXIT_FAILURE);
+ }
+
+ if (!pid)
+ {
+ close(pfds[1]); /* child will not be writing anything */
+ ret = read(pfds[0], buf, MAX_LINE);
+ buf[ret] = 0;
+ printf("Child read: %s\n", buf);
+ }
+ else
+ {
+ close(pfds[0]); /* parent will not be reading anything */
+ ret = write(pfds[1], testbuf, strlen(testbuf));
+ ret = waitpid(pid, &stat_loc, WCONTINUED);
+ }
+ }
+ else
+ {
+ perror("pipe");
+ exit(EXIT_FAILURE);
+ }
+
+ close(pfds[0]);
+ close(pfds[1]);
+
+ return 0;
+}
+
diff --git a/realloc.c b/realloc.c
new file mode 100644
index 0000000..e9aec19
--- /dev/null
+++ b/realloc.c
@@ -0,0 +1,77 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+typedef struct
+{
+ int length;
+ char *line;
+} sNode;
+
+struct String
+{
+ int index;
+ int size;
+ sNode *arr;
+};
+
+struct String *string_new(char *str);
+void string_append(struct String *a, char *b);
+void string_resize(struct String *s);
+
+struct String *string_new(char *str)
+{
+ struct String *ret = (struct String *) calloc(1, sizeof(struct String));
+ ret->arr = (sNode *) calloc(1, (sizeof(sNode) * 200));
+ ret->size = 200;
+ string_append(ret, str);
+
+ return ret;
+}
+
+void string_append(struct String *a, char *b)
+{
+ if ((a->size-1) == a->index)
+ string_resize(a);
+
+ a->arr[a->index].line = b;
+ a->arr[a->index].length = strlen(b);
+
+ a->index++;
+}
+
+void string_resize(struct String *s)
+{
+ /* quadruple the size */
+ int newSize = s->size * 4;
+ printf("debug: resizing to new size of %d\n", newSize);
+
+ s->size = newSize;
+ sNode *newarr = (sNode *) realloc(s->arr, (sizeof(sNode) * newSize));
+ if (!newarr)
+ {
+ perror("realloc");
+ exit(-1);
+ }
+
+ /* free old array */
+ free(s->arr);
+ s->arr = newarr;
+
+}
+
+int main(int argc, char **argv)
+{
+ if (argc < 2)
+ {
+ fprintf(stderr,"Usage: app n\n");
+ exit(1);
+ }
+
+ struct String *str = string_new("");
+
+ int i;
+ for (i = 0; i < atoi(argv[1]); i++)
+ string_append(str, "anything that can go wrong, will");
+}
+
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;
+}
+
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;
+}
+
diff --git a/tokenizer.c b/tokenizer.c
new file mode 100644
index 0000000..461383a
--- /dev/null
+++ b/tokenizer.c
@@ -0,0 +1,33 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int main(int argc, char *argv[])
+{
+ char *str1, *str2, *token, *subtoken;
+ char *saveptr1, *saveptr2;
+ int j;
+
+ if (argc != 4) {
+ fprintf(stderr, "Usage: %s string delim subdelim\n",
+ argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
+ token = strtok_r(str1, argv[2], &saveptr1);
+ if (token == NULL)
+ break;
+ printf("%d: %s\n", j, token);
+
+ for (str2 = token; ; str2 = NULL) {
+ subtoken = strtok_r(str2, argv[3], &saveptr2);
+ if (subtoken == NULL)
+ break;
+ printf(" --> %s\n", subtoken);
+ }
+ }
+
+ exit(EXIT_SUCCESS);
+}
+