{"id":91,"date":"2017-03-15T16:37:28","date_gmt":"2017-03-15T07:37:28","guid":{"rendered":"http:\/\/shinke1987.net\/?p=91"},"modified":"2017-03-15T16:37:28","modified_gmt":"2017-03-15T07:37:28","slug":"post-91","status":"publish","type":"post","link":"https:\/\/shinke1987.net\/?p=91","title":{"rendered":"ls\u30b3\u30de\u30f3\u30c9\u3082\u3069\u304d\u3002"},"content":{"rendered":"<h2>\u6982\u8981<\/h2>\n<p>C\u8a00\u8a9e\u306e\u52c9\u5f37\u3068\u904a\u3073\u3092\u517c\u306d\u3066\u3002<br \/>\n\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u8b58\u5225\u5b50\u306f<a href=\"https:\/\/en.wikipedia.org\/wiki\/List_of_file_signatures\" target=\"_blank\">\u3053\u3053<\/a>\u3092\u53c2\u8003\u306b\u3057\u305f\u3002<br \/>\nLinux\u4e0a\u3067\u52d5\u304b\u3059\u3053\u3068\u3092\u60f3\u5b9a\u3057\u3066\u3044\u308b\u3002<\/p>\n<h2>\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9<\/h2>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n\/* \r\n * ls\u30b3\u30de\u30f3\u30c9\u306b\u3001\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u304b\u901a\u5e38\u30d5\u30a1\u30a4\u30eb\u304b\u3092\u5224\u5225\u3057\u3066 \r\n * \u8868\u793a\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u3002 \r\n * \u5f15\u6570\u306f1\u500b\u3060\u3051\u3068\u308b\u3002\u305d\u306e\u5f15\u6570\u306fls\u30b3\u30de\u30f3\u30c9\u3067\u901a\u5e38\u4f7f\u308f\u308c\u308b\u3088\u3046\u306a\u3001 \r\n * \u8868\u793a\u3057\u305f\u3044\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u30d1\u30b9\u3002 \r\n * \u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u304b\u901a\u5e38\u30d5\u30a1\u30a4\u30eb\u304bjpg\u30d5\u30a1\u30a4\u30eb\u304b\u3001\u3092\u5224\u5225\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u305f\u3002 \r\n * \u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3067\u3042\u308c\u3070\u3001&#x5B;DIR]\u304c\u982d\u306b\u3064\u304f\u3002 \r\n * jpg\u30d5\u30a1\u30a4\u30eb\u3067\u3042\u308c\u3070\u3001&#x5B;JPG]\u304c\u982d\u306b\u3064\u304f\u3002 \r\n *\/ \r\n  \r\n#include &lt;stdio.h&gt; \r\n#include &lt;stdlib.h&gt; \r\n#include &lt;dirent.h&gt; \r\n#include &lt;string.h&gt; \r\n#include &lt;sys\/types.h&gt; \r\n#include &lt;sys\/stat.h&gt; \r\n  \r\n\/* \r\n * jpg\u30d5\u30a1\u30a4\u30eb\u304b\u3069\u3046\u304b\u3092\u5224\u5225\u3059\u308b\u3002 \r\n * jpg\u30d5\u30a1\u30a4\u30eb\u306a\u30891\u3092\u8fd4\u3059\u3002 \r\n * jpg\u30d5\u30a1\u30a4\u30eb\u3067\u306a\u3044\u306a\u30890\u3092\u8fd4\u3059\u3002 \r\n * \u30d5\u30a1\u30a4\u30eb\u3092\u958b\u304f\u3053\u3068\u306b\u5931\u6557\u3057\u305f\u3089\u3001-1\u3092\u8fd4\u3059\u3002 \r\n *\/ \r\nint chkjpg ( char *path ) { \r\n  FILE *file; \r\n  unsigned char buf&#x5B;3]; \r\n  int i, size; \r\n  \/\/ \u30de\u30b8\u30c3\u30af\u30ca\u30f3\u30d0\u30fc\u3068\u6bd4\u8f03\u3059\u308b\u6642\u306b\u4f7f\u3046\u30ab\u30a6\u30f3\u30c8\u7528\u5909\u6570\u3002 \r\n  unsigned short int count = 0; \r\n  \/\/ jpg\u30d5\u30a1\u30a4\u30eb\u306e\u30de\u30b8\u30c3\u30af\u30ca\u30f3\u30d0\u30fc\uff08\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u8b58\u5225\u5b50\uff09 \r\n  unsigned int jpg_magic&#x5B;3] = { 255, 216, 255 }; \r\n  \r\n  \/\/ \u30d5\u30a1\u30a4\u30eb\u3092\u958b\u304f\u3002 \r\n  file = fopen( path, &quot;rb&quot; ); \r\n  if ( file == NULL ) { \r\n    perror( &quot;fopen&quot; ); \r\n    return -1; \r\n  } \r\n  \r\n  size = fread( buf, 1, 3, file ); \r\n  \r\n\/* \r\n  printf( &quot;** chkjpg &quot; ); \r\n  for ( i = 0; i &lt; size; i++ ) { \r\n    printf( &quot;%02x&quot;, buf&#x5B;i] ); \r\n  } \r\n  printf( &quot;\\n&quot; ); \r\n  printf( &quot;**chkjpg2 &quot; ); \r\n  for (i = 0; i &lt; size; i++ ) { \r\n    printf( &quot;%d&quot;, buf&#x5B;i] ); \r\n  } \r\n  printf( &quot;\\n&quot; ); \r\n  printf( &quot;**chkjpg_compare &quot; ); \r\n  for ( i = 0; i &lt; size; i++ ) { \r\n    if ( buf&#x5B;i] == magic&#x5B;i] ) { \r\n      printf( &quot;OK!&quot; ); \r\n    } \r\n  } \r\n  printf( &quot;\\n&quot; ); \r\n*\/ \r\n  \/\/ \u30de\u30b8\u30c3\u30af\u30ca\u30f3\u30d0\u30fc\u3092\u6bd4\u8f03\u3059\u308b\u3002\u6b63\u3057\u3051\u308c\u3070count\u5909\u6570\u306e\u5024\u304c\u5897\u3048\u308b\u3002 \r\n  for ( i = 0; i &lt; size; i++ ) { \r\n    if ( buf&#x5B;i] == jpg_magic&#x5B;i] ) { \r\n      count++; \r\n    } \r\n  } \r\n  \r\n  fclose( file ); \r\n  if ( count == size ) { \r\n    return 1; \r\n  } else {  \r\n    return 0; \r\n  } \r\n} \r\n  \r\nint main ( int argc, char *argv&#x5B;] ) \r\n{ \r\n  \/\/ DIR\u578b\u306fdirent.h\u3092include\u3059\u308b\u3068\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308b\u3002 \r\n  \/\/ \u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u6307\u3057\u793a\u3059\u3002 \r\n  DIR *dir; \r\n  \/\/ \u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u4e2d\u306b\u3042\u308b\u30d5\u30a1\u30a4\u30eb\u306e\u60c5\u5831\u3092\u4fdd\u6301\u3059\u308b\u3002 \r\n  struct dirent *dp; \r\n  \/\/ dirent\u578b\u3088\u308a\u3082\u3088\u308a\u8a73\u3057\u3044\u30d5\u30a1\u30a4\u30eb\u306e\u60c5\u5831\u3092\u4fdd\u6301\u3059\u308b\u3002 \r\n  struct stat sb; \r\n  \/\/ Debian\u3067\u306e\u30d5\u30a1\u30a4\u30eb\u540d\u306e\u6587\u5b57\u6570\u5236\u9650\u306f255byte\u3089\u3057\u3044\u3002 \r\n  \/\/ NAME_MAX\u306fdirent.h\u3092include\u3059\u308b\u3068\u4f7f\u3048\u308b\u3088\u3046\u306b\u306a\u308b\u3002 \r\n  char path&#x5B;NAME_MAX * 2]; \r\n  \/\/ \u4e00\u6642\u7684\u306b\u30d1\u30b9\u3092\u683c\u7d0d\u3059\u308b\u3002\u6700\u7d42\u7684\u306b\u7d76\u5bfe\u30d1\u30b9\u304c\u5165\u308b\u3002 \r\n  char tmp_path&#x5B;NAME_MAX * 2]; \r\n  \r\n  \/* \r\n   * \u5f15\u6570\u304c\u3042\u308b\u304b\u3069\u3046\u304b\u3092\u5224\u65ad\u3057\u3066\u3001\u7121\u3044\u306a\u3089\u30ab\u30ec\u30f3\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092path\u306b\u4fdd\u5b58\u3002 \r\n   * \u5f15\u6570\u304c\u3042\u308b\u306a\u3089\u3001\u305d\u306e\u5f15\u6570\u3092path\u306b\u4fdd\u5b58\u3002 \r\n   *\/ \r\n  if ( argc &lt;= 1 ) { \r\n    strcpy( path, &quot;.&quot; ); \r\n  } else { \r\n    strcpy( path, argv&#x5B;1] ); \r\n  } \r\n  \r\n  \/\/ \u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u958b\u304f\u3002 \r\n  dir = opendir( path ); \r\n  if ( dir == NULL ) { \r\n    perror( &quot;opendir&quot; ); \r\n    \/\/ exit\u95a2\u6570\u306fstdlib.h\u3067\u5b9a\u7fa9\u3055\u308c\u3066\u3044\u308b\u3002 \r\n    exit( EXIT_FAILURE ); \r\n  } \r\n  \r\n  \/\/ \u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u4e2d\u306e\u30d5\u30a1\u30a4\u30eb\u3092\u898b\u3066\u3044\u304f\u3002 \r\n  for ( dp = readdir ( dir ); dp != NULL; dp = readdir ( dir ) ) { \r\n    \/\/ stat\u95a2\u6570\u306b\u7d76\u5bfe\u30d1\u30b9\u304c\u6e21\u308b\u3088\u3046\u306b\u3059\u308b\u3002 \r\n    strcpy( tmp_path, path ); \r\n    strcat( tmp_path, &quot;\/&quot; ); \r\n    strcat( tmp_path, dp -&gt; d_name ); \r\n    if ( stat( tmp_path, &amp;sb ) == -1 ) { \r\n      \/\/ perror( &quot;stat&quot; ); \r\n      \/\/ closedir( dir ); \r\n      \/\/ exit( EXIT_FAILURE ); \r\n      continue; \r\n    } \r\n  \r\n    \/\/ \u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u304b\u901a\u5e38\u30d5\u30a1\u30a4\u30eb\u304b\u3092\u5224\u5225\u3002 \r\n    if ( S_ISDIR( sb.st_mode ) ) { \r\n      printf(&quot;&#x5B;DIR]&quot;); \r\n    } else if ( chkjpg ( tmp_path ) ) { \/\/ jpg\u30d5\u30a1\u30a4\u30eb\u304b\u901a\u5e38\u30d5\u30a1\u30a4\u30eb\u304b\u3092\u5224\u5225\u3002 \r\n      printf(&quot;&#x5B;JPG]&quot;); \r\n    } \r\n  \r\n    \/\/ \u30d5\u30a1\u30a4\u30eb\u540d\u3092\u8868\u793a\u3002 \r\n    printf( &quot;name : %s\\n&quot;, dp -&gt; d_name ); \r\n  } \r\n  \r\n  closedir( dir ); \r\n  exit( EXIT_SUCCESS ); \r\n} \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 C\u8a00\u8a9e\u306e\u52c9\u5f37\u3068\u904a\u3073\u3092\u517c\u306d\u3066\u3002 \u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u8b58\u5225\u5b50\u306f\u3053\u3053\u3092\u53c2\u8003\u306b\u3057\u305f\u3002 Linux\u4e0a\u3067\u52d5\u304b\u3059\u3053\u3068\u3092\u60f3\u5b9a\u3057\u3066\u3044\u308b\u3002 \u30bd\u30fc\u30b9\u30b3\u30fc\u30c9 \/* * ls\u30b3\u30de\u30f3\u30c9\u306b\u3001\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u304b\u901a\u5e38\u30d5\u30a1\u30a4\u30eb\u304b\u3092\u5224\u5225\u3057\u3066 * \u8868\u793a\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[14,24],"class_list":["post-91","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-linux"],"_links":{"self":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/91","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=91"}],"version-history":[{"count":3,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/91\/revisions"}],"predecessor-version":[{"id":94,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/91\/revisions\/94"}],"wp:attachment":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}