From 5b401722520386cfe3dd920fd59cc0d47e9f9503 Mon Sep 17 00:00:00 2001 From: Kyle K Date: Fri, 11 Feb 2011 16:37:00 -0600 Subject: cleanups --- strpbrk.c | 57 ++++++++++++++++----------------------------------------- 1 file changed, 16 insertions(+), 41 deletions(-) (limited to 'strpbrk.c') diff --git a/strpbrk.c b/strpbrk.c index 4d614d7..a292a6e 100644 --- a/strpbrk.c +++ b/strpbrk.c @@ -2,20 +2,27 @@ #include #include -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; } -- cgit v1.2.3