'closedir'에 해당되는 글 2건

  1. 2007/05/09 Real root device 찾기
  2. 2007/05/08 scandir 예제
어떤 파일이나 디렉토리가 있습니다.
그런데 이것이 어떤 device 에 존재하는지 찾고 싶습니다.
그렇때 어떻게 할까요?
그에 대한 약간의 힌트를 예제로 만들어 보았습니다.

코드:
/*
Copyright (C) Information Equipment co.,LTD.
All rights reserved.
Code by JaeHyuk Cho <mailto:minzkn@infoeq.com>
CVSTAG="$Header$"

Simple is best !

WARNING: 이 코드는 버퍼오버플로우의 위약점을 고려하지 않았습니다.
*/

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <mntent.h>

#define def_max_buffer_size (4 << 10)

static char * (mz_real_root_device)(char *s_buffer, const char *s_path, const char *s_device_path)
{
char *s_result = (char *)0;
char s_local_path[ def_max_buffer_size ];
struct stat s_stat_path, s_stat_device;
dev_t s_device_node;
DIR *s_directory;
struct dirent *s_directory_entry;
if(stat(s_path, (struct stat *)(&s_stat_path)) == 0)
{
  if(s_stat_path.st_rdev != ((dev_t)0))s_device_node = s_stat_path.st_rdev;
  else s_device_node = s_stat_path.st_dev;
  s_directory = opendir(s_device_path);
  if(s_directory != ((DIR *)0))
  {
   do
   {
    s_directory_entry = readdir(s_directory);
    if(s_directory_entry == ((struct dirent *)0))break;
    if((strcmp(s_directory_entry->d_name, ".") == 0) || (strcmp(s_directory_entry->d_name, "..") == 0))continue;
    (void)sprintf((char *)(&s_local_path[0]), "%s/%s", s_device_path, s_directory_entry->d_name);
    if(stat((char *)(&s_local_path[0]), (struct stat *)(&s_stat_device)) == 0)
    {
     if(S_ISBLK(s_stat_device.st_mode) != 0)
     {
      if(s_stat_device.st_rdev == s_device_node)
      {
       s_result = strcpy(s_buffer, (char *)(&s_local_path[0]));
       break;
      }
     }
#if 1 /* 하위 dev 검색 : 안하는게 더 좋을지도 */
     else if(S_ISDIR(s_stat_device.st_mode) != 0)
     {
      if(lstat((char *)(&s_local_path[0]), (struct stat *)(&s_stat_device)) == 0)
      { /* do not follow link */
       if(S_ISDIR(s_stat_device.st_mode) != 0)s_result = mz_real_root_device(s_buffer, s_path, (char *)(&s_local_path[0]));
      }
     }
#endif
    }
   }while(s_result == ((char *)0));
   (void)closedir(s_directory);
  }
}
return(s_result);
}

int main(int s_argc, char **s_argv)
{
int s_index;
char s_buffer[ def_max_buffer_size ];
char *s_real_root_device;
if(s_argc >= 2)
{
  for(s_index = 1;s_index < s_argc;s_index++)
  {
   s_real_root_device = mz_real_root_device((char *)(&s_buffer[0]), s_argv[s_index], "/dev");
   (void)fprintf(stdout, "\"\x1b[1;33m%s\x1b[0m\" on \"%s\"\n",
    s_real_root_device ? s_real_root_device : "none", s_argv[s_index]);
   (void)fflush(stdout);
  }
}
else (void)fprintf(stdout, "usage: %s <pathname> [...]\n", s_argv[0]);
return(0);
}

/* End of source */
크리에이티브 커먼즈 라이센스
Creative Commons License
Posted by minzkn

트랙백 주소 :: http://blog.minzkn.com/trackback/100

댓글을 달아 주세요