header include compatibility question

Discussion in 'C Programming' started by songbird, Jan 27, 2011.

  1. songbird

    songbird Guest

    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,
    songbird, Jan 27, 2011
    #1
    1. Advertising

  2. songbird

    Seebs Guest

    On 2011-01-27, songbird <> wrote:
    > 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
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
    Seebs, Jan 27, 2011
    #2
    1. Advertising

  3. songbird

    Jorgen Grahn Guest

    On Thu, 2011-01-27, Seebs wrote:
    > On 2011-01-27, songbird <> wrote:
    >> 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 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

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, Jan 27, 2011
    #3
  4. songbird <> writes:
    > 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?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 27, 2011
    #4
  5. songbird

    songbird Guest

    Keith Thompson wrote:
    > songbird writes:


    >> 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.)


    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,
    songbird, Jan 28, 2011
    #5
  6. songbird

    songbird Guest

    Seebs wrote:
    > On 2011-01-27, songbird <> wrote:
    >> 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.


    *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. :)


    >> 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


    righto. *whack!* thanks,


    songbird
    songbird, Jan 28, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aguilar, James
    Replies:
    2
    Views:
    675
    Aguilar, James
    Jul 16, 2004
  2. Victor Bazarov
    Replies:
    18
    Views:
    558
    Richard Herring
    Feb 21, 2005
  3. Andreas Bogenberger
    Replies:
    3
    Views:
    885
    Andreas Bogenberger
    Feb 22, 2008
  4. mlt
    Replies:
    2
    Views:
    817
    Jean-Marc Bourguet
    Jan 31, 2009
  5. mathog
    Replies:
    3
    Views:
    289
    mathog
    Oct 17, 2013
Loading...

Share This Page