c - I'm at a loss at finding the segfault in my program -
c - I'm at a loss at finding the segfault in my program -
so i've been spending hours trying find source of segmentation fault 11 error, , hoping if guys can help me out.
so purpose of programme find redirection in input string, , homecoming redircode struct gives direction of redirection, filename, , argswithoutfile.
example input:
"ls -a > test.txt"
returns redir code with:
argswithoutfile = "ls -a" filename = "test.txt" code = 2 //stdout
i'm pretty sure seg fault coming trying substrings. loop seems fine, because when comment out substring stuff @ end doesn't give segfault. seems fine me except substring. substring want this:
char *input = "ls -a > test.txt' char *desiredsubstring = "ls -a "
here code in entirety:
#include <stdio.h> #include <string.h> #define buffer 1024 struct redircode findredirects(char *input); struct redircode { /* code: * 0 = none * 1 = stdin * 2 = stdout */ int code; char *argswithoutfile; char *filename; }; int main(){ const char *delims = "<>"; struct redircode temp; char line[buffer]; printf("input: "); fgets(line, 1024, stdin); temp = findredirects(line); printf("temp:\n"); printf("temp.code = %d\n", temp.code); printf("temp.filename = %s\n", temp.filename); printf("temp.argswithoutfile = %s\n", temp.argswithoutfile); } /* looks '>', '<' in string. * destroy string *input * returns redircode struct with: * 1. filename - name of file * wants redirected/stdin * 2. code - direction of redirect * 3. args - arguments w/o filename * */ struct redircode findredirects(char *input) { const char *delims = "<>"; struct redircode redirtoreturn; //do initial search delimeters //before strtok destroys it. o(n) time. int redirectoperatorreached = 0; int count = 0; int i; (i = 0; input[i] != 0; i++){ if (input[i] == '<'){ redirtoreturn.code = 1; redirectoperatorreached = 1; } else if (input[i] == '>'){ redirtoreturn.code = 2; redirectoperatorreached = 1; } else { redirtoreturn.code = 0; } if (redirectoperatorreached != 1){ count++; } } printf("sizeof(input) = %lu\n", sizeof(input)); printf("count = %d\n", count); strncpy(redirtoreturn.argswithoutfile, input, count); printf("input = %s\n", input); redirtoreturn.argswithoutfile[count] = '\0'; printf("argsw/ofile = %s\n", redirtoreturn.argswithoutfile); homecoming redirtoreturn; }
and here terminal logs
macbook-air:practice keithy$ cc strtokonlyonce.c macbook-air:practice keithy$ ./a.out input: hi sizeof(input) = 8 count = 3 segmentation fault: 11 macbook-air:practice keithy$ cc strtokonlyonce.c macbook-air:practice keithy$ ./a.out input: ls -a > test.txt sizeof(input) = 8 count = 6 segmentation fault: 11 macbook-air:practice keithy$
edit: got work! had malloc strings in redircode. here working code:
#include <stdio.h> #include <string.h> #include <stdlib.h> #define buffer 1024 struct redircode findredirects(char *input); struct redircode { /* code: * 0 = none * 1 = stdin * 2 = stdout */ int code; char *argswithoutfile; char *filename; }; int main(){ const char *delims = "<>"; struct redircode temp; char line[buffer]; printf("input: "); fgets(line, 1024, stdin); temp = findredirects(line); printf("temp.code = %d\n", temp.code); printf("temp.filename = %s\n", temp.filename); printf("temp.argswithoutfile = %s\n", temp.argswithoutfile); } /* looks '>', '<' in string. * destroy string *input * returns redircode struct with: * 1. filename - name of file * wants redirected/stdin * 2. code - direction of redirect * 3. args - arguments w/o filename * */ struct redircode findredirects(char *input) { const char *delims = "<>"; struct redircode *redirtoreturn = malloc(sizeof(struct redircode)); //do initial search delimeters //before strtok destroys it. o(n) time. int redirectoperatorreached = 0; int count = 0; int i; (i = 0; input[i] != 0; i++){ if (input[i] == '<'){ redirtoreturn->code = 1; redirectoperatorreached = 1; input[i] = ' '; } else if (input[i] == '>'){ redirtoreturn->code = 2; redirectoperatorreached = 1; input[i] = ' '; } if (redirectoperatorreached != 1){ count++; } } int lengthofinput = strlen(input); int sizeofmalloc = (lengthofinput+1)*sizeof(char); redirtoreturn->argswithoutfile = (char *) malloc(sizeofmalloc); redirtoreturn->filename = (char *) malloc(sizeofmalloc); strncpy(redirtoreturn->argswithoutfile, input, count); redirtoreturn->argswithoutfile[count] = '\0'; strncpy(redirtoreturn->filename, input + count, lengthofinput - count); homecoming *redirtoreturn; } /*output *./a.out *input: ls -a > test.txt *temp.code = 2 *temp.filename = test.txt *temp.argswithoutfile = ls -a */
struct redircode { /* code: * 0 = none * 1 = stdin * 2 = stdout */ int code; char *argswithoutfile; char *filename; };
you need allocate memory construction fellow member named char *argswithoutfile;
, char *filename;
before using it.
you can this
struct redircode *redirtoreturn = malloc(sizeof(struct redircode )); redirtoreturn->argswithoutfile = malloc(1024); redirtoreturn->filename = malloc(1024);
c
Comments
Post a Comment