c通过system取得分区信息
1 #include <fcntl.h>
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <assert.h>
10
11 //level:
12 // 0:major print,call path
13 // 1:more detailed print
14 // 2:most detailed print ,or not used
15 #define LOCAL_DBG_PRINT_ENABLE 1
16 #define CUR_DBG_PRINT_LEVEL 1
17 #define LOCAL_DBG_PRINT_PRIFIX "[parse_fdisk]"
18
19 #if LOCAL_DBG_PRINT_ENABLE
20 #define DBG_PRINT(dbgLevel,string,...)\
21 {\
22 if(CUR_DBG_PRINT_LEVEL>=dbgLevel)\
23 printf("[Test]" LOCAL_DBG_PRINT_PRIFIX string,__VA_ARGS__); \
24 }
25 #define DBG_PRINT(dbgLevel,...)\
26 {\
27 if(CUR_DBG_PRINT_LEVEL>=dbgLevel)\
28 printf("[Test]" LOCAL_DBG_PRINT_PRIFIX __VA_ARGS__); \
29 }
30 #else
31 #define DBG_PRINT(...)
32 #endif
33
34 #define PATITION_NAME_LEN 32
35 struct Disk_Partition
36 {
37 char name[PATITION_NAME_LEN];
38 unsigned int start;
39 unsigned int end;
40 unsigned int size;
41 char type;
42 };
43 #ifndef NULL
44 #define NULL ((void*)0)
45 #endif
46 int main()
47 {
48 struct Disk_Partition dp[4];
49 memset(dp,0,sizeof(struct Disk_Partition)*4);
50
51 FILE *fp = NULL;
52 int part_len = 0;
53 char* head="fdisk -l ";
54 char* disk="/dev/sdb";
55 char cmd[24] = {0};
56 char buf[128] = {0};
57 char *cp,*lineend,*result;
58 assert(disk);
59
60 sprintf((char*)&cmd,"%s %s",head,disk);
61 if((fp = popen((char*)&cmd, "r")) == NULL) {
62 perror("Fail to popen!\n");
63 return -1;
64 }
65
66 const char* delims = " ";
67 int i = 0,j=0;
68 while(fgets(buf, sizeof(buf), fp) != NULL) {
69 lineend = strchr(buf, '\n');
70 if (!lineend)
71 break;
72 if((cp = strstr(buf,disk)) == NULL)
73 continue;
74 if(*(cp+strlen(disk))==':')
75 continue;
76 result = strtok(cp,delims);
77 i = 0;
78 while(result)
79 {
80 switch(i)
81 {
82 case 0:
83 memcpy((char*)&dp[part_len].name,result,strlen(result));
84 DBG_PRINT(1,"name = %s \n" ,(char*)&dp[part_len].name);
85 break;
86 case 1:
87 dp[part_len].start = strtoul(result,NULL,0);
88 DBG_PRINT(1,"start = %d \n",dp[part_len].start);
89 break;
90 case 2:
91 dp[part_len].end = strtoul(result,NULL,0);
92 DBG_PRINT(1,"end = %d \n",dp[part_len].end);
93 break;
94 case 3:
95 dp[part_len].size = strtoul(result,NULL,0);
96 DBG_PRINT(1,"size = %d \n",dp[part_len].size);
97 break;
98 case 4:
99 j=atoi(result);
100 if(j==5)
101 dp[part_len].type = 'E';
102 else if(j == 83)
103 dp[part_len].type = 'L';
104 DBG_PRINT(1,"type = %c \n",dp[part_len].type);
105 break;
106 default:
107 DBG_PRINT(1,"i=%d, result is \"%s\"\n",i, result );
108 break;
109 }
110 i++;
111 result = strtok(NULL,delims);
112
113 }
114 DBG_PRINT(1,"=========================================\n");
115 part_len++;
116
117
118 }
119 DBG_PRINT(1,"================parse end=========\n");
120 pclose(fp);
121 return 0;
122 }
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <assert.h>
10
11 //level:
12 // 0:major print,call path
13 // 1:more detailed print
14 // 2:most detailed print ,or not used
15 #define LOCAL_DBG_PRINT_ENABLE 1
16 #define CUR_DBG_PRINT_LEVEL 1
17 #define LOCAL_DBG_PRINT_PRIFIX "[parse_fdisk]"
18
19 #if LOCAL_DBG_PRINT_ENABLE
20 #define DBG_PRINT(dbgLevel,string,...)\
21 {\
22 if(CUR_DBG_PRINT_LEVEL>=dbgLevel)\
23 printf("[Test]" LOCAL_DBG_PRINT_PRIFIX string,__VA_ARGS__); \
24 }
25 #define DBG_PRINT(dbgLevel,...)\
26 {\
27 if(CUR_DBG_PRINT_LEVEL>=dbgLevel)\
28 printf("[Test]" LOCAL_DBG_PRINT_PRIFIX __VA_ARGS__); \
29 }
30 #else
31 #define DBG_PRINT(...)
32 #endif
33
34 #define PATITION_NAME_LEN 32
35 struct Disk_Partition
36 {
37 char name[PATITION_NAME_LEN];
38 unsigned int start;
39 unsigned int end;
40 unsigned int size;
41 char type;
42 };
43 #ifndef NULL
44 #define NULL ((void*)0)
45 #endif
46 int main()
47 {
48 struct Disk_Partition dp[4];
49 memset(dp,0,sizeof(struct Disk_Partition)*4);
50
51 FILE *fp = NULL;
52 int part_len = 0;
53 char* head="fdisk -l ";
54 char* disk="/dev/sdb";
55 char cmd[24] = {0};
56 char buf[128] = {0};
57 char *cp,*lineend,*result;
58 assert(disk);
59
60 sprintf((char*)&cmd,"%s %s",head,disk);
61 if((fp = popen((char*)&cmd, "r")) == NULL) {
62 perror("Fail to popen!\n");
63 return -1;
64 }
65
66 const char* delims = " ";
67 int i = 0,j=0;
68 while(fgets(buf, sizeof(buf), fp) != NULL) {
69 lineend = strchr(buf, '\n');
70 if (!lineend)
71 break;
72 if((cp = strstr(buf,disk)) == NULL)
73 continue;
74 if(*(cp+strlen(disk))==':')
75 continue;
76 result = strtok(cp,delims);
77 i = 0;
78 while(result)
79 {
80 switch(i)
81 {
82 case 0:
83 memcpy((char*)&dp[part_len].name,result,strlen(result));
84 DBG_PRINT(1,"name = %s \n" ,(char*)&dp[part_len].name);
85 break;
86 case 1:
87 dp[part_len].start = strtoul(result,NULL,0);
88 DBG_PRINT(1,"start = %d \n",dp[part_len].start);
89 break;
90 case 2:
91 dp[part_len].end = strtoul(result,NULL,0);
92 DBG_PRINT(1,"end = %d \n",dp[part_len].end);
93 break;
94 case 3:
95 dp[part_len].size = strtoul(result,NULL,0);
96 DBG_PRINT(1,"size = %d \n",dp[part_len].size);
97 break;
98 case 4:
99 j=atoi(result);
100 if(j==5)
101 dp[part_len].type = 'E';
102 else if(j == 83)
103 dp[part_len].type = 'L';
104 DBG_PRINT(1,"type = %c \n",dp[part_len].type);
105 break;
106 default:
107 DBG_PRINT(1,"i=%d, result is \"%s\"\n",i, result );
108 break;
109 }
110 i++;
111 result = strtok(NULL,delims);
112
113 }
114 DBG_PRINT(1,"=========================================\n");
115 part_len++;
116
117
118 }
119 DBG_PRINT(1,"================parse end=========\n");
120 pclose(fp);
121 return 0;
122 }