Thread: Exec function using getenv

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    10

    Exec function using getenv

    Hi,
    Its for my operating systems project and i'm having some issues.

    I can't seem to figure out how to execute functions in C. I'm suppose to use getenv to construct a file path to execute a function.

    I know how to obtain the file path and current working directory.

    So my question is I guess after I use getenv("PATH") how do i construct a full file name of a file then execute it?

    I cannot use execlp or execvp to do this.

    A simple example is all i'm looking for.

    Is this the right code for executing this file?

    /Users/Scott/Desktop/a.out

    Code:
    execl("/Users/Scott/Desktop/a.out", "a.out", 0, 0);
    Thank you!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you could do
    Code:
    int result = execl("/Users/Scott/Desktop/a.out", "a.out", 0, 0);
    and print some error message if it isn't working for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    After significantly more reading I am using execv and it executes the right program however then exits my main program.....

    Code:
    int main()
    {
    
    
        int result;
        
        getcwd(cwd, sizeof(cwd));
        fprintf(stdout, "Current working dir: %s\n", cwd);
        printf("PATH = %s\n", getenv("PATH"));
        result = execv("/Users/Scott/Desktop/hello.out", NULL);
        printf("result = %i\n", result);
        do{
        printf("%c", prompt);
        fgets(input, sizeof(input), stdin);
        printf("%s", input);
        }while(strcmp(input,"exit\n"));
        
        printf("Exit, goodbye!\n");
        return 0;
    }
    ignoring the includes and some variables thats the program, why does it exit the main program after I call the hello.out program?

    do i need to be using forks on this creaking child processes?

    Also what kind of files can i open with execv? I tried opening jpgs and it doesn't work but i assume thats because its not actually executing anything...
    Last edited by Scotty33; 10-31-2011 at 12:35 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    That's how exec is supposed to work. Read the man page for execv and look for some good tutorials online. All the exec family of functions replace the current executable with the new one you call. That means your program goes bye-bye. Look into the system() function, or possibly use the fork-exec idiom. This tutorial is technically C++, but it looks like that's mostly IO. The actual fork-exec code should work in C with little to no modification.

    The exec family of functions isn't meant for "opening files". It replaces the current program with a new one, almost as if you ran that program from the command line yourself. You can't type "/path/to/foo.jpg" from your shell and expect anything to happen. You must call an executable program, like "/path/to/image_viewer foo.jpg". If you just want to open a file for processing, use fopen.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well that's the thing -

    Code:
        result = execv("/Users/Scott/Desktop/hello.out", NULL);
        printf("result = %i\n", result);
        do{
        printf("%c", prompt);
        fgets(input, sizeof(input), stdin);
        printf("%s", input);
        }while(strcmp(input,"exit\n"));
        
        printf("Exit, goodbye!\n");
        return 0;
    
    If execv is successful, you're running hello.out, and none of the red code runs.

    If you're trying to create some kind of sub-process, then you use fork() as well, like
    Code:
    if ( fork() == 0 ) {
        result = execv("/Users/Scott/Desktop/hello.out", NULL);
        printf("result = %i\n", result);  // only happens if execl fails
    } else {
        do{
        printf("%c", prompt);
        fgets(input, sizeof(input), stdin);
        printf("%s", input);
        }while(strcmp(input,"exit\n"));
        
        printf("Exit, goodbye!\n");
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    10
    Code:
    int main()
    {
    
    
    	int result;
    	int pid;
    	int status;
    	char cwd[1024];
    	char line[80];
    	char input[64];
    	
    	getcwd(cwd, sizeof(cwd));
    	fprintf(stdout, "Current working dir: %s\n", cwd);
    	printf("PATH = %s\n", getenv("PATH"));
    	
    	
    	if ((pid = fork())==0){
     		result = execv("/Users/Scott/Desktop/hello.out", NULL);
    		printf("result = %i\n", result);
     		exit(0);
     	}
     	
     	pid = wait(&status);
    	
    	
    	do{
    		printf("%c", prompt);
    		fgets(input, sizeof(input), stdin);
    		printf("%s", input);
    	}while(strcmp(input,"exit\n"));
    	
    	printf("Exit, goodbye!\n");
    	return 0;
    }
    Got it working, thanks guys! Now just to put all this into my project!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using the exec call function to execute an app
    By it01y2 in forum C Programming
    Replies: 5
    Last Post: 12-20-2009, 01:08 PM
  2. how to force a function to never return...like exec() does it
    By creeping death in forum C Programming
    Replies: 7
    Last Post: 03-28-2009, 01:08 PM
  3. using getenv correctly
    By cus in forum Linux Programming
    Replies: 3
    Last Post: 01-04-2009, 12:32 PM
  4. getenv function
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-15-2002, 09:51 AM
  5. getenv
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 02-07-2002, 06:15 PM