[net.unix-wizards] #include <stat.h> question answered

vtl@stolaf.UUCP (Victor Lee) (11/05/83)

It looks like the "<stat.h>" peculiarity only exists in 4.1BSD.
The problem with the documentation (the reference to <stat.h>)
seems to have made it into 4.2BSD, though I have only checked
one system.

Thanks to Guy Harris, Geoff Collyer, Bill Shannon and Doug Gwyn for the
"best" answers.

Victor Lee -- St. Olaf College, Northfield MN -- ihnp4!stolaf!vtl
					      -- decvax!stolaf!vtl

The following is a summary of the responses:
------
Looking at a V7 "/usr", an S3 "/usr" and a 4.2BSD "/usr" we have online,
there doesn't seem to be a "stat.h" in "/usr/include", only in
"/usr/include/sys".  I'd vote for '#include <sys/stat.h>'.  I suspect there
was a typo in the V7 manual when it said <stat.h>, somebody at Berkeley took it
seriously, and when it didn't work instead of correcting their code they
put a copy of "stat.h" in "/usr/include".

------
<stat.h> is a hang-over from PWB (System I) where there was no
/usr/include/sys; Berkeley has propagated this stupidity.  There's no
great mystery.

------
<stat.h> was a mistake that crept into the Berkeley system at some
point.  The correct file is <sys/stat.h>.  This has been fixed in
4.2bsd.  A similar thing happened to several other files, most
notably <sys/dir.h>.

------
There should not be any /usr/include/stat.h.  The correct location
for the file is /usr/include/sys/stat.h (you must include
/usr/include/sys/types.h before <sys/stat.h> to properly define the
data types).

I have no idea when Berkeley screwed this up, but other UNIXes have
<sys/stat.h> in the right place.

------

guy@rlgvax.UUCP (Guy Harris) (11/07/83)

While we're on the subject of include files "moved" between /usr/include and
/usr/include/sys, why is there a "time.h" in /usr/include/sys on Berkeley
systems and why do people include "sys/time.h"?  This file contains *only*
the definition of a "struct tm", which is only used by the routines associated
with "ctime" (like "localtime") and is not used *anywhere* in the 4.1 kernel
that I could see.  It *is* used in the 4.1c/4.2 kernel.  However, there are
*user mode* programs which include "sys/time.h", and these programs don't
compile on non-Berkeley systems unless you change the "sys/time.h" to "time.h".
Since "time.h" and "sys/time.h" are identical on 4.1 and "time.h" is a
symbolic link to "sys/time.h" on 4.2, it would make no difference when compiled
on Berkeley systems if it included "time.h", and it would at least compile
on non-Berkeley systems.

	Guy Harris
	{seismo,ihnp4,allegra}!rlgvax!guy

Lepreau@UTAH-20.ARPA (11/09/83)

From:  Jay Lepreau <Lepreau@UTAH-20.ARPA>

Not true: as distributed, there is no <time.h> in 4.2, only a <sys/time.h>,
which includes the tm struct along with the kernel time stuff.
There is no such symlink.  That is a damn good reason for not referencing
<time.h>.   Don't ask me why it's not there; I'm sure it's no accident
(I already asked them about it).
-------

guy@rlgvax.UUCP (Guy Harris) (11/10/83)

	Not true: as distributed, there is no <time.h> in 4.2, only
	a <sys/time.h>, which includes the tm struct along with the
	kernel time stuff.  There is no such symlink.  That is a damn
	good reason for not referencing <time.h>.  Don't ask me why it's
	not there; I'm sure it's no accident (I already asked them about it).

No, it's a damn good reason for Berkeley to put the include file back, unless
they are actively trying to discourage people from writing programs that
can be compiled, linked, and run on 4.2BSD as well as other flavors of
UNIX.  In V7 and 4.1BSD there was <time.h> that had the struct tm stuff
and nothing else, and <sys/timeb.h> which defined the structure filled in
by the "ftime" system call.  That structure contained, in effect, a time value
with a resolution greater than 1 second, as well as containing the time
zone and daylight savings time flag.  The information in the 4.2BSD <sys/time.h>
consists of the same sort of stuff the "timeb" structure had, as well as some
other stuff for the new timer facilities, *plus* the "struct tm" which is
not used anywhere in the kernel.  I think better choices would have been:

1) make a link, symbolic or hard, between <time.h> and <sys/time.h>

2) put only the "struct tm" in <time.h> and only the rest of the stuff in
   <sys/time.h>

3) put the "struct tm" stuff in <time.h> and leave <sys/time.h> alone

Either way, at least it would be compatible with other versions of UNIX.
They can stamp <time.h> with large red "backward compatibility only" labels
if they genuinely want to discourage people from using it.  However, not
supporting it is a minor pain for those of us who have to run programs
both under 4.2BSD and other UNIX systems (like the 4.2BSD UUCP, which does
*not* require 4.2BSD to run; we have it running on a 4.1BSD VAX and on a
MC68000-based micro running System III).  If their omission of <time.h>
was not an accident, either the fact that it makes some programs not compile
*was* an accident or was deliberate; in the latter case, the person responsible
needs to have their error pointed out to them.

4.2BSD may be just another step in the evolution of UNIX or it may be a total
departure; I think that's a judgement call.  However, it loses the point of
being a UNIX system if you can't move programs to it from other UNIX systems.
(Which is a good reason for every UNIX system out there to pick up the
Berkeley directory library; it hides the only user-visible file system
difference between 4.2BSD and other systems, as well as providing a lot of
functionality in a nice package - how many times have equivalents of those
routines been written?  Unfortunately, the include file names are different
between 4.2BSD and systems with a V7 file system, and the routines aren't in
libc.a, so that should probably be changed to make the conversion of programs
as transparent as possible.)  If a little bit of effort done once can obviate
the need for doing that same little bit of effort several times over, it's
worth doing.

	Guy Harris
	{seismo,ihnp4,allegra}!rlgvax!guy

shannon@sun.UUCP (Bill Shannon) (11/12/83)

On the Sun 4.2 system we have both a <sys/time.h> and a <time.h>.
<sys/time.h> defines the structs that the kernel uses.  <time.h>
defines the structs that are only used in user programs (i.e.
struct tm).  As a concession to existing Berkeley programs,
<sys/time.h> includes <time.h> (if KERNEL is not defined).

				Bill Shannon
				Sun Microsystems, Inc.