module_07.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <archive.h>
  6. #include <archive_entry.h>
  7. #define BOLD_GRAY "\033[1;90m"
  8. #define RED "\033[0;31m"
  9. #define WHITE "\033[0;37m"
  10. #define RESET "\033[0m"
  11. int module_07_run(const char *filename) {
  12. char log_msg[1024];
  13. struct archive *a = archive_read_new();
  14. if (!a) {
  15. snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libarchive: " RED "Error initializing libarchive" RESET);
  16. printf("%s\n", log_msg);
  17. return 1;
  18. }
  19. archive_read_support_format_all(a);
  20. archive_read_support_filter_all(a);
  21. if (archive_read_open_filename(a, filename, 10240) != ARCHIVE_OK) {
  22. snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libarchive: " RED "Not an archive: %s" RESET,
  23. archive_error_string(a));
  24. printf("%s\n", log_msg);
  25. archive_read_free(a);
  26. return 1;
  27. }
  28. struct archive_entry *entry;
  29. int entries_read = 0;
  30. while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
  31. entries_read++;
  32. }
  33. if (entries_read > 0) {
  34. snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libarchive: " WHITE "Archive format: %s, Entries: %d" RESET,
  35. archive_format_name(a), entries_read);
  36. } else {
  37. snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libarchive: " RED "Corrupted or empty archive" RESET);
  38. }
  39. printf("%s\n", log_msg);
  40. archive_read_close(a);
  41. archive_read_free(a);
  42. return 0;
  43. }