{"id":3323,"date":"2014-07-30T00:40:49","date_gmt":"2014-07-29T15:40:49","guid":{"rendered":"http:\/\/pchero21.com\/?p=3323"},"modified":"2014-07-30T00:47:45","modified_gmt":"2014-07-29T15:47:45","slug":"dirent-stat","status":"publish","type":"post","link":"http:\/\/pchero21.com\/?p=3323","title":{"rendered":"dirent, stat"},"content":{"rendered":"<p>\uc774\ubc88\uc5d0 dirent \uc640 stat \uc744 \uc774\uc6a9\ud558\uc5ec \uc791\uc5c5\uc744 \ud558\uba74\uc11c \uc54c\uac8c\ub41c \ub0b4\uc6a9\ub4e4\uc744 \uac04\ub2e8\ud558\uac8c \uc815\ub9ac\ud588\ub2e4.<\/p>\n<p><strong>dirent<\/strong><\/p>\n<p>dirent \ub294 \ub514\ub809\ud1a0\ub9ac \uc815\ubcf4\ub97c \ub2f4\ub294 \uad6c\uc870\uccb4\uc774\ub2e4.<br \/>\ndirent \uc640 scandir() \uc744 \uc0ac\uc6a9\ud558\uba74 \ub514\ub809\ud1a0\ub9ac\uc5d0 \uc5b4\ub5a4 \ud30c\uc77c\ub4e4\uc774 \uc788\ub294\uc9c0 \uac04\ub7b5\ud558\uac8c \uc54c \uc218 \uc788\ub294 \uc18d\uc131\uacfc \ud30c\uc77c \ub9ac\uc2a4\ud2b8 \ud3ec\uc778\ud130\ub97c \uc774\uc6a9\ud560 \uc218 \uc788\ub2e4.<\/p>\n<pre class=\"brush:cpp\">struct dirent\r\n{\r\n#ifndef __USE_FILE_OFFSET64\r\n    __ino_t d_ino;\r\n    __off_t d_off;\r\n#else\r\n    __ino64_t d_ino;\r\n    __off64_t d_off;\r\n#endif\r\n    unsigned short int d_reclen;\r\n    unsigned char d_type;\r\n    char d_name[256];\t\t\/* We must not include limits.h! *\/\r\n};\r\n<\/pre>\n<p>\uad6c\uc870\uccb4 \ub0b4\uc6a9 \uc911, d_type \uc73c\ub85c \ud30c\uc77c\uc758 \uc18d\uc131\uc744 \ud30c\uc545\ud560 \uc218 \uc788\ub294\ub370, \uc885\ub958\ub294 \ub2e4\uc74c\uacfc \uac19\ub2e4.<\/p>\n<pre class=\"brush:cpp\">#ifdef __USE_BSD\r\n\/* File types for `d_type'.  *\/\r\nenum\r\n  {\r\n    DT_UNKNOWN = 0,\r\n# define DT_UNKNOWN\tDT_UNKNOWN\r\n    DT_FIFO = 1,\r\n# define DT_FIFO\tDT_FIFO\r\n    DT_CHR = 2,\r\n# define DT_CHR\t\tDT_CHR\r\n    DT_DIR = 4,\r\n# define DT_DIR\t\tDT_DIR\r\n    DT_BLK = 6,\r\n# define DT_BLK\t\tDT_BLK\r\n    DT_REG = 8,\r\n# define DT_REG\t\tDT_REG\r\n    DT_LNK = 10,\r\n# define DT_LNK\t\tDT_LNK\r\n    DT_SOCK = 12,\r\n# define DT_SOCK\tDT_SOCK\r\n    DT_WHT = 14\r\n# define DT_WHT\t\tDT_WHT\r\n  };\r\n\r\n\/* Convert between stat structure types and directory types.  *\/\r\n# define IFTODT(mode)\t(((mode) &amp; 0170000) &gt;&gt; 12)\r\n# define DTTOIF(dirtype)\t((dirtype) &lt;&lt; 12)\r\n#endif<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>stat<\/strong><\/p>\n<p>stat \uc740 \ud30c\uc77c \uc18d\uc131\uc744 \uac00\uc838\uc624\ub294 \ud568\uc218\uc774\uba70, \uad6c\uc870\uccb4\uc774\ub2e4. stat \uc744 \ud638\ucd9c\ud558\uba74, \uc785\ub825\ud55c \ud30c\uc77c\uc5d0 \ub300\ud55c \uc18d\uc131\ub4e4\uc774 struct stat \uad6c\uc870\uccb4\uc5d0 \uc785\ub825\ub41c\ub2e4.<\/p>\n<pre class=\"brush:cpp\">struct stat {\r\n   dev_t     st_dev;     \/* ID of device containing file *\/\r\n   ino_t     st_ino;     \/* inode number *\/\r\n   mode_t    st_mode;    \/* protection *\/\r\n   nlink_t   st_nlink;   \/* number of hard links *\/\r\n   uid_t     st_uid;     \/* user ID of owner *\/\r\n   gid_t     st_gid;     \/* group ID of owner *\/\r\n   dev_t     st_rdev;    \/* device ID (if special file) *\/\r\n   off_t     st_size;    \/* total size, in bytes *\/\r\n   blksize_t st_blksize; \/* blocksize for filesystem I\/O *\/\r\n   blkcnt_t  st_blocks;  \/* number of 512B blocks allocated *\/\r\n   time_t    st_atime;   \/* time of last access *\/\r\n   time_t    st_mtime;   \/* time of last modification *\/\r\n   time_t    st_ctime;   \/* time of last status change *\/\r\n};\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Eaxmple<\/strong><\/p>\n<p>\uc544\ub798\ub294 stat \uacfc scandir \uc744 \uc774\uc6a9\ud55c \uc608\uc81c \ud504\ub85c\uadf8\ub7a8\uc774\ub2e4.<br \/>\n\ud604\uc7ac \ub514\ub809\ud1a0\ub9ac \ub0b4\uc5d0 \uc788\ub294 \ubaa8\ub4e0 \ud30c\uc77c\ub4e4\uc744 \ucd5c\uadfc \uc218\uc815\ud55c \uc21c\uc11c\ub300\ub85c \uac80\uc0c9\ud558\uba70 \uac01\uac01\uc758 \ud30c\uc77c\uba85\uacfc \uc0ac\uc774\uc988(Byte)\ub97c \ud45c\uc2dc\ud55c\ub2e4.<br \/>\n\ub9cc\uc57d, \ub514\ub809\ud1a0\ub9ac \uc548\uc5d0 \uc788\ub294 \ud30c\uc77c\ub4e4\uc758 \uc6a9\ub7c9\uc758 10M\uac00 \ub118\uc5b4\uac00\uba74, \uadf8 \uc774\ud6c4\ubd80\ud130\ub294 \ud30c\uc77c\uc744 \uc0ad\uc81c\ud55c\ub2e4.<\/p>\n<p>\uc989, \ub514\ub809\ud1a0\ub9ac\uc5d0 \uc788\ub294 \ud30c\uc77c\ub4e4\uc758 \uc6a9\ub7c9\uc774 10M\uac00 \ub118\uc5b4\uac00\uba74 \uc624\ub798\ub41c \ud30c\uc77c\ubd80\ud130 \uc0ad\uc81c\ud558\ub294 \uac83\uc774\ub2e4.<\/p>\n<p>\uadf8\ub9ac\uace0 \ud30c\uc77c\uc744 \uac80\uc0c9\ud558\ub294 \ub3c4\uc911 \ud30c\uc77c\uba85\uc5d0 .wav(wav \ud30c\uc77c)\uc774 \ub4e4\uc5b4\uc788\uc73c\uba74 &#8220;Found wav file&#8221; \uc744 \ud45c\uc2dc\ud55c\ub2e4.<\/p>\n<pre class=\"brush:cpp\">#include &lt;dirent.h&gt;\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;sys\/types.h&gt;\r\n#include &lt;sys\/stat.h&gt;\r\n#include &lt;unistd.h&gt;\r\n#include &lt;string.h&gt;\r\n#include &lt;errno.h&gt;\r\n\r\n#define TARGET_DIR \".\"\r\n#define LIMIT_SIZE 10485760 \/\/ 10M\r\n\r\nint file_select( const struct dirent *d );\r\nint cmp_time(const struct dirent **e1, const struct dirent **e2);\r\n\r\n\r\nint main(int argc, char** argv)\r\n{\r\n    struct dirent **namelist;\r\n    struct stat tmp_stat;\r\n    unsigned long total_size;\r\n    char* pszFilename;\r\n    int iCnt;\r\n    int i;\r\n    int iRet;\r\n    char* pRet;\r\n\r\n    iCnt = scandir(TARGET_DIR, &amp;namelist, file_select, cmp_time);\r\n    if(iCnt &lt; 0)\r\n    {\r\n        printf(\"Could not scanning.. err:%d, msg:%s\\n\", errno, strerror(errno));\r\n        return 0;\r\n    }\r\n\r\n    total_size = 0;\r\n    printf(\"Scan files:%d\\n\", iCnt);\r\n    for(i = 0; i &lt; iCnt; i++)\r\n    {\r\n        asprintf(&amp;pszFilename, \"%s\/%s\", TARGET_DIR, namelist[i]-&gt;d_name);\r\n        stat(pszFilename, &amp;tmp_stat);\r\n        printf(\"filename: %s, size:%ld\\n\", pszFilename, tmp_stat.st_size);\r\n        pRet = strstr(namelist[i]-&gt;d_name, \".wav\");\r\n        if(pRet != NULL)\r\n        {\r\n            printf(\"Found wav file!\\n\");\r\n        }\r\n\r\n        total_size += tmp_stat.st_size;\r\n        if(total_size &gt; LIMIT_SIZE)\r\n        {\r\n            total_size -= tmp_stat.st_size;\r\n            \r\n            printf(\"Delete limit overed files... name: %s\\n\", pszFilename);\r\n            iRet = remove(pszFilename);\r\n            if(iRet == -1)\r\n            {\r\n                printf(\"Err! Could not delete file. name:%s, err:%d, msg:%s\\n\", pszFilename, errno, strerror(errno));\r\n            }\r\n        }\r\n        \r\n        free(pszFilename);\r\n        free(namelist[i]);\r\n    }\r\n    \r\n    free(namelist);\r\n    \r\n    printf(\"totalSize %lu\\n\", total_size);\r\n    \r\n    return 0;\r\n}\r\n\r\n\/**\r\n * \r\n *\/\r\nint file_select(const struct dirent *entry)\r\n{\r\n    if(entry-&gt;d_type == DT_DIR)\r\n    {\r\n        return 0;\r\n    }\r\n\r\n    return 1;\r\n}\r\n\r\n\/**\r\n *\r\n *\/\r\nint cmp_time(const struct dirent **e1, const struct dirent **e2)\r\n{\r\n    int iRet;\r\n    struct stat sb1;\r\n    struct stat sb2;\r\n    char* filename_e1;\r\n    char* filename_e2;\r\n    \r\n    asprintf(&amp;filename_e1, \"%s\/%s\", TARGET_DIR, (*e1)-&gt;d_name);\r\n    asprintf(&amp;filename_e2, \"%s\/%s\", TARGET_DIR, (*e2)-&gt;d_name);\r\n        \r\n    iRet = stat(filename_e1, &amp;sb1);\r\n    if(iRet == -1)\r\n    {\r\n        printf(\"Error occured! name:%s, err:%d, msg:%s\\n\", filename_e1, errno, strerror(errno));\r\n\r\n        free(filename_e1);\r\n        free(filename_e2);\r\n        return -1;\r\n    }\r\n    \r\n    iRet = stat(filename_e2, &amp;sb2);\r\n    if(iRet == -1)\r\n    {\r\n        printf(\"Error occured! Err:%d, Msg:%s\\n\", errno, strerror(errno));\r\n\r\n        free(filename_e1);\r\n        free(filename_e2);\r\n        return -1;\r\n    }\r\n    \r\n    free(filename_e1);\r\n    free(filename_e2);\r\n    \r\n    return sb1.st_mtime &lt; sb2.st_mtime;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\uc774\ubc88\uc5d0 dirent \uc640 stat \uc744 \uc774\uc6a9\ud558\uc5ec \uc791\uc5c5\uc744 \ud558\uba74\uc11c \uc54c\uac8c\ub41c \ub0b4\uc6a9\ub4e4\uc744 \uac04\ub2e8\ud558\uac8c \uc815\ub9ac\ud588\ub2e4. dirent dirent \ub294 \ub514\ub809\ud1a0\ub9ac \uc815\ubcf4\ub97c \ub2f4\ub294 \uad6c\uc870\uccb4\uc774\ub2e4. dirent \uc640 scandir() \uc744 \uc0ac\uc6a9\ud558\uba74 \ub514\ub809\ud1a0\ub9ac\uc5d0 \uc5b4\ub5a4 \ud30c\uc77c\ub4e4\uc774 \uc788\ub294\uc9c0 \uac04\ub7b5\ud558\uac8c \uc54c \uc218 \uc788\ub294 \uc18d\uc131\uacfc \ud30c\uc77c \ub9ac\uc2a4\ud2b8 \ud3ec\uc778\ud130\ub97c \uc774\uc6a9\ud560 \uc218 \uc788\ub2e4. struct &hellip; <a href=\"http:\/\/pchero21.com\/?p=3323\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[823,824,825],"_links":{"self":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/posts\/3323"}],"collection":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3323"}],"version-history":[{"count":9,"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/posts\/3323\/revisions"}],"predecessor-version":[{"id":3352,"href":"http:\/\/pchero21.com\/index.php?rest_route=\/wp\/v2\/posts\/3323\/revisions\/3352"}],"wp:attachment":[{"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3323"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/pchero21.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}