header include compatibility question

S

songbird

hello,

i'm fiddling with some code that contains the following:

# ifdef BSD41
# include <time.h>
# else
# include <sys/time.h>
# endif

i would like to retain compatibility but need to
have the first expression evaluate to true for my
current system (i.e. <time.h> is the one that works)

Linux Debian Gnu Squeeze using gcc (Debian 4.4.5-8) 4.4.5

in reading what documents i can scrounge up (including
the FAQ) i couldn't find a list of constants like the
BSD41 for Debian or Gnu, so i'm not sure what would be
a compatible way to do this. sorry if this is a newbie
question, but i'm wading through this the best i can...

and yes, sure i can take out the defines and use a
straight include of <time.h>, but would that break
BSD compiles for someone else?


thanks for your replies,
 
S

Seebs

i'm fiddling with some code that contains the following:
# ifdef BSD41
# include <time.h>
# else
# include <sys/time.h>
# endif

Wow, that's old.
i would like to retain compatibility but need to
have the first expression evaluate to true for my
current system (i.e. <time.h> is the one that works)

It'd be useful to know what it is that's been requested that this header
is used for.

In general, though...

What this tells you is that at some point, "BSD41" was defined on some systems
and not on others, and the "some systems" used <time.h> and the "others"
used <sys/time.h>. Usually, either that means that "BSD41" is a new thing
and the "others" are old, or vice versa. So the question is, which of these
is likely the correct default now? (Nowadays, a lot more code will work
with the same header on all likely systems.)

The simple answer is: Try something, and if it works you're probably fine.
In this case, <time.h> is a standard header which is now pretty well supported
and a lot of things that may have migrated around back in the day are now
consistently there.

You can get more specific if you happen to know that BSD 4.1 was released in
1981. The chances are, that if the BSD41 version aligns with current
practice, the other branch of the system was useful for stuff which predated
1981. My guess is you can just drop that branch entirely and you'll never
see it not be the right choice.

-s
 
J

Jorgen Grahn

Wow, that's old.

I happened to re-read the classic 1992 paper "#ifdef Considered
Harmful" today, and one of its examples contained #ifdef BSD_42.
("Figure 8. A truly awful style".)

/Jorgen
 
K

Keith Thompson

songbird said:
i'm fiddling with some code that contains the following:

# ifdef BSD41
# include <time.h>
# else
# include <sys/time.h>
# endif

i would like to retain compatibility but need to
have the first expression evaluate to true for my
current system (i.e. <time.h> is the one that works)

Linux Debian Gnu Squeeze using gcc (Debian 4.4.5-8) 4.4.5

in reading what documents i can scrounge up (including
the FAQ) i couldn't find a list of constants like the
BSD41 for Debian or Gnu, so i'm not sure what would be
a compatible way to do this. sorry if this is a newbie
question, but i'm wading through this the best i can...

and yes, sure i can take out the defines and use a
straight include of <time.h>, but would that break
BSD compiles for someone else?

If your code is only using things that are defined *by the
language standard* for <time.h>, then I'd just remove the #ifdef
and unconditionally use <time.h>. (BSD 4.1 was released in 1981;
unless you have some specific reason to think your code might be
used on such an ancient system, I wouldn't worry about it.)

The <sys/time.h> header is defined by POSIX; if you're using anything
POSIX-specific, you should include it (and keep in mind that your
code won't be portable to non-POSIX systems). POSIX also defines
some additional declarations in <time.h>. Note that including
<sys/time.h> doesn't (necessarily?) give you <time.h> so if you're
using the time() function, for example, <sys/time.h> by itself
probably isn't sufficient.

If you're using POSIX-specific stuff, comp.unix.programmer
is probably a better place to ask about it.

What declarations from <time.h> and/or <sys/time.h> is your code
actually using?
 
S

songbird

Keith said:
songbird writes:

If your code is only using things that are defined *by the
language standard* for <time.h>, then I'd just remove the #ifdef
and unconditionally use <time.h>. (BSD 4.1 was released in 1981;
unless you have some specific reason to think your code might be
used on such an ancient system, I wouldn't worry about it.)

ah, good thing.

out it goes for now. it's not that critical,
i'm just using this as a practice project
to get used to git and the other tools people
are developing with these days. i've not done
any coding beyond some simple projects for many
years. now that i have plenty of free time i
figured i'd refresh some skills and play with
some new toys.

the code compiles fine with <time.h> alone. it
has been worked on recently enough that it runs
with a few tweaks. [i won't say how many warnings
i'm ignoring and the commented out sections of
code that i'll ignore for now too... :) ]

the reason i picked this particular code is that
it is non-gui so i can upload/download versions
easy over a very slow connection and it happens to
be something i enjoyed messing with many many years
ago.

it does actually run so that is good, i have bugs
to fix yet.

The <sys/time.h> header is defined by POSIX; if you're using anything
POSIX-specific, you should include it (and keep in mind that your
code won't be portable to non-POSIX systems). POSIX also defines
some additional declarations in <time.h>. Note that including
<sys/time.h> doesn't (necessarily?) give you <time.h> so if you're
using the time() function, for example, <sys/time.h> by itself
probably isn't sufficient.

If you're using POSIX-specific stuff, comp.unix.programmer
is probably a better place to ask about it.

ok, i'll keep that in mind.

What declarations from <time.h> and/or <sys/time.h> is your code
actually using?

the time function, struct tm, and the type time_t,

for reference and fun i'm using K&R 2nd ed and
C Traps and Pitfalls. i'm still pissed at the SOB
who stole my first K&R edition.


thanks again,
 
S

songbird

Seebs said:
Wow, that's old.

*laughs* yeah, the original code is from the mid-80s.
it's been tweaked and changed over the years, the last
person who worked on it messed with it in 2008, so i'm
just having to tweak it a little to get it to run.

ignoring the stuff he commented out that i'd like
to take a crack at fixing sometime and the compiler
warnings it compiles and runs with two header fixes
and one function replacement. not bad for two
years difference.

that it doesn't run fully is ok, i'm enjoying
the bug hunting as it is making me learn how to
use various tools i've not messed with before.
which is the whole point of this project anyways. :)

It'd be useful to know what it is that's been requested that this header
is used for.

In general, though...

What this tells you is that at some point, "BSD41" was defined on some systems
and not on others, and the "some systems" used <time.h> and the "others"
used <sys/time.h>. Usually, either that means that "BSD41" is a new thing
and the "others" are old, or vice versa. So the question is, which of these
is likely the correct default now? (Nowadays, a lot more code will work
with the same header on all likely systems.)

The simple answer is: Try something, and if it works you're probably fine.
In this case, <time.h> is a standard header which is now pretty well supported
and a lot of things that may have migrated around back in the day are now
consistently there.

You can get more specific if you happen to know that BSD 4.1 was released in
1981. The chances are, that if the BSD41 version aligns with current
practice, the other branch of the system was useful for stuff which predated
1981. My guess is you can just drop that branch entirely and you'll never
see it not be the right choice.

-s

righto. *whack!* thanks,


songbird
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top