Hello, I'm trying to setup a #define macro that can be used through out the program and also part of another macro. I know this won't work but you may get the idea of what I am trying to do:
#define PARALLEL_PORT parport0 #define PARPORT_BASE_FILE "/proc/sys/dev/parport/"PARALLEL_PORT"/base-addr" ... ... fd = open("/dev/PARALLEL_PORT",O_RDWR); ... printf(PARPORT_BASE_FILE\n); ...
Any help on this would be much appreciated.
Joseph Smith wrote:
I'm trying to setup a #define macro that can be used through out the program and also part of another macro.
Please take it to comp.lang.c!
Google for stringify on the way.
//Peter
On Sat, 20 Dec 2008 21:59:36 +0100, Peter Stuge peter@stuge.se wrote:
Joseph Smith wrote:
I'm trying to setup a #define macro that can be used through out the program and also part of another macro.
Please take it to comp.lang.c!
Sorry, I am still learning more and more every day....Thanks to all you greate code writers :-)
Google for stringify on the way.
Ok, I guess I am just a little confused, I have only ever used a #define to signify one thing.
Joseph Smith wrote:
I'm trying to setup a #define macro that can be used through out the program and also part of another macro.
Please take it to comp.lang.c!
Sorry, I am still learning more and more every day....
And that's a great thing. Please don't let me discourage it. But coreboot isn't really the place to learn C.
Google for stringify on the way.
Ok, I guess I am just a little confused, I have only ever used a #define to signify one thing.
http://gcc.gnu.org/onlinedocs/cpp/Stringification.html
//Peter
Sorry, I am still learning more and more every day....
And that's a great thing. Please don't let me discourage it. But coreboot isn't really the place to learn C.
I hate to say it Peter sometimes you can be a little discouraging, I think it is just embedded in your personality, so I try not to take offence to it. For me personally the best way to learn is from my peers, but if it irritates people that I ask C related questions on the mailing list I will stop, and find help else where.
Hi Joe,
Joseph Smith wrote:
And that's a great thing. Please don't let me discourage it. But coreboot isn't really the place to learn C.
I hate to say it Peter sometimes you can be a little discouraging, I think it is just embedded in your personality, so I try not to take offence to it.
Sorry about that. I know I can be harsh sometimes. I hope people have the energy to let me know if I'm out of line.
For me personally the best way to learn is from my peers, but if it irritates people that I ask C related questions on the mailing list I will stop, and find help else where.
Learning from peers is really nice. My point was that there are very good communities, much better than coreboot, for learning C. I know of the newsgroup and the ##c IRC channel on freenode, they both have FAQs and a wealth of other information already structured to help people.
I do have a passion for teaching whatever little I think I know, but I believe it is important for each forum to keep it's focus.
//Peter
On Sat, Dec 20, 2008 at 11:45 AM, Joseph Smith joe@settoplinux.org wrote:
Hello, I'm trying to setup a #define macro that can be used through out the program and also part of another macro. I know this won't work but you may get the idea of what I am trying to do:
#define PARALLEL_PORT parport0 #define PARPORT_BASE_FILE "/proc/sys/dev/parport/"PARALLEL_PORT"/base-addr" ... ... fd = open("/dev/PARALLEL_PORT",O_RDWR); ... printf(PARPORT_BASE_FILE\n); ...
you can do this kind of magic. I frown on it. Why? Because it is 1. not totally portable 2. saving time on a one-time operation which takes a trivial amount of time 3. over-emphasizing cpp magic, which is way overdone nowadays. 4. inflexible; why are you hard-coding a parallel port name at compile time? What if you need to use a different one-- what do you do, recompile the program?
An easy and portable and flexible way to do this: char *partport = "parport0"; static char parportname[MAXPATHLEN];
sprintf(parportname, "/proc/sys/dev/parport/%s/base-addr", parport );
now add the code to set parport at runtime based on options.
In general, but especially for those things which can change, it's a very bad idea to hard-code them in cpp magic.
Of course, this code is still very linux specific, but ...
thanks
ron
ron minnich wrote:
char *partport = "parport0"; static char parportname[MAXPATHLEN];
sprintf(parportname, "/proc/sys/dev/parport/%s/base-addr", parport );
Please make sure to always use
snprintf(parportname, sizeof(parportname), "/proc/...", parport);
to avoid buffer overflows, and check the return value. Not that I always do, but I should.
//Peter
On Mon, Dec 22, 2008 at 8:54 AM, Peter Stuge peter@stuge.se wrote:
ron minnich wrote:
char *partport = "parport0"; static char parportname[MAXPATHLEN];
sprintf(parportname, "/proc/sys/dev/parport/%s/base-addr", parport );
Please make sure to always use
snprintf(parportname, sizeof(parportname), "/proc/...", parport);
to avoid buffer overflows, and check the return value. Not that I always do, but I should.
arg! good point. I have gotten out of the habit of using any of these, as Plan 9 has this: smprint(format, ...)
smprint mallocs the buffer as needed.
snprintf now feels SOOO 20th-century, as my daughter might say :-)
ron
ron minnich wrote:
Please make sure to always use
snprintf(parportname, sizeof(parportname), "/proc/...", parport);
to avoid buffer overflows, and check the return value. Not that I always do, but I should.
arg! good point. I have gotten out of the habit of using any of these, as Plan 9 has this: smprint(format, ...)
smprint mallocs the buffer as needed.
It's asprintf in GNU.
snprintf now feels SOOO 20th-century, as my daughter might say :-)
It still makes sense in loops, to avoid overhead.
//Peter
On Mon, 22 Dec 2008 17:54:08 +0100, Peter Stuge peter@stuge.se wrote:
ron minnich wrote:
char *partport = "parport0"; static char parportname[MAXPATHLEN];
sprintf(parportname, "/proc/sys/dev/parport/%s/base-addr", parport );
Please make sure to always use
snprintf(parportname, sizeof(parportname), "/proc/...", parport);
to avoid buffer overflows, and check the return value. Not that I always do, but I should.
Thanks for the tip Peter, I'll check this out.
On Mon, 22 Dec 2008 08:47:28 -0800, "ron minnich" rminnich@gmail.com wrote:
On Sat, Dec 20, 2008 at 11:45 AM, Joseph Smith joe@settoplinux.org
wrote:
Hello, I'm trying to setup a #define macro that can be used through out the program and also part of another macro. I know this won't work but you
may
get the idea of what I am trying to do:
#define PARALLEL_PORT parport0 #define PARPORT_BASE_FILE
"/proc/sys/dev/parport/"PARALLEL_PORT"/base-addr"
... ... fd = open("/dev/PARALLEL_PORT",O_RDWR); ... printf(PARPORT_BASE_FILE\n); ...
you can do this kind of magic. I frown on it. Why? Because it is
- not totally portable
- saving time on a one-time operation which takes a trivial amount of
time 3. over-emphasizing cpp magic, which is way overdone nowadays. 4. inflexible; why are you hard-coding a parallel port name at compile time? What if you need to use a different one-- what do you do, recompile the program?
An easy and portable and flexible way to do this: char *partport = "parport0"; static char parportname[MAXPATHLEN];
sprintf(parportname, "/proc/sys/dev/parport/%s/base-addr", parport );
now add the code to set parport at runtime based on options.
In general, but especially for those things which can change, it's a very bad idea to hard-code them in cpp magic.
Of course, this code is still very linux specific, but ...
Excellent, Thanks Ron :-) That's the kind of help I am talking about :-)