Читаем Linux программирование в примерах полностью

48  fp = setmntent(filename, "r"); /* только для чтения */

49  if (fp == NULL) {

50   fprintf(stderr, "%s: %s: could not open: %s\n",

51    myname, filename, strerror(errno));

52   exit(1);

53  }

54

55  while ((fs = getmntent(fp)) != NULL)

56   do_statvfs(fs);

57

58  endmntent(fp);

59 }

Строки 1–59 в сущности те же самые, как и для ch08-mounted.c. main() обрабатывает командную стоку, a process() просматривает в цикле каждую смонтированную файловую систему. do_statvfs() осуществляет действительную работу, выводя для каждой интересующей файловой системы struct statvfs.

61  /* do_statvfs --- Использовать statvfs и вывести сведения */

62

63  void do_statvfs(const struct mntent *fs)

64  {

65   struct statvfs vfs;

66

67   if (fs->mnt_fsname[0] != '/') /* пропустить ненастоящие файловые системы */

68    return;

69

70   if (statvfs(fs->mnt_dir, &vfs) != 0) {

71    fprintf(stderr, "%s: %s: statvfs failed: %s\n",

72     myname, fs->mnt_dir, strerror(errno));

73    errors++;

74    return;

75   }

76

77   printf("%s, mounted on %s:\n", fs->mnt_dir, fs->mnt_fsname);

78   printf("\tf_bsize: %ld\n", (long)vfs.f_bsize);

79   printf("\tf_frsize: %ld\n", (long)vfs.f_frsize);

80   printf("\tf_blocks: %lu\n", (unsigned long)vfs.f_blocks);

81   printf("\tf_bfree: %lu\n", (unsigned long)vfs.f_bfree);

82   printf("\tf_bavail: %lu\n", (unsigned long)vfs.f_bavail);

83   printf("\tf_files: %lu\n", (unsigned long)vfs.f_files);

84   printf("\tf_ffree: %lu\n", (unsigned long)vfs.f_ffree);

85   printf("\tf_favail: %lu\n", (unsigned long)vfs.f_favail);

86   printf("\tf_fsid: %#lx\n", (unsigned long)vfs.f_fsid);

87

88   printf("\tf_flag: ");

89   if (vfs.f_flag == 0)

90    printf("(none)\n");

91   else {

92    if ((vfs.f_flag & ST_RDONLY) != 0)

93     printf("ST_RDONLY ");

94    if ((vfs.f_flag & ST_NOSUID) != 0)

95     printf("ST_NOSUID");

96    printf("\n");

97   }

98

99   printf("\tf_namemax: %#ld\n", (long)vfs.f_namemax);

100 }

Строки 67–68 пропускают файловые системы, которые не основываются на реальных дисковых устройствах. Это означает, что файловые системы типа /proc или /dev/pts игнорируются. (Правда, эта проверка эвристическая, но она работает: в /etc/mtab смонтированные устройства перечислены по полному пути устройства: например, /dev/hda1.) Строка 70 вызывает statvfs() с соответствующей проверкой ошибок, а строки 77-99 выводят сведения.

Строки 89–96 имеют дело с флагами: отдельные биты информации, которые присутствуют или не присутствуют. Обсуждение того, как биты флагов используются в коде С, см. во врезке. Вот вывод ch08-statvfs:

$ ch08-statvfs /* Запуск программы */

/, mounted on /dev/hda2: /* Результаты для файловой системы ext2 */

f_bsize: 4096

f_frsize: 4096

f_blocks: 1549609

f_bfree: 316663

f_bavail: 237945

f_files: 788704

f_ffree: 555482

f_favail: 555482

f_fsid: 0

f_flag: (none)

f_namemax: 255

...

/win, mounted on /dev/hda1: /* Результаты для файл. системы vfat */

f_bsize: 4096

f_frsize: 4096

f_blocks: 2092383

f_bfree: 1391952

Перейти на страницу:

Похожие книги

C++ Primer Plus
C++ Primer Plus

C++ Primer Plus is a carefully crafted, complete tutorial on one of the most significant and widely used programming languages today. An accessible and easy-to-use self-study guide, this book is appropriate for both serious students of programming as well as developers already proficient in other languages.The sixth edition of C++ Primer Plus has been updated and expanded to cover the latest developments in C++, including a detailed look at the new C++11 standard.Author and educator Stephen Prata has created an introduction to C++ that is instructive, clear, and insightful. Fundamental programming concepts are explained along with details of the C++ language. Many short, practical examples illustrate just one or two concepts at a time, encouraging readers to master new topics by immediately putting them to use.Review questions and programming exercises at the end of each chapter help readers zero in on the most critical information and digest the most difficult concepts.In C++ Primer Plus, you'll find depth, breadth, and a variety of teaching techniques and tools to enhance your learning:• A new detailed chapter on the changes and additional capabilities introduced in the C++11 standard• Complete, integrated discussion of both basic C language and additional C++ features• Clear guidance about when and why to use a feature• Hands-on learning with concise and simple examples that develop your understanding a concept or two at a time• Hundreds of practical sample programs• Review questions and programming exercises at the end of each chapter to test your understanding• Coverage of generic C++ gives you the greatest possible flexibility• Teaches the ISO standard, including discussions of templates, the Standard Template Library, the string class, exceptions, RTTI, and namespaces

Стивен Прата

Программирование, программы, базы данных