Unix 6th Edition source oddity
The 6th Edition Unix kernel source code has a very weird struct:
/*
* structure to access an
* integer in bytes
*/
struct
{
char lobyte;
char hibyte;
};
That’s from /usr/sys/param.h, but it appears a couple of other times in the source tree.
This doesn’t make sense in modern day C, but in the C of 1975, all struct members were in a single namespace.
You can see remnants today. From man 3type stat
on a typical Linux system:
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* Inode number */
mode_t st_mode; /* File type and mode */
nlink_t st_nlink; /* Number of hard links */
uid_t st_uid; /* User ID of owner */
gid_t st_gid; /* Group ID of owner */
dev_t st_rdev; /* Device ID (if special file) */
off_t st_size; /* Total size, in bytes */
...
}
Elements of the struct have names st_dev
, st_ino
,
rather than dev
or device
, inode
.
The st_
prefix ensured that all the various struct stat
elements
had the correct offset from a struct pointer.
Back in 1975, you could use ->st_nlink
to get an offset from any old pointer.
The weird, anonymous struct let Thompson and Ritchie access high and low
bytes off any pointer address, p->hibyte
, p->lobyte
,
which was important on a 16-bit CPU like the PDP-11s that 6th Edition Unix ran on.