[sclug] C programming question and date on Linux
James Fidell
james at cloud9.co.uk
Sat Oct 25 09:05:36 UTC 2003
Quoting pieter claassen (pieter at openauth.co.uk):
> Hello All,
>
> Here are two simple questions.
> 1. Why does C on linux have such cool undocumented data types such as
> u_int16_y and where are they documented?
Without checking the source, I think you'll find it's just shorthand
for "unsigned char" and "unsigned short" and you'll find a typedef to that
effect somewhere in the headers.
> My Kernighan and Ritchie says nothing about them.
Because they're not fundamental types in C.
> How do I know when I write a program which ones I am supposed to use?
You're not necessarily "supposed" to use them at all, though it
may be convenient if you want an unsigned 8-bit or 16-bit value.
> Also, if I look through the /net/ethernet.h I get the
> following declaration
>
> struct ether_header
> {
> u_int8_t ether_dhost[ETH_ALEN]; /* destination eth addr */
> u_int8_t ether_shost[ETH_ALEN]; /* source ether addr */
> u_int16_t ether_type; /* packet type ID field */
> } __attribute__ ((__packed__));
>
> What is the __attribute__((__packed__)); about and where can I get more
> info on that?
Again, without checking, it's probably a hint to the compiler to force
the variables in the struct to be stored contiguously in memory. So,
in the above example, the compiler would allocate 32 bits for the entire
struct.
Where datatyptes don't match word boundaries the compiler will often
align variables to word boundaries if it's faster to access them that
way, leaving "gaps" between the storage used for variables.
> How do I find out where ETH_ALEN comes from?
find /usr/include -type f -print | xargs fgrep ETH_ALEN
and take it from there.
> 2. My PC clock is always way out and it seems to stay out. Firstly, how
> do I set daylight savings in the command line and is there a simpler way
> to set the date than with date -s foo? Is there is way to stop this
> problem (there must be time servers online with appropriate clients that
> does this magic securely)?
You can set the current effective timezone using the TZ environment
variable. However, unless you need it otherwise it's a lot easier just
to set the correct timezone globally. The exact details of how to do
that depend on your distribution, but, for example, in RedHat 7.3 that
I happen to be running on my desktop, I'd put
ZONE="Europe/London"
into /etc/sysconfig/clock. Timezone data is in /usr/share/zoneinfo.
For accurate timekeeping, NTP is what you need. All the major distributions
will include it. See http://www.eecis.udel.edu/~mills/ntp/servers.html for
information about time servers.
James
More information about the Sclug
mailing list