#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Chicago
    Posts
    14
    Rep Power
    0

    Exclamation pass a value into system unix call


    How do I pass a parameter into a system value

    like

    #include <stdlib.h>
    main()
    {
    char file[20] = "/home/temp/temp.txt";
    printf("Files in Directory are:\n");
    system("cat file");
    }

    I need to pass file name into system. How do I do it.


    thanks
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,648
    Rep Power
    4248
    Use sprintf() or strcat() to build your command line and then pass it to system().
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    int main(void) {
        char file[20] = "/home/temp/temp.txt"; 
        char cmd[512];
        printf("Files in Directory are:\n"); 
        /* Build the command line */
        sprintf(cmd, "cat %s", file); 
        system(cmd); 
        return 0;
    }
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    55
    Rep Power
    12
    execve would be much better for this, and it is safer too as you have control over environmental variables.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Chicago
    Posts
    14
    Rep Power
    0

    Smile thanks


    Thanks for the code. I avoided writting a script and calling it. I guess I will go for the first approch unless someone can tell how to use evecve.

    Thanks Again
  8. #5
  9. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    Code:
    int  execve(const  char  *filename,  char  *const  argv [], char *const envp[]);
    an example would be:
    Code:
    char *argv[3] = {"/bin/cat", "filename", NULL};
    execve(argv[0], argv, NULL};

IMN logo majestic logo threadwatch logo seochat tools logo