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

    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    4

    Trying to remember how to program in C


    It's been a while since I have done C programming and I have a simple C program to write and am struggling. This is a Homework assignment. I need to write a program called faked_uname and it creates an executable and when you run

    faked_uname --machine

    is should print out the machine hardware name. I know how to access uname and get that info, I guess my real question is about compiling and running it. I'm confused about the faked_uname --machine. What does that access? does it call the function "machine" inside faked_uname?? I can't remember how to do this, and I know it's something simple.
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    This is a Homework assignment
    Are you saying they assigned you something they didn't teach you anything about?
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    4
    Originally Posted by ptr2void
    Are you saying they assigned you something they didn't teach you anything about?
    It isn't a C programming class, it's just a class that uses some basic C programming, but I am having troubles recalling how to do some basic things in C
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    How about a refresher tutorial
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    You are apparently doing this under either UNIX or Linux and not under Windows.

    There is a shell command uname, whose man page can be read at http://linux.die.net/man/1/uname. One of its options is -m, also written as --machine. Read that page and familiarize yourself with the command. You should even open a shell and play with it for a bit.

    Under UNIX/Linux, many shell commands have a corresponding C function -- shell commands tend to go on page 1 of the man pages and C functions on the other pages (2 or 3, usually). The equivalent C function is uname(), whose man page you can read at http://linux.die.net/man/2/uname. It used the utsname struct, which is described on that man page. One of the struct's fields is a char array (AKA "C-style string") called machine.

    BTW, your system should have the man pages loaded on it, accessible in the shell via the man command. Or you can Google for it using the search words man page uname, substituting the command in question for "uname".

    The main function's parameters are (int argc, char *argv[]). argv is an array of strings into which the command line invocation is loaded: argv[0] is the name of the program (possibly with the directory path prepended; printf it to find out) and argv[1] contains the first command-line parameter, arg[2] the second, etc. argc tells you how many strings are in argv. In the case of your example, uname --machine, argc would be 2, argv[0] would be something like "uname", and argv[1] would be "--machine".

    In your assignment, since you already know that you will be calling uname, you could simply create a string onto which you concatenate argv[1] to create a string, eg "uname --machine" and then feed that to system() or popen(). Or you could call the uname() function and compare argv[1] to the possible option strings, identify which option it is, and output the appropriate string from the utsname struct.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    4
    Originally Posted by dwise1_aol
    You are apparently doing this under either UNIX or Linux and not under Windows.

    There is a shell command uname, whose man page can be read at http://linux.die.net/man/1/uname. One of its options is -m, also written as --machine. Read that page and familiarize yourself with the command. You should even open a shell and play with it for a bit.

    Under UNIX/Linux, many shell commands have a corresponding C function -- shell commands tend to go on page 1 of the man pages and C functions on the other pages (2 or 3, usually). The equivalent C function is uname(), whose man page you can read at http://linux.die.net/man/2/uname. It used the utsname struct, which is described on that man page. One of the struct's fields is a char array (AKA "C-style string") called machine.

    BTW, your system should have the man pages loaded on it, accessible in the shell via the man command. Or you can Google for it using the search words man page uname, substituting the command in question for "uname".

    The main function's parameters are (int argc, char *argv[]). argv is an array of strings into which the command line invocation is loaded: argv[0] is the name of the program (possibly with the directory path prepended; printf it to find out) and argv[1] contains the first command-line parameter, arg[2] the second, etc. argc tells you how many strings are in argv. In the case of your example, uname --machine, argc would be 2, argv[0] would be something like "uname", and argv[1] would be "--machine".

    In your assignment, since you already know that you will be calling uname, you could simply create a string onto which you concatenate argv[1] to create a string, eg "uname --machine" and then feed that to system() or popen(). Or you could call the uname() function and compare argv[1] to the possible option strings, identify which option it is, and output the appropriate string from the utsname struct.
    Ok, It's starting to come back to me now. This is what I have so far, no testing yet though

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/utsname.h>
    
    struct utsname unameData;
    
    
    int main(int argc, char** argv) {
    
        if (argv[1] == "--machine")
        {
            printf("%s", unameData.machine);
        }
        else if (argv[1] == "--operating-system")
        {
            printf("%s", unameData.sysname);
        }
        else if (argv[1] == "--kernel-release")
        {
            printf("%s", unameData.release);
        }
        
    }
  12. #7
  13. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    You were doing at least intermediate level stuff six months ago with C, and you've forgotten how to do one of the most rudimentary tasks in C, comparing strings???

    My chemistry teacher in high school called learning this way "the bathtub method".
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  14. #8
  15. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Originally Posted by rarment
    Ok, It's starting to come back to me now. This is what I have so far, no testing yet though

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/utsname.h>
    
    struct utsname unameData;
    
    
    int main(int argc, char** argv) {
    
        if (argv[1] == "--machine")
        {
            printf("%s", unameData.machine);
        }
        else if (argv[1] == "--operating-system")
        {
            printf("%s", unameData.sysname);
        }
        else if (argv[1] == "--kernel-release")
        {
            printf("%s", unameData.release);
        }
        
    }
    That won't work. Hint: Use strcmp().
    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
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    4
    Originally Posted by Scorpions4ever
    That won't work. Hint: Use strcmp().
    thanks, I realized that after the post about comparing strings :) . I only did a little C and then I have been working on Java and C# since.

    Here is the updated code, in case anyone see's anymore simple errors.

    Thanks!!



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/utsname.h>
    
    
    
    
    
    int main(int argc, char** argv) {
            
            struct utsname unameData;
            uname(&unameData);
        
            char machine[] = "--machine";
            char os[] = "--operating-system";
            char kernel[] = "--kernel-release";
    
            if (strcmp (argv[1], machine) == 0)
            {
                printf("%s", unameData.machine);
            }
            else if (strcmp (argv[1], os) == 0)
            {
                printf("%s", unameData.sysname);
            }
            else if (strcmp (argv[1], kernel) == 0)
            {
                printf("%s", unameData.release);
            }
    
    }
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    You forgot to return 0 for success. You should also add an else to return a non-zero for failure (eg, if they don't give you a valid option).

    You should also check up front for argc not equal to 2, in which case I think uname will have some kind of default action. For that matter, do a few test runs of uname to see what it does, since you're supposed to mimic that. What does it do if you don't give it any options? Or if you give it more than one option?

    Other than that, I would have declared the string literals as char* or just used them in the code as you did before. I normally only declare a string literal as a char array if I'm planning on modifying it later.

    Other than that, how does it work when you run it? That's what I call the "Jello Test", as in "The proof of the pudding is in the eating."
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    54
    Rep Power
    4
    Originally Posted by dwise1_aol
    You forgot to return 0 for success. You should also add an else to return a non-zero for failure (eg, if they don't give you a valid option).

    You should also check up front for argc not equal to 2, in which case I think uname will have some kind of default action. For that matter, do a few test runs of uname to see what it does, since you're supposed to mimic that. What does it do if you don't give it any options? Or if you give it more than one option?

    Other than that, I would have declared the string literals as char* or just used them in the code as you did before. I normally only declare a string literal as a char array if I'm planning on modifying it later.

    Other than that, how does it work when you run it? That's what I call the "Jello Test", as in "The proof of the pudding is in the eating."
    It's seems to be functioning with the spec's given to us. Thank you guys for your help!

IMN logo majestic logo threadwatch logo seochat tools logo