Results 1 to 10 of 12
so im trying to fork another process in a program im writing that will open the text editor on a specific file..
like this:
pid_t parent_id, child_id,exited_id;
parent_id = getpid();
...
- 10-20-2007 #1Just Joined!
- Join Date
- Oct 2007
- Posts
- 60
execve() issue
so im trying to fork another process in a program im writing that will open the text editor on a specific file..
like this:
pid_t parent_id, child_id,exited_id;
parent_id = getpid();
if((child_id = fork()) == 0){
int status;
pid_t id = getpid();
char* newProg = "/home/mazazino/Desktop/dsk/272.1/powers-1/powers_one";
char* newArgs = {NULL};
char* newEnv = {NULL};
status = execve(newProg,newArgs,newEnv);
exit(0);
}
exited_id = wait();
printf("child process done\n");
so in the execve() instruction, i want to load the executable of the text editor (Kate in Kubuntu), which is supposedly somehwere in the root but i didnt find it..and then i wanna open a specific file directly in the editor...so if anyone has any idea what the executable file of the text editor is, and how to automatically load the text file i want to open in the editor, i would appreciate the help..
- 10-20-2007 #2
There are several things wrong with this program, one of which is that it won't compile.
When you fix the compilation problems, consider also these points:
If you're going to use execve(), you don't really want to pass an empty environment variable list. You want to look at your current environment variable list and change (or weed out) only what's absolutely necessary to change (or weed out).
You can get a list of the current process's environment variables by declaring your main() function thus:
You can then examine arge.Code:int main(int argc, char **argv, char **arge )
You'll want newProg to be the name of Kate itself (herself?). You'll need to find the full pathname of that (/usr/bin/kate, or whatever).
If you're willing to hardwire that pathname into your program, do this at the command line to find that pathname:
If you want to be more general about this, have your program look at all the paths in the PATH environment variable, and find the first one which contains a copy of kate which is executable.Code:which kate
The first element in newArgs should be the same as newProg (that is, the fully qualified pathname to kate that you just found).
The remaining elements in newArgs are the same command-line arguments you'd use when running kate from the command line, including the name of the file you'll be editing.
For further info, please enter these commands at the command line, and read them carefully before asking your next question. (grin)
Hope this helps.Code:man kate man execve man wait man which
- 10-20-2007 #3Just Joined!
- Join Date
- Oct 2007
- Posts
- 60
i tried typing 'kate mytextfile' in thhe directory that 'mytexyfile' exists from the shell and it openned kate at the text file i want(which is called 'mytextfile'..so i know now that the first argument the program takes is "kate", and the second is the file name which has to be in the same directory im in..so i modified the code..but i dont know what environment variables kate should take...actually i dont know what are environment variables..i used the which command to find the path to kate and i did..and i put it in...so tell me where'd i go wrong in this..('file' is the name of the text file i wanna open)
if((child_id = fork()) == 0){
int status;
char* newProg = "/usr/bin/kate";
char* newArgs = {"kate","/home/mazazino/Desktop/dsk/Trials/mytextfile",NULL};
char* newEnv = {NULL};
status = execve(newProg,newArgs,newEnv);
exit(0);
}
exited_id = wait();
printf("done\n");
- 10-20-2007 #4
You couldn't have possibly run the code you post here, because it won't compile without errors. Fix that first, then let's continue.
- 10-20-2007 #5Just Joined!
- Join Date
- Oct 2007
- Posts
- 60
i did...it gave me warnings but it did run..
- 10-20-2007 #6
I've done some experimenting, and there's much to untangle here.
First, is it true that this isn't the whole source file? Is this in a function main()? Do you have any #include statements? Because it's the set of #include statements you used that could provide the path to enlightenment.
Could you please post a program which:
- is a complete source file, so that we can compile it and watch the behavior you talk about; and
- is tiny enough that we can see clearly just what's going on?
And to make it more readable, could you please put it within CODE markers? To do this:
- highlight the code as it sits in your reply window at this site; and
- click the # icon just above your editing space.
- 10-20-2007 #7Just Joined!
- Join Date
- Oct 2007
- Posts
- 60
this is the full code...
#include <stdio.h>
#include <sys/types.h>
#include <termios.h>
#include <unistd.h>
#include <ctype.h>
#include <signal.h>
int main()
{
pid_t parent_id, child_id,exited_id;
parent_id = getpid();
if((child_id = fork()) == 0){
int status;
char* newProg = "/usr/bin/kate";
char* newArgs = {"kate","/home/mazazino/Desktop/dsk/Trials/file",NULL};
char* newEnv = {NULL};
status = execve(newProg,newArgs,newEnv);
exit(0);
}
exited_id = wait();
printf("done\n");
return 0;
}
- 10-20-2007 #8Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
If you dont' pass an environment variable with the 3th argument you can use execvp:
RegardsCode:char* arg_list[] = "/usr/bin/kate","/home/mazazino/Desktop/dsk/Trials/mytextfile","/",NULL}; status = execvp ("/usr/bin/kate", arg_list);
- 10-20-2007 #9Just Joined!
- Join Date
- Oct 2007
- Posts
- 60
i tried it but it gave me an error when it started Kate..it said that the file/ is not a folder..so i tried to change your code and i erased the third entry in the args array, and it worked..something like this:
so it worksCode:char* args_list[] = {"/usr/bin/kate","/home/mazazino/Desktop/dsk/Trials/file",NULL}; status = execvp("/usr/bin/kate",args_list);
..thanks..but a few questions i dont know about..what's the difference between execvp and execve, and what should have been in the environment variables array if i was to use execve?..in other words, whats the environment variable array?..and thanks for the help
- 10-20-2007 #10Do this at the command line:what's the difference between execvp and execve
Code:man execvp man execve
As was mentioned in the second post of this thread:what should have been in the environment variables array if i was to use execve
Unless you know of a specific reason to do so, don't weed out anything.You want to look at your current environment variable list and change (or weed out) only what's absolutely necessary to change (or weed out).
For a good, full explanation, go here. You have access to your environment variable array by using the arge parameter to main(), as shown in the second post.whats the environment variable array?


Reply With Quote
