Thread: execlp command

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    36
    Rep Power
    10

    execlp command


    Hi guys,

    I am trying to use the execlp command in my C program to run the PS command and send the output into a file specified in the Argument. Following is the command that I am trying to use without any luck.
    any help would be greatly appreciated.
    Thanks, Arun

    PHP Code:
    #include <stdio.h>
    main()
    {
    char Argument[10]
    printf ("Enter Argument\n");
    gets(Argument);

    execlp("/bin/ps","ps > "%s",Argument",NULL);

  2. #2
  3. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    I don't think all of your code is here. What is this line supposed to be?
    Code:
    execlp("/bin/ps","ps > "%s",Argument",NULL);
    execlp() does not take printf() like arguments. Also, your double quotes are all over the place. Lastly, gets() is one of the most dangerous method calls ever created in the standard C library. If this is just a school project, ok. If this is meant to ever be used by someone besides you then it needs to be removed.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    36
    Rep Power
    10
    Originally Posted by stdunbar
    I don't think all of your code is here. What is this line supposed to be?
    Code:
    execlp("/bin/ps","ps > "%s",Argument",NULL);
    execlp() does not take printf() like arguments. Also, your double quotes are all over the place. Lastly, gets() is one of the most dangerous method calls ever created in the standard C library. If this is just a school project, ok. If this is meant to ever be used by someone besides you then it needs to be removed.
    You are right, this is for a school project. I am trying to execute the UNIX's PS command in my C program. If I use the following command in my C program the PS command gets executed and the output of the PS command is written on the screen.
    execlp("/bin/ps","ps",NULL);

    But I need to change this command so the output of the PS command goes into a file, which is specified in the Argument as shown in the earliear code. I am not sure where to instert the Argument(file name) in this command.

    Thanks
  6. #4
  7. Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    May 2004
    Location
    Superior, CO, USA
    Posts
    2,418
    Rep Power
    1713
    Take a look at the sprintf() man page. You'll want something on the order of:
    Code:
    char command[128];
    sprintf( command, "ps > %s", argument );
    
    execlp("/bin/sh", command, NULL);
    You want to run the shell instead of ps as you want the redirect - that is a shell concept.

    As a warning this is totally untested...
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jul 2004
    Location
    Middle Europa
    Posts
    1,200
    Rep Power
    14

    why not


    Originally Posted by arun0000
    Hi guys,

    I am trying to use the execlp command in my C program to run the PS command and send the output into a file specified in the Argument. Following is the command that I am trying to use without any luck.
    any help would be greatly appreciated.
    Thanks, Arun

    PHP Code:
    #include <stdio.h>
    /* since early '90 , is main a int, declared as
    int main(int argc,char **argv)
    and returns|exits an int :(
    */
    main()
    {
    char Argument[10]
    printf ("Enter Argument\n");
    gets(Argument); 
    /*VERY. VERY, VERY dangerous !!!!!!
    the chance of a core dump is 99,99%
    prefer something like
    fgets(Argument,sizeof(Argument),stdin);
    */
    execlp("/bin/ps","ps > "%s",Argument",NULL); /* that's no C */
    /* re-read man pages of execlp */
    /*try
    #define BUFF 512
    FILE *ps;
    char buff[BUFF];
    ps = popen("/bin/ps mybe-whith-options","r");
    while(fgets(buff,sizeof(bufff),ps)) printf("%s",buff);
    pclose(ps);
    */


    at least: think about color-blind peoble, don't play with colors!!
    working on Solaris[5-9], preferred languages french and C.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0
    if the below is a true command sequence for ps as arguments
    char commbuffer[1024];
    strcpy(commbuffer , " > \"%s\", "); /* note your comma whether it should be a space (or was to be outside the quotes???) to separate arguments to execlp (really only one for most purpose) */
    strcat(commbuffer , Argument);
    strcat(commbuffer ,"\0"); /* terminate the string or it could go funny in the head after reading it */

    execlp("/bin/ps","/bin/ps",commbuffer,NULL);

    also these exec.. return an int , and there are other versions such as execvp virtual path
    e.g.
    char *const Arrayofargs[3]={"ps",commbuffer,NULL};
    execvp("ps",Arrayofargs);
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,441
    Rep Power
    1688
    Hopefully in the past 6 1/2 years a solution had been found ...
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0
    Originally Posted by SimonJM
    Hopefully in the past 6 1/2 years a solution had been found ...
    True, but i put it there because i had those troubles finding it myself so its more a public lookup not an answer.

    Each time i looked it up on the net the technical information explanation is only useful for Comp. Sci. Phd. to interpret in the platform API docs for C. It took me a few months to decipher various docs and its a limited type of function. system() or the platform program API command execution accesses may be much better to use but most of those can be quite complex.

    nb: forgot to ad the int it returns is only -1 on error and NULL for anything else.

    forums.netbeans.org/topic44740.html

IMN logo majestic logo threadwatch logo seochat tools logo