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

39  return(EXIT_SUCCESS);

40 }

Вот результаты для GNU/Linux:

$ ch05-trymkdir

mkdir("/tmp/t1/t2/t3/t4") returns -1: errno = 2 [No such file or directory)

mkdir("/tmp/t1/t2/t3") returns -1: errno = 2 [No such file or directory)

mkdir("/tmp/t1/t2") returns -1: errno = 2 [No such file or directory]

mkdir("/tmp/t1") returns 0: errno = 0 [Success]

mkdir("/tmp/u1") returns 0: errno = 0 [Success]

mkdir("/tmp/u1/u2") returns 0: errno = 0 [Success]

mkdir("/tmp/u1/u2/u3") returns 0: errno = 0 [Success]

mkdir("/tmp/u1/u2/u3/u4") returns 0: errno = 0 [Success]

mkdir("/tmp/v1/") returns 0: errno = 0 [Success]

mkdir("/tmp/v1/v2/") returns 0: errno = 0 (Success]

mkdir("/tmp/v1/v2/v3/") returns 0: errno = 0 [Success]

mkdir("/tmp/v1/v2/v3/v4/") returns 0: errno = 0 [Success]

Обратите внимание, как GNU/Linux принимает завершающий слеш. Не все системы так делают.

<p>5.3. Чтение каталогов</p>

В оригинальных системах Unix чтение содержимого каталогов было просто. Программа открывала каталог с помощью open() и непосредственно читала двоичные структуры struct direct, по 16 байтов за раз. Следующий фрагмент кода из программы V7 rmdir[53], строки 60–74. Он показывает проверку на пустоту каталога.

60 if ((fd = open(name, 0)) < 0) {

61  fprintf(stderr, "rmdir: %s unreadable\n", name);

62  ++Errors;

63  return;

64 }

65 while (read(fd, (char*)&dir, sizeof dir) == sizeof dir) {

66  if (dir.d_ino == 0) continue;

67  if (!strcmp(dir.d_name, ".") || !strcmp(dir.d_name, ".."))

68   continue;

69  fprintf(stderr, "rmdir: %s not empty\n", name);

70  ++Errors;

71  close(fd);

72  return;

73 }

74 close(fd);

В строке 60 каталог открывается для чтения (второй аргумент равен 0, что означает O_RDONLY). В строке 65 читается struct direct. В строке 66 проверяется, не является ли элемент каталога пустым, т. е. с номером индекса 0. Строки 67 и 68 проверяют на наличие '.' и '..'. По достижении строки 69 мы знаем, что было встречено какое-то другое имя файла, следовательно, этот каталог не пустой.

(Тест '!strcmp(s1, s2)' является более короткой формой 'strcmp(s1, s2) == 0', т.е. проверкой совпадения строк. Стоит заметить, что мы рассматриваем '!strcmp(s1, s2)' как плохой стиль. Как сказал однажды Генри Спенсер (Henry Spencer), «strcmp() это не boolean!».)

Когда 4.2 BSD представило новый формат файловой системы, который допускал длинные имена файлов и обеспечивал лучшую производительность, были также представлены несколько новых функций для абстрагирования чтения каталогов. Этот набор функций можно использовать независимо от того, какова лежащая в основе файловая система и как организованы каталоги. Основная ее часть стандартизована POSIX, а программы, использующие ее, переносимы между системами GNU/Linux и Unix.

<p>5.3.1. Базовое чтение каталогов</p>

Элементы каталогов представлены struct dirent (не то же самое, что V7 struct direct!):

struct dirent {

 ...

 ino_t d_ino;      /* расширение XSI --- см. текст */

 char d_name[...]; /* О размере этого массива см. в тексте */

 ...

};

Для переносимости POSIX указывает лишь поле d_name, которое является завершающимся нулем массивом байтов, представляющим часть элемента каталога с именем файла. Размер d_name стандартом не указывается, кроме того, что там перед завершающим нулем может быть не более NAME_MAX байтов. (NAME_MAX определен в .) Расширение XSI POSIX предусматривает поле номера индекса d_ino.

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

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

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

Стивен Прата

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