Thread: Using the exec call function to execute an app

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    21

    Wink Using the exec call function to execute an app

    Im just wondering how i can pass multiple parameters in this instance:

    Prompt the user to enter a program name and then input multiple parameters:

    e.g:Program Name: ls, First Parameter: -l (and so on....)

    so... Program var is : ls, parameter var 1 = -l, parameter var 2 = -p, etc. ......

    Code:
    char program[256];
    char *parameters=NULL;
    execlp(program,program,parameters, (char *)0);
    I need to use the execlp or an alternative call.

    Thanks

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    21
    I have looked around for the solution but cant get my code to work, can you help?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by it01y2 View Post
    I have looked around for the solution but cant get my code to work, can you help?
    The code you posted will obviously not do anything. Is that what you have tried? If not, post what you are actually doing, and someone can correct it.

    ps. "NULL" will suffice in place of (char*)0.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    21

    Code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <string.h>
    #include "simpleio.h"
    
    int main(int arc, char **argv, char **envp)
    {
    	int i, repetitions,separator;
    	char *result=NULL;
    	char input[256];
            char *arg_list=NULL;
    
    	char program[256];
    	
    	printf("Dmush$: "); 
    		
    	getString(input,256);
    
    	int x=0;
    
    	result = strtok( input, " ");
    	while( result != NULL ) {
    
    			if(x ==0)
    			{
    				strcpy(program,result);
    			}
    			else
    			{
    			arg_list = result;
    			}
    	x++;
    	result = strtok( NULL, " ");
    	}
    			execlp(program,program,arg_list,(char *)0); /* Access Child */
    			printf("Dmush$: Error - Unknown command '%s'\n",program);
    }
    Last edited by it01y2; 12-20-2009 at 11:07 AM.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    "NULL" will suffice in place of (char*)0.
    Unfortunately, that's not the case. Because execlp() take a variable number of arguments, no conversions apart from the usual arithmetic conversions are done. If NULL is (void*) then it'll probably work (I don't believe that void* and char* are required to be passed in the same way, but I'd be shocked if there were a system where they aren't); but if NULL is 0 then you're passing an int where execlp() expects a char*, and if these are different sizes, it's a real problem.

    This is one of those rare cases where a cast is needed; either (char *)NULL or (char *)0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive function
    By WatchTower in forum C Programming
    Replies: 11
    Last Post: 07-15-2009, 07:42 AM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

Tags for this Thread