#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// fiter string to the first |
char* filterstringfirst(char* command, int i){
char *tok = command;
int x = 0;
while ((tok = strtok(tok, "|")) != NULL && x <= i)
{
if( x == i){
return tok;
}
x++;
printf(" === Parsed: --%s-- ===\n", tok);
tok = NULL;
}
return tok;
}
int main () {
char command[] = "ls -a | sort -h | grep h | wc -l";
char command2[] = "ls -a | sort -h | grep h | wc -l";
char* temp = command;
char* x = filterstringfirst(temp, 0);
printf("%s\n",x);
char* temp2 = command;
char* x2 = filterstringfirst(temp2, 1);
printf("%s\n",x2);
temp = command;
return 0;
}
I have this function I made which is supposed to just return part of a string. The original string should be similar to "ls -l | grep temp | sort".
The idea was that it would be called with the string and a number, and return that segment. Eg. 0 -> "ls -l"
Now this works the first time I call it, but calling it again seems to break and end in a segfault.
char command[] = "ls -a | sort -h | grep h | wc -l";
char* temp = command;
char* x = filterstringfirst(temp, 0);
printf("%s\n",x);
char* temp2 = command;
char* x2 = filterstringfirst(temp2, 1);
printf("%s\n",x2);`
This was my testing code
And the output:
ls -a
=== Parsed: --ls -a -- ===
[1] 1126 segmentation fault ./templ
➜ Current gcc -o templ templ.c
➜ Current ./templ ls -a
=== Parsed: --ls -a -- === [1]
1136 segmentation fault ./templ
Edit: Updated to have main too (based on comments)