Thread: very easy C question(s), pls help

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    2

    php shell_exec clone with a twist? need help

    I am NOT a C developer but I need to make a quickie C interface to an application that can run my PHP scripts, do logic on the results, etc. I am having a hard time getting the returned array to return the correct value. I wish C was as easy as PHP :/ Currently it returns the same value in every element and I have no idea why. Here is what I have so far:

    PHP Code:
    #include <stdio.h>
    #include <string.h>
    #define MAX_SHELL_EXEC_RETURN_VALUE 1000
    #include <ctype.h>

    //I dont like the shell_exec_.results syntax, I'd rather use shell_exec_data->results instead.  How do I do that?
    typedef struct shell_exec_data
    {
    //Keep it small for testing purposes, make it bigger later, ideally it should only be as big as the actual results.  I dont know if thats possible with C though.
     
    char *results[5]; 
    }
    SHELL_EXEC_DATA;
    SHELL_EXEC_DATA shell_exec();

    //Testing area, all your main are belong to us
    int main()
    {
      
    SHELL_EXEC_DATA test;
      
    test shell_exec("ls -l");  //Quickie command to get \n terminated lines for test data
      
    printf("main() print: %s, size: %d"test.results[2], sizeof(test.results));//Print element 2 of the results array.
    }

    //The guts of the thing, shell_exec() function
    SHELL_EXEC_DATA shell_exec(charcommand)
    {
       
    FILE *fp;
       
    int i 0;
       
    int key 0;
       
    char data[255];
       
    SHELL_EXEC_DATA shell_exec;
       
    fp popen(command"r");
       if (
    fp == NULL)
       {
          
    //error handling here
          
    return;
       }

       while (
    fgets(dataMAX_SHELL_EXEC_RETURN_VALUEfp) != NULL)
       {
          
    printf("%s"data);//Print out the value of path to make sure its right
          
    shell_exec.results[i] = path;//Stuff the value into array
          
    i++;
       }
       
    pclose(fp);

       
    //Before returning, make sure array is correct
       
    for (key=0;key<5;key++)
       {
          
    printf("%d %s\n"keyshell_exec.results[key]);//print out each element in array
       
    }

        
    //return struct/object whatever which contains array since C cant return an array.
        
    return shell_exec;

    The output of this on a linux machine is:
    [root@localhost ryan]# gcc test2.c
    [root@localhost ryan]# ./a.out
    total 1122
    drwxr-xr-x 3 ryan ryan 216 May 8 16:39 Desktop
    drwxr-xr-x 2 ryan ryan 80 May 8 16:39 Documents
    drwxr-xr-x 2 ryan ryan 80 May 8 16:39 Movies
    drwxr-xr-x 2 ryan ryan 80 May 8 16:39 Music
    drwxr-xr-x 2 ryan ryan 80 May 8 16:39 Pictures
    -rwxr-xr-x 1 root root 7417 Jun 17 15:36 a.out
    drwxr-xr-x 15 root root 888 May 30 10:09 ldir
    drwxr-xr-x 10 root root 712 May 30 10:50 pvpgn-199.r521
    -rw-r--r-- 1 root root 1125922 May 30 10:11 pvpgn-199.r521.tar.gz
    -rw-r--r-- 1 root root 2121 Jun 17 12:20 test.c
    -rw-r--r-- 1 root root 50 Jun 17 00:53 test.php
    -rw-r--r-- 1 root root 1589 Jun 17 15:36 test2.c
    lrwxrwxrwx 1 ryan ryan 4 May 8 16:39 tmp -> /tmp
    0 lrwxrwxrwx 1 ryan ryan 4���� 8 16:39 tmp -> /tmp

    1 lrwxrwxrwx 1 ryan ryan 4���� 8 16:39 tmp -> /tmp

    2 lrwxrwxrwx 1 ryan ryan 4���� 8 16:39 tmp -> /tmp

    3 lrwxrwxrwx 1 ryan ryan 4���� 8 16:39 tmp -> /tmp

    4 lrwxrwxrwx 1 ryan ryan 4���� 8 16:39 tmp -> /tmp

    main() print: lrwxrwxrwx 1 ryan ryan 4���� 8 16:39 tmp -> /tmp
    , size: 20[root@localhost ryan]#
    Notice how the date is 4,,,,, in the bad results. I dont know why it does that, but in the terminal its a wierd looking character. Like memory corruption or something, so theres something done wrong somewhere. Also notice that the returned array has the LAST result in every single element. What the heck?
    Last edited by humanturk; 06-17-2009 at 04:27 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    typedef struct shell_exec_data
    {
    //Keep i...
     char *results[5]; 
    }
    This is an array of pointers to characters. You need to actually be allocating memory for each one, and/or making it point at something. You also don't actually have anything called path which means this isn't really the code you've compiled. Also, you cannot copy strings with the = operator.


    Quzah.
    Last edited by quzah; 06-17-2009 at 04:48 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    87
    Because with "strings" in C, you are really dealing with pointers to characters, you cannot use "=" to copy a string. strncpy() will do the trick for you.
    strncpy

    To use it, you must first make sure you have allocated space for the string yourself. You'll want malloc() for that.
    malloc

    If I wanted to allocate memory for a 10 character string, i could do
    Code:
    char *s;
    
    s = malloc(10 * sizeof(char));
    You'll probably want to do that for each element of results[].

    Later, when I wanted to copy t into s, I could do
    Code:
    /* Copy at most 10 characters of data from the string pointed to
     * by t into the string pointed to by s */
    strncpy(s, t, 10);
    Hope that helps get you going.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    2
    Hmm not sure how "path" got in there, but it should say "data". I'll try to figure out how to use the malloc thing along with strncpy, I assume thats why the returned array has the same thing in it. It all points to the same place I guess, I have no idea.

    Also, if I allocate 10000000 for the max length of a string, is that 10MByte or 10MBit? Just how big is it in bytes? I dont want to allocate like a 500 element array with 10MByte strings, I assume that would eat all the ram on my machine right?

  5. #5
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by humanturk View Post
    Hmm not sure how "path" got in there, but it should say "data". I'll try to figure out how to use the malloc thing along with strncpy, I assume thats why the returned array has the same thing in it. It all points to the same place I guess, I have no idea.

    Also, if I allocate 10000000 for the max length of a string, is that 10MByte or 10MBit? Just how big is it in bytes? I dont want to allocate like a 500 element array with 10MByte strings, I assume that would eat all the ram on my machine right?
    a character is a byte
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  3. Some C test questions: challenge
    By Mister C in forum C Programming
    Replies: 47
    Last Post: 09-10-2002, 06:47 AM
  4. questions about new and delete
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2001, 01:48 PM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM