c - Forking with command line arguments -
i building linux shell, , current headache passing command line arguments forked/exec'ed programs , system functions.
currently input tokenized on spaces , new lines, in global variable char * parsed_arguments. example, input dir /usa/folderb tokenized as:
parsed_arguments[0] = dir parsed_arguments[1] = /usa/folderb
parsed_arguments tokenizes perfectly; issue wish take subset of parsed_arguments, excludes command/ first argument/path executable run in shell, , store them in new array, called passed_arguments.
so in previous example dir /usa/folderb
parsed_arguments[0] = dir parsed_arguments[1] = /usa/folderb passed_arguments[0] = /usa/folderb passed_arguments[1] = etc....
currently not having luck i'm hoping me this. here code of have working far:
how i'm trying copy arguments:
void command_line() { int = 1; for(i;parsed_arguments[i]!=null;i++) printf("%s",parsed_arguments[i]); }
function read commands:
void readcommand(char newcommand[]){ printf("readcommand: %s\n", newcommand); //parsed_arguments = (char* malloc(max_args)); // strcpy(newcommand,inputstring); parsed = parsed_arguments; *parsed++ = strtok(newcommand,separators); // tokenize input while ((*parsed++ = strtok(null,separators))) //printf("test1\n"); // last entry null //passed_arguments=parsed_arguments[1]; if(parsed[0]){ char *initial_command =parsed[0]; parsed= parsed_arguments; while (*parsed) fprintf(stdout,"%s\n ",*parsed++); // free (parsed); // free(parsed_arguments); }//end of if command_line(); }//end of readcommand
forking function:
else if(strstr(parsed_arguments[0],"./")!=null) { int pid; switch(pid=fork()){ case -1: printf("fork error, aborting\n"); abort(); case 0: execv(parsed_arguments[0],passed_arguments); } }
this shell outputs. first time run it, outputs close want, every subsequent call breaks program. in addition, each additional call appends parsed arguments output.
this original shell produces. again it's close want, not quite. want omit command (i.e. "./testline").
your testline
program sensible 1 have in toolbox; have similar program call al
(for argument list) prints arguments, 1 per line. doesn't print argv[0]
though (i know called al
). can arrange testline
skip argv[0]
too. note unix convention argv[0]
name of program; should not try change (you'll fighting against entire system).
#include <stdio.h> int main(int argc, char **argv) { while (*++argv != 0) puts(*argv); return 0; }
your function command_line()
reasonable except relies unnecessarily on global variables. think of global variables nasty smell (h2s, example); avoid them when can. should more like:
void command_line(char *argv[]) { (int = 1; argv[i] != null; i++) printf("<<%s>>\n", argv[i]); }
if you're stuck c89, you'll need declare int i;
outside loop , use for (i = 1; ...)
in loop control. note printing here separates each argument on line on own, , encloses in marker characters (<<
, >>
— change suit whims , prejudices). fine skip newline in loop (maybe use space instead), , add newline after loop (putchar('\n');
). makes better, more general purpose debug routine. (when write 'dump' function, use void dump_argv(file *fp, const char *tag, char *argv[])
can print standard error or standard output, , include tag string identify dump written.)
unfortunately, given fragmentary nature of readcommand()
function, not possible coherently critique it. commented out lines enough elicit concern, without actual code you're running, can't guess problems or mistakes you're making. shown, equivalent to:
void readcommand(char newcommand[]) { printf("readcommand: %s\n", newcommand); parsed = parsed_arguments; *parsed++ = strtok(newcommand, separators); while ((*parsed++ = strtok(null, separators)) != 0) { if (parsed[0]) { char *initial_command = parsed[0]; parsed = parsed_arguments; while (*parsed) fprintf(stdout, "%s\n ", *parsed++); } } command_line(); }
the variables parsed
, parsed_arguments
both globals , variable initial_command
set not used (aka 'pointless'). if (parsed[0])
test not safe; incremented pointer in previous line, pointing @ indeterminate memory.
superficially, judging screen shots, not resetting parsed_arguments[]
and/or passed_arguments[]
arrays correctly on second use; might index not being set zero. without knowing how data allocated, hard know might doing wrong.
i recommend closing question, going system , producing minimal sscce. should under 100 lines; need not execv()
(or fork()
), should print commands executed using variant of command_line()
function above. if answer prevents deleting (closing) question, edit sscce code, , notify me comment answer see you've done that.
Comments
Post a Comment