12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #include <sys/stat.h>
- #include <sys/sysmacros.h>
- #include <unistd.h>
- //#include "log.h"
- #define BOLD_GRAY "\033[1;90m"
- #define RED "\033[0;31m"
- #define WHITE "\033[0;37m"
- #define RESET "\033[0m"
- int module_03_run(const char *filename) {
- char log_msg[1024];
- struct stat st;
- if (stat(filename, &st) != 0) {
- snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libstat: " RED "File type not recognized: %s" RESET, strerror(errno));
- printf("%s\n", log_msg);
- // log_message(log_msg);
- return 1;
- }
- const char *type;
- char extra_info[512] = "";
- if (S_ISREG(st.st_mode)) {
- type = "regular file";
- } else if (S_ISDIR(st.st_mode)) {
- type = "directory";
- } else if (S_ISCHR(st.st_mode)) {
- type = "character device";
- snprintf(extra_info, sizeof(extra_info), ", Major: %d, Minor: %d",
- major(st.st_rdev), minor(st.st_rdev));
- } else if (S_ISBLK(st.st_mode)) {
- type = "block device";
- snprintf(extra_info, sizeof(extra_info), ", Major: %d, Minor: %d",
- major(st.st_rdev), minor(st.st_rdev));
- } else if (S_ISFIFO(st.st_mode)) {
- type = "FIFO (named pipe)";
- } else if (S_ISSOCK(st.st_mode)) {
- type = "socket";
- } else if (S_ISLNK(st.st_mode)) {
- type = "symbolic link";
- char link_target[256];
- ssize_t len = readlink(filename, link_target, sizeof(link_target) - 1);
- if (len != -1) {
- link_target[len] = '\0';
- snprintf(extra_info, sizeof(extra_info), ", Points to: %s", link_target);
- } else {
- snprintf(extra_info, sizeof(extra_info), ", Error reading link: %s", strerror(errno));
- }
- } else {
- type = "unknown";
- }
- if (strncmp(filename, "/proc/", 6) == 0) {
- type = "procfs pseudo-file";
- } else if (strncmp(filename, "/sys/", 5) == 0) {
- type = "sysfs pseudo-file";
- }
- snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libstat: " WHITE "Size: %ld bytes, Permissions: %o, Type: %s%s" RESET,
- (long)st.st_size, st.st_mode & 0777, type, extra_info);
- printf("%s\n", log_msg);
- // log_message(log_msg);
- return 0;
- }
|