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

scandir() читает каталог, имя которого дано в dir, создает с использованием malloc() массив указателей struct dirent и устанавливает *namelist, чтобы он указывал на начало этого массива. Как массив указателей, так и указываемые структуры struct dirent выделяются с помощью malloc(); вызывающий код должен использовать free(), чтобы избежать утечек памяти.

Для выбора нужных элементов используйте указатель функции select. Когда это значение равно NULL, все действительные элементы каталога включаются в конечный массив. В противном случае (*select)() вызывается для каждого элемента, и те элементы, для которых она возвращает ненулевое (истинное) значение, включаются в массив.

Указатель функции compare сравнивает два элемента каталога. Он передается функции qsort() для использования при сортировке.

alphasort() лексикографически сравнивает имена файлов. Она использует для сравнения функцию strcoll(). strcoll() похожа на strcmp(), но учитывает связанные с местной спецификой правила сортировки (см. раздел 13.4 «Не могли бы вы написать это для меня по буквам?»).

versionsort() является расширением GNU, которое использует для сравнения имен файлов функцию GNU strverscmp() (см. strverscmp(3).) Короче говоря, эта функция понимает обычные соглашения по версиям имен файлов и сравнивает их соответствующим образом.

В ch06-sortdir.c приведена программа, похожая на ch04-catdir.c. Однако, она использует для работы scandir() и alphasort().

1  /* ch06-sortdir.c --- Демонстрирует scandir(), alphasort(). */

2

3  #include /* для printf() etc. */

4  #include /* для errno */

5  #include /* для системных типов */

6  #include /* для функций каталогов */

7

8  char *myname;

9  int process(const char *dir);

10

11 /* main --- перечислить аргументы каталога */

12

13 int main(int argc, char **argv)

14 {

15  int i;

16  int errs = 0;

17

18  myname = argv[0];

19

20  if (argc == 1)

21   errs = process("."); /* по умолчанию текущий каталог */

22  else

23   for (i = 1; i < argc; i++)

24    errs += process(argv[i]);

25

26  return (errs != 0);

27 }

28

29 /* nodots --- игнорирует файлы с точкой, для scandir() */

30

31 int

32 nodots(const struct dirent *dp)

33 {

34  return (dp->d_name[0] != '.');

35 }

36

37 /*

38  * process --- сделать что-то с каталогом, в данном случае,

39  * вывести в стандартный вывод пары индекс/имя.

40  * Вернуть 0, если все нормально, в противном случае 1.

41  */

42

43 int

44 process(const char *dir)

45 {

46  DIR *dp;

47  struct dirent **entries;

48  int nents, i;

49

50  nents = scandir(dir, &entries, nodots, alphasort);

51  if (nents < 0) {

52   fprintf(stderr, "%s: scandir failed: %s\n", myname,

53    strerror(errno));

54   return 1;

55  }

56

57  for (i = 0; i < nents; i++) {

58   printf("%81d %s\n", entries[i]->d_ino, entries[i]->d_name);

59   free(entries[i]);

60  }

61

62  free(entries);

63

64  return 0;

65 }

Функция main() программы (строки 1–27) следует стандартному шаблону, который мы использовали до этого. Функция nodots() (строки 31–35) действует как параметр select, выбирая лишь имена файлов, которые не начинаются с точки.

Функция process() (строки 43–65) довольно проста, причем scandir() делает большую часть работы. Обратите внимание, как каждый элемент отдельно освобождается с помощью free() (строка 59) и как освобождается также весь массив (строка 62).

При запуске содержимое каталога в самом деле выводится в отсортированном порядке, без '.' и '..'.

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

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

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

Стивен Прата

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