C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 5th, 2012, 12:35 PM
rarment rarment is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 54 rarment User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 11 m 17 sec
Reputation Power: 2
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.

Reply With Quote
  #2  
Old September 5th, 2012, 01:16 PM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Dec 2003
Posts: 2,476 ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 5 h 48 m 16 sec
Reputation Power: 2194
Quote:
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!

Reply With Quote
  #3  
Old September 5th, 2012, 01:23 PM
rarment rarment is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 54 rarment User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 11 m 17 sec
Reputation Power: 2
Quote:
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

Reply With Quote
  #4  
Old September 5th, 2012, 02:00 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 58 m 15 sec
Reputation Power: 1774
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

Reply With Quote
  #5  
Old September 5th, 2012, 02:03 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,130 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 19 h 43 m 21 sec
Reputation Power: 1949
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.

Reply With Quote
  #6  
Old September 5th, 2012, 04:39 PM
rarment rarment is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 54 rarment User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 11 m 17 sec
Reputation Power: 2
Quote:
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);
    }
    
}

Reply With Quote
  #7  
Old September 5th, 2012, 05:00 PM
ptr2void ptr2void is offline
I haz teh codez!
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Dec 2003
Posts: 2,476 ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level)ptr2void User rank is General 18th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 5 h 48 m 16 sec
Reputation Power: 2194
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".

Reply With Quote
  #8  
Old September 5th, 2012, 07:16 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,387 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 1 Day 21 h 39 m 3 sec
Reputation Power: 4080
Quote:
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

Reply With Quote
  #9  
Old September 5th, 2012, 07:20 PM
rarment rarment is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 54 rarment User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 11 m 17 sec
Reputation Power: 2
Quote:
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);
        }

}

Reply With Quote
  #10  
Old September 5th, 2012, 07:57 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,130 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 19 h 43 m 21 sec
Reputation Power: 1949
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."

Reply With Quote
  #11  
Old September 5th, 2012, 08:45 PM
rarment rarment is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 54 rarment User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 11 m 17 sec
Reputation Power: 2
Quote:
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!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Trying to remember how to program in C

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap