Thread: run code found on a website ..HELP

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    run code found on a website ..HELP

    I'm looking for help on running some code that I found on a website. I have used Code:Blocks to compile and run it but I'm not getting the results that I should get. I'm not sure where I going wrong? The code should be looking at a file to get the results for the bandwidth used.

    Can someone point me in the right direction to see where my error is?

    Thanks


    Code:
        /*
                rstats modified for standalone use.
    
                Some code copied from tomatousb project
                Copyright (C) 2006-2009 Jonathan Zarate
    
                Will read rstats backup file to JSON output.
        */
    
        #include <string.h>
        #include <stdio.h>
        #include <stdlib.h>
        #include <unistd.h>
        #include <fcntl.h>
        #include <sys/stat.h>
        #include <stdint.h>
        #include <inttypes.h>
    
        #define K 1024
        #define M (1024 * 1024)
        #define G (1024 * 1024 * 1024)
    
        #define MAX_NDAILY 62
        #define MAX_NMONTHLY 25
    
        #define MAX_COUNTER     2
    
        #define DAILY           0
        #define MONTHLY         1
    
        #define ID_V0 0x30305352
        #define ID_V1 0x31305352
        #define CURRENT_ID ID_V1
    
        typedef struct {
                uint32_t xtime;
                uint64_t counter[MAX_COUNTER];
        } data_t;
    
        typedef struct {
                uint32_t id;
    
                data_t daily[MAX_NDAILY];
                int dailyp;
    
                data_t monthly[MAX_NMONTHLY];
                int monthlyp;
        } history_t;
    
        typedef struct {
                uint32_t id;
    
                data_t daily[62];
                int dailyp;
    
                data_t monthly[12];
                int monthlyp;
        } history_v0_t;
    
        history_t history;
    
        const char uncomp_fn[] = "/var/tmp/rstats-uncomp";
    
        int f_read(const char *path, void *buffer, int max)
        {
                int f;
                int n;
    
                if ((f = open(path, O_RDONLY)) < 0) return -1;
                n = read(f, buffer, max);
                close(f);
                return n;
        }
    
        int decomp(const char *fname, void *buffer, int size, int max)
        {
                char s[256];
                int n;
    
                unlink(uncomp_fn);
    
                n = 0;
                sprintf(s, "gzip -dc %s > %s", fname, uncomp_fn);
                if (system(s) == 0)
                {
                        n = f_read(uncomp_fn, buffer, size * max);
                        if (n <= 0) n = 0;
                                else n = n / size;
                }
                else {
                        printf("%s: %s != 0\n", __FUNCTION__, s);
                }
                unlink(uncomp_fn);
                memset((char *)buffer + (size * n), 0, (max - n) * size);
                return n;
        }
    
        static void clear_history(void)
        {
                memset(&history, 0, sizeof(history));
                history.id = CURRENT_ID;
        }
    
        int load_history(const char *fname)
        {
                history_t hist;
    
                if ((decomp(fname, &hist, sizeof(hist), 1) != 1) || (hist.id != CURRENT_ID)) {
                        history_v0_t v0;
    
                        if ((decomp(fname, &v0, sizeof(v0), 1) != 1) || (v0.id != ID_V0)) {
                                return 0;
                        }
                        else {
                                // temp conversion
                                clear_history();
    
                                // V0 -> V1
                                history.id = CURRENT_ID;
                                memcpy(history.daily, v0.daily, sizeof(history.daily));
                                history.dailyp = v0.dailyp;
                                memcpy(history.monthly, v0.monthly, sizeof(v0.monthly));        // v0 is just shorter
                                history.monthlyp = v0.monthlyp;
                        }
                }
                else {
                        memcpy(&history, &hist, sizeof(history));
                }
                return 1;
        }
    
        int* get_time(int32_t xtime)
        {
                int *i = malloc(sizeof(int) * 3);
    
                i[0] = ((xtime >> 16) & 0xFF) + 1900;
                i[1] = ((xtime >> 8) & 0xFF) + 1;
                i[2] = xtime & 0xFF;
    
                return i;
        }
    
        void print_counters(data_t data, int div)
        {
                int* time = get_time(data.xtime);
    
                printf("{\"date\":");
                printf("\"%d/%d/%d\",", time[1], time[2], time[0]);
                printf("\"download\":");
                printf("\"%"PRIu64"\",", data.counter[0] / div);
                printf("\"upload\":");
                printf("\"%"PRIu64"\"", data.counter[1] / div);
                printf("}");
        }
    
        void print_json()
        {
                int i, prnt = 0;
    
                printf("{\"daily\":[");
                for(i = 0; i < MAX_NDAILY; i++)
                {
                        data_t data = history.daily[i];
    
                        if(data.xtime == 0) {
                                prnt = 0; continue;
                        }
                        else {
                                if(prnt) {
                                        printf(",");
                                }
                        }
    
                        print_counters(data, M);
                        prnt = 1;
                }
                printf("],");
    
                printf("\"monthly\":[");
                for(i = 0; i < MAX_NMONTHLY; i++)
                {
                        data_t data = history.monthly[i];
    
                        if(data.xtime == 0) {
                                prnt = 0; continue;
                        }
                        else {
                                if(prnt) {
                                        printf(",");
                                }
                        }
    
                        print_counters(data, G);
                        prnt = 1;
                }
                printf("]}");
        }
    
        int main(int argc, char *argv[])
        {
                if (argc > 1)
                {
                        load_history(argv[1]);
                        print_json();
                }
                else {
                        printf("rstats Copyright (C) 2006-2009 Jonathan Zarate\n");
                        printf("usage: <fname>\n");
                }
                return 0;
        }
    file: rstats.tgz

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Maybe the file is corrupted. Maybe the program has a bug. Maybe, maybe, maybe.

    Maybe you should elaborate on how does the program not work, e.g., what exactly is it supposed to do and what is the test input, actual output and expected output.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    Quote Originally Posted by laserlight View Post
    Maybe the file is corrupted. Maybe the program has a bug. Maybe, maybe, maybe.

    Maybe you should elaborate on how does the program not work, e.g., what exactly is it supposed to do and what is the test input, actual output and expected output.
    When I run the code I get the following message in the dos screen:
    rstats Copyright <C> 2006-2009 Jonathan Zarate
    usage: <fname>

    From what I understand the usage should show
    Date Download Upload Total
    2012-02 5.61 GB 0.33 GB 5.94 GB
    But I have no way of knowing if this is true, because of how the info is stored in the file.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It sounds like you merely did not provide the input file name as a command line argument.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Ask how you are supposed to execute the program.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    Quote Originally Posted by stahta01 View Post
    Ask how you are supposed to execute the program.

    Tim S.
    What is the purpose of your response other than to increase your post count? Run run or execute code, either way no didn't add any value to this thread.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by zebra View Post
    What is the purpose of your response other than to increase your post count? Run run or execute code, either way no didn't add any value to this thread.
    The person needs to find out what parameters needs passed on the command line.

    Edit: You do realize laserlight and I posted at the same 5 minute interval.

    Tim S.
    PS: Your post added no good to the thread. Except get you added to my ignore list.
    Last edited by stahta01; 02-09-2012 at 04:12 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code for Monitoring Website activities
    By manoj96067 in forum Linux Programming
    Replies: 4
    Last Post: 05-26-2009, 04:56 PM
  2. Hi, I found this C code that...
    By DrSnuggles in forum C++ Programming
    Replies: 13
    Last Post: 10-12-2008, 08:57 PM
  3. C/C++ Code Database Website
    By Sembiance in forum C++ Programming
    Replies: 2
    Last Post: 08-24-2005, 01:56 PM
  4. question regarding code found on this board
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-23-2002, 08:03 PM