#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
int main() {
// Всё создаём в отдельной директории, чтобы потом удалить одной командой
if (mkdir("symlink_test", 0755) == -1) {
perror("mkdir symlink_test");
return 1;
}
if (chdir("symlink_test") == -1) {
perror("chdir");
return 1;
}
int fd = creat("a", 0644);
if (fd == -1) {
perror("creat");
return 1;
}
close(fd);
char prev[64] = "a";
char name[64];
int depth = 0;
for (;;) {
snprintf(name, sizeof(name), "link%d", depth + 1);
if (symlink(prev, name) == -1) {
perror("symlink");
return 1;
}
fd = open(name, O_RDONLY);
if (fd == -1) {
if (errno == ELOOP) {
printf("Файл %s открыть не удалось: %s\n", name, strerror(errno));
break;
}
perror("open");
return 1;
}
close(fd);
depth++;
strcpy(prev, name);
}
printf("Глубина рекурсии: %d символьных связей\n", depth);
return 0;
}