³Ì«á¨Ò¤l»Ýn¬Y¨Ç C µ{¦¡³]pª¾ÃÑ. ¤j³¡¤Àªº Linux ³nÅé¬O¥Î C ¼gªº, ¦Ó¥B¦Ü¤Ö¾ÇÂI C ©úÅ㪺¹ï¥ô¦ó·Q³nÅé¦w¸Ëªº¤H·|¦³§U¯q.
´c¦W¬L¹üªº(notorious) fortune µ{¦¡¦b¨C¦¸ Linux ¶}¾÷°_¨Ó®É¨q¥X«ÕÀqªº¿Î»y "fortune cookie". ¤£©¯¦a (¦³ÂùÃö·N«äªº), ³]ªk¦b Red Hat µo¦æ®M¥ó 2.0.30 ªº®Ö¤ß¤U«Ø¥ß,¥X²{¤F ¤@°ïÄY«ªº¿ù»~.
~/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
¬Ý¤@¤U fortune.c
, ¦³ÃöÁpªº´X¦æ¦b³o.
if (dirent->d_namlen == 0) continue; name = copy(dirent->d_name, dirent->d_namlen);
§ÚÌ»Ýn§ä¥X dirent
ªº structure, ¦ý¥¦¨S¦³«Å§i(declared)¦b
fortune.c Àɮפ¤, ·Q¥Î grep dirent
¨Ó¨q¥X¬O§_¦b¨ä¥¦ì©l½XªºÀɮפ¤, ¦ý¤]¨S¦³. µM¦Ó, ¦b fortune.c
Àɪº³Ì¤W¤è¦³¤U¦C³o¦æ.
#include <dirent.h>
³o¦ü¥G¬O¨t²Î¨ç¦¡®wªº include ÀÉ®×, ©Ò¥Hn§ä dirent.h ªº¦X²z¦ì¸m¬O¦b
/usr/include. ¨Æ¹ê¤W, dirent.h ªº½T¦³¦b /usr/include
¤¤, ¦ý¸ÓÀɨS¦³¥]§t dirent
ªº structure. µM¦Ó, °Ñ¦Ò¥t¤@Ó
dirent.h ÀÉ.
#include <linux/dirent.h>
³Ì«á, ¥h /usr/include/linux/dirent.h, §ÚÌ¥i§ä¨ì§Ú̩һÝn«Å§iªº structure.
struct dirent { long d_ino; __kernel_off_t d_off; unsigned short d_reclen; char d_name[256]; /* We must not include limits.h! */ };
§Ú̽s¿èÀÉ®× fortune.c, ¦Ó¥B§ïÅܦb551¦æ©M553¦æªº d_namelen
Åܦ¨ d_reclen
. ¦A¸Õ¸Õ make all
. Success.
³o¦¸«Ø¥ß¨S¦³¿ù»~. §Ú̲{¦b¯à°÷±q fortune Àò±o "cheap thrills"