c - Issue with array manipulation and memset -
i have c program reads characters stdin (using getchar) until blank line read, , null element added , characters read before blank line sent execve. then, more characters read , added same array (which i'm trying reset after first call execve) until blank line read, , second section should executed. having trouble using memset reset array after each call execve , working args[i] = null can pass array execve.
e.g. usage:
echo
hello
after seeing blank line executes "echo hello"
cat
goodbye
*should cat file "goodbye" when first 2 arguments "echo" , "null"
int main() { int pid, status, c, size = 10, length = 0, i, blankflag = 0, alrdyexec = 0; char argument[1000]; char **args; char *token; args = (char**)malloc(size); extern int process(char **args); fflush(stdout); while ((c = getchar()) != eof) { if (length == size) { size += 10; args = realloc(args, size); } if (c != '\n') { argument[length++] = c; blankflag = 0; alrdyexec = 0; } else if ((c == '\n') && (blankflag == 0)) { blankflag = 1; argument[length++] = c; } else if ((c == '\n') && (blankflag == 1) && (alrdyexec == 0)) { alrdyexec = 1; = 0; memset(args, 0, sizeof(args)); token = strtok(argument, "\n"); while (token != null) { args[i] = token; token = strtok(null, "\n"); i++; } args[i] = null; // here executes function runs execve } } return(0); }
Comments
Post a Comment