ÀÌ ¿¹´Â C ÇÁ·Î±×·¡¹Ö¿¡ ´ëÇÑ Áö½ÄÀÌ Á¶±Ý ÇÊ¿äÇÏ´Ù. À¯´Ð½º³ª ¸®´ª½º¿ë ¼ÒÇÁÆ®¿þ¾îÀÇ ¸¹Àº ºÎºÐÀÌ C·Î ¾²¿©Á® ÀÖÀ¸¹Ç·Î, ¼ÒÇÁÆ®¿þ¾î ¼³Ä¡¿¡ ´ëÇؼ ÁøÁöÇÑ »ç¶÷À̶ó¸é ÃÖ¼ÒÇÑ Á¶±ÝÀº C¸¦ ¹è¿öµÎ´Â °ÍÀÌ ÁÁ´Ù.
¾Ç¸í³ôÀº fortune ÇÁ·Î±×·¥Àº ¸®´ª½º°¡ ¶ã ¶§¸¶´Ù "fortune cookie"¶ó°í ºÒ¸®´Â Àç¹ÌÀÖ´Â °Ý¾ðÀ» º¸¿©ÁØ´Ù. ºÒÇàÇÏ°Ôµµ(unfortunately, ¹ßÀ½°®°í ³ó´ãÇÏ´Â °Å´Ù), 2.0.30 Ä¿³ÎÀ» »ç¿ëÇÏ´Â ·¹µå ÇÞ ¹èÆ÷ÆÇ¿¡¼ fortuneÀ» ÄÄÆÄÀÏÇÏ·Á°í ÇÏ¸é ½É°¢ÇÑ ¿¡·¯°¡ »ý±ä´Ù.
~/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
fortune.c
¸¦ »ìÆ캸¸é, ÇØ´çµÇ´Â ÁÙÀº ´ÙÀ½°ú °°´Ù.
if (dirent->d_namlen == 0)
continue;
name = copy(dirent->d_name, dirent->d_namlen);
¿ì¸®´Â dirent
¶ó´Â ±¸Á¶Ã¼¸¦ ã¾Æ¾ß ÇÏÁö¸¸, ÀÌ ±¸Á¶Ã¼´Â
fortune.c ¾È¿¡ ¼±¾ðµÇ¾î ÀÖÁö ¾ÊÀ¸¸ç, grep dirent¸¦ ÇغÁµµ
´Ù¸¥ ¼Ò½º ÆÄÀϵ鿡¼µµ ãÀ» ¼ö ¾ø´Ù.
ÇÏÁö¸¸, fortune.cÀÇ Á¦ÀÏ À§¿¡ ´ÙÀ½°ú °°Àº ÁÙÀÌ ÀÖ´Ù.
#include <dirent.h>
ÀÌ°ÍÀº ½Ã½ºÅÛ ¶óÀ̺귯¸®ÀÇ include ÆÄÀÏ·Î º¸À̹ǷÎ, dirent.h¸¦ ãÀ»
³í¸®ÀûÀÎ À§Ä¡´Â /usr/include ´Ù. ½ÇÁ¦·Î /usr/include¿¡´Â
dirent.h ÆÄÀÏÀÌ ÀÖÁö¸¸, ±× ÆÄÀÏÀº dirent
±¸Á¶Ã¼ÀÇ Á¤ÀǸ¦
Æ÷ÇÔÇÏ°í ÀÖÁö ¾Ê´Ù. ÀÌ ÆÄÀÏÀº ±× ´ë½Å ¶Ç´Ù¸¥ dirent.h ÆÄÀÏÀ»
ÂüÁ¶ÇÏ°í ÀÖ´Ù.
#include <linux/dirent.h>
¸¶Ä§³», /usr/include/linux/dirent.h¿¡¼, ¹Ù¶ó´ø ±¸Á¶Ã¼ ¼±¾ðÀ» ã¾Ò´Ù.
struct dirent {
long d_ino;
__kernel_off_t d_off;
unsigned short d_reclen;
char d_name[256]; /* We must not include limits.h! */
};
¹°·Ð ÀÌ ±¸Á¶Ã¼ ¼±¾ðÀº d_namelen À» Æ÷ÇÔÇÏ°í ÀÖÁö ¾ÊÁö¸¸, ±×¿¡ ÇØ´çµÉ ¸¸ÇÑ "Èĺ¸"´Â µÎ °³ ÀÖ´Ù. °¡Àå ±×·² µí ÇÑ °ÍÀº d_reclenÀε¥, ÀÌ ±¸Á¶Ã¼ ¸â¹ö´Â ¹«¾ùÀΰ¡ÀÇ ±æÀ̸¦ ³ªÅ¸³»´Â °Í °°°í ¶Ç short integer ÇüÀ̱⠶§¹®ÀÌ´Ù. ¶Ç ÇϳªÀÇ °¡´É¼ºÀº, d_inoÀε¥ ±× À̸§°ú ÀÚ·áÇüÀ¸·Î º¸¾Æ inodeÀÇ ¹øÈ£ÀÏ °Í °°´Ù. ¿ì¸®´Â ¾Æ¸¶µµ "directory entry" ±¸Á¶Ã¼¸¦ ´Ù·ç°í ÀÖ´Â µí ÇÏ´Ù. ±× ¿ø¼ÒµéÀº ÆÄÀÏÀÇ ¼Ó¼º, ±× À̸§, inode, ±æÀÌ(ºí·° ¼ö)¸¦ ³ªÅ¸³¾ °ÍÀÌ´Ù. À̰͵µ (d_reclen °ú d_ino ¿¡ ´ëÇÑ) ¿ì¸®ÀÇ ÃßÃøÀ» È®ÀÎÇØ ÁÖ´Â °ÍÀ¸·Î º¸ÀδÙ.
fortune.c
ÆÄÀÏÀ» ÆíÁýÇؼ, 551°ÁÙ°ú 553°ÁÙÀÇ d_namelen¿¡
´ëÇÑ ÂüÁ¶¸¦ d_reclen¿¡ ´ëÇÑ ÂüÁ¶·Î °íÄ¡µµ·Ï ÇÏÀÚ.
´Ù½Ã make all À» ÇØ º¸ÀÚ. ¼º°øÀÌ´Ù.
¿¡·¯ ¾øÀÌ ÄÄÆÄÀϵǾú´Ù. ¿ì¸®´Â ÀÌÁ¦ fortuneÀ¸·ÎºÎÅÍ ½Ñ °ª¿¡ ½º¸±À» ´À³¥ ¼ö
ÀÖ´Ù.