The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Trying to remember how to program in C
Discuss Trying to remember how to program in C in the C Programming forum on Dev Shed. Trying to remember how to program in C C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 5th, 2012, 12:35 PM
|
|
Contributing User
|
|
Join Date: Sep 2011
Posts: 54
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.
|

September 5th, 2012, 01:16 PM
|
|
|
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!
|

September 5th, 2012, 01:23 PM
|
|
Contributing User
|
|
Join Date: Sep 2011
Posts: 54
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
|

September 5th, 2012, 02:00 PM
|
 |
Contributed User
|
|
|
|
|

September 5th, 2012, 02:03 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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.
|

September 5th, 2012, 04:39 PM
|
|
Contributing User
|
|
Join Date: Sep 2011
Posts: 54
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);
}
}
|

September 5th, 2012, 05:00 PM
|
|
|
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".
|

September 5th, 2012, 07:16 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
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
|

September 5th, 2012, 07:20 PM
|
|
Contributing User
|
|
Join Date: Sep 2011
Posts: 54
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);
}
}
|

September 5th, 2012, 07:57 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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."
|

September 5th, 2012, 08:45 PM
|
|
Contributing User
|
|
Join Date: Sep 2011
Posts: 54
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!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|