当前位置: 技术问答>linux和unix
在linux底下如何用c语言来实现“获得所有磁盘列表”这个功能的编程,谢谢!
来源: 互联网 发布时间:2016-02-05
本文导语: 请高手指点,有源码更好,谢谢! | #define PROC_PARTITIONS "/proc/partitions" /* * for fdisk -l: * try all things in /proc/partitions that look like a full disk */ static void tryprocpt(void) { FILE...
请高手指点,有源码更好,谢谢!
|
#define PROC_PARTITIONS "/proc/partitions"
/*
* for fdisk -l:
* try all things in /proc/partitions that look like a full disk
*/
static void
tryprocpt(void) {
FILE *procpt;
char line[100], ptname[100], devname[120];
int ma, mi, sz;
procpt = fopen(PROC_PARTITIONS, "r");
if (procpt == NULL) {
fprintf(stderr, _("cannot open %sn"), PROC_PARTITIONS);
return;
}
while (fgets(line, sizeof(line), procpt)) {
if (sscanf (line, " %d %d %d %[^n ]",
&ma, &mi, &sz, ptname) != 4)
continue;
snprintf(devname, sizeof(devname), "/dev/%s", ptname);
if (is_probably_full_disk(devname))
try(devname, 0);
}
fclose(procpt);
}
来源于:
util-linux-2.12r/fdisk/fdisk.c
/*
* for fdisk -l:
* try all things in /proc/partitions that look like a full disk
*/
static void
tryprocpt(void) {
FILE *procpt;
char line[100], ptname[100], devname[120];
int ma, mi, sz;
procpt = fopen(PROC_PARTITIONS, "r");
if (procpt == NULL) {
fprintf(stderr, _("cannot open %sn"), PROC_PARTITIONS);
return;
}
while (fgets(line, sizeof(line), procpt)) {
if (sscanf (line, " %d %d %d %[^n ]",
&ma, &mi, &sz, ptname) != 4)
continue;
snprintf(devname, sizeof(devname), "/dev/%s", ptname);
if (is_probably_full_disk(devname))
try(devname, 0);
}
fclose(procpt);
}
来源于:
util-linux-2.12r/fdisk/fdisk.c
|
这不属于c的范畴,c中好象没有提供这要的调用.这属于操作系统的问题.
如果你在linux下,可用查看一个/proc/partitions
这是个内核影响文件.里边保存了系统内的磁盘信息.
如果你在linux下,可用查看一个/proc/partitions
这是个内核影响文件.里边保存了系统内的磁盘信息.
|
#include
int main()
{
system("cat /proc/partitions >> 1.txt");
}
上面将磁盘列表写入1.txt,然后在C里打开1.txt得到
int main()
{
system("cat /proc/partitions >> 1.txt");
}
上面将磁盘列表写入1.txt,然后在C里打开1.txt得到