반응형

 

 

디바이스 드라이버를 insmod 해서 올릴 때마다 디바이스 파일의 전체 경로의 중간 이름이 자꾸 바뀌어서 수동으로 계속 입력하기 힘들어서 간만에 시스템 프로그래밍 책을 꺼내 들고 짜봤습니다.

/sys/bus/w1/devices/28-000005e41463/w1_slave

 

/sys/bus/w1/devices/ 경로 아래에서 28-로 시작하는 엔트리를 읽어온 후 문자열을 조합하면 되는 군요. 진짜 간만에 하는 거라 좀 어설플지 모르지만 의도한대로 잘 동작합니다.

  1. #include <sys/types.h>  
  2. #include <dirent.h>  
  3. #include <stdio.h>  
  4. #include <stdlib.h>  
  5. #include <string.h>  
  6.     
  7.     
  8. char* findDeviceName(char *path, char *devicename)  
  9. {  
  10.     struct dirent *entry;  
  11.     DIR *dir;  
  12.     char* fulldevicename=malloc(1024);  
  13.     
  14.     
  15.     dir = opendir(path);  
  16.     
  17.     while( (entry = readdir(dir)) != NULL )  
  18.     {  
  19.         if ( strncmp( entry->d_name, devicename, strlen(devicename)  ) == 0 )  
  20.         {  
  21.             strcpy( fulldevicename, entry->d_name );  
  22.     
  23.             closedir(dir);  
  24.             return fulldevicename;  
  25.         }  
  26.     }  
  27.     
  28.     if ( !entry )  
  29.         perror( "readdir" );  
  30.     closedir(dir);  
  31.     
  32.     return NULL;  
  33. }  
  34.     
  35.     
  36. int main()  
  37. {  
  38.     char *path="/sys/bus/w1/devices/";  
  39.     char *fulldevicename;  
  40.     char fullpath[1024];  
  41.     
  42.     if ( (fulldevicename=findDeviceName(path, "28-" )) != NULL )  
  43.     {  
  44.         strcpy( fullpath, path );  
  45.         strcat( fullpath, fulldevicename );         
  46.         strcat( fullpath, "/w1_slave" );  
  47.         printf( "%s\n", fullpath );  
  48.     }  
  49. }  

 

 

반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts