Next Previous Contents

11. Third Example: Fortune

This example requires some knowledge of C programming. The majority of UNIX/Linux software is written in C, and learning at least a little bit of C would certainly be an asset for anyone serious about software installation.

The notorious fortune program displays up a humorous saying, a "fortune cookie", every time Linux boots up. Unfortunately (pun intended), attempting to build fortune on a Red Hat distribution with a 2.0.30 kernel generates fatal errors.


~/fortune# make all


gcc -O2 -Wall -fomit-frame-pointer -pipe   -c fortune.c -o
fortune.o
fortune.c: In function `add_dir':
fortune.c:551: structure has no member named `d_namlen'
fortune.c:553: structure has no member named `d_namlen'
make[1]: *** [fortune.o] Error 1
make[1]: Leaving directory `/home/thegrendel/for/fortune/fortune'
make: *** [fortune-bin] Error 2

Looking at fortune.c, the pertinent lines are these.


   if (dirent->d_namlen == 0)
            continue;
        name = copy(dirent->d_name, dirent->d_namlen);

We need to find the structure dirent, but it is not declared in the fortune.c file, nor does a grep dirent show it in any of the other source files. However, at the top of fortune.c, there is the following line.


#include <dirent.h>

This appears to be a system library include file, therefore, the logical place to look for dirent.h is in /usr/include. Indeed, there does exist a dirent.h file in /usr/include, but that file does not contain the declaration of the dirent structure. There is, however, a reference to another dirent.h file.


#include <linux/dirent.h>

At last, going to /usr/include/linux/dirent.h, we find the structure declaration we need.


struct dirent {
        long            d_ino;
        __kernel_off_t  d_off;
        unsigned short  d_reclen;
        char            d_name[256]; /* We must not include
limits.h! */
};

Sure enough, the structure declaration contains no d_namelen, but there are a couple of "candidates" for its equivalent. The most likely of these is d_reclen, since this structure member probably represents the length of something and it is a short integer. The other possibility, d_ino, could be an inode number, judging by its name and type. As a matter of fact, we are probably dealing with a "directory entry" structure, and these elements represent attributes of a file, its name, inode, and length (in blocks). This would seem to validate our guess.

Let us edit the file fortune.c, and change the two d_namelen references in lines 551 and 553 to d_reclen. Try a make all again. Success. It builds without errors. We can now get our "cheap thrills" from fortune.


Next Previous Contents