linux - C-Passing arguments for execution of a program in new Xterm window -


problem statement:-

how pass arguments program execution in new xterm/gnome window calling through execlp.

a little elaborate explanation:-(oxymoron eh?)

consider following program take string argument , display it

//output.c #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h>  int main(int argc, char *argv[]) {     if(argc<2)         {               printf("insufficient parameters\n");               exit(1);         }     printf("%s",argv[1]);     sleep(10);     return 0; 

}

and program,client.cwhich, during course of execution required call output.c , make display in new xterm/gnome-terminal window.

//client.c int main() {     char buf[25]="test string";//as argument program called     int pid_child=fork();     if(pid_child==-1)             {                 printf("fork failed. exiting");                 exit(1);             }      if(pid_child==0)             {                execlp("/usr/bin/xterm","-e","./output",buf,null);             }      int status=0;     while(wait(&status)!=-1); } 

the line of contention here

execlp("/usr/bin/xterm","-e","./output",buf,null); //with string `buf` argument `output`. 

result:-does not run

error -e: explicit shell /~/cs60/directory/./output

-e: bad command line option "test string"

execlp("/usr/bin/xterm","-e","./output",null);//without passing variable `buf`  

result:- a) new xterm window opens. b) output terminates insufficient parameters (as expected).

the manpage states xterm:

-e program [ arguments ... ]

            option specifies program (and command  line  arguments) run in xterm window. 

it works fine when run terminal (as script). how can achieve through c.

any highly appreciated

you need understand how execlp() works.

you need add second argument execlp command name ("xterm").

execlp("/usr/bin/xterm", "xterm", "-e", "./output", buf, null); 

also, output program may want fflush (so see output), , should exit or otherwise take proper evasive action if execl() fails. note when command name ("/usr/bin/xterm") contains slashes, execlp() behaves same execl().


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -