dirent的char d_name[1]问题

SunOS dirent 结构解析
SunOS中dirent中的文件名定义为char d_name[0];
这样,分配的时候malloc分配空间给d_name但是并不d_name本身并不占用空间.若声明为指针,则会多4个字节.分配方式如下.实际上把d_name这个结构的空间和字符串的空间放在一起,
d_name就是name的首地址,起一个标号的作用.很明显,这个d_name必须是结构体中的最后一个元素,不然是不成立的.
struct dirent
{
   int len;
   char a[0];
};

struct dirent *fun(char *str, int len)
{
   struct dirent* n = (struct dirent*)malloc(len +1 + sizeof(struct dirent));
   if (!n)
      return NULL;
   n->len = len;
   memcpy(n->a, str, len);
   return n;
}


#include <stdio.h> #include <stdlib.h> #include <string.h> #include <dirent.h> #include <sys/stat.h> #include <time.h> #include <pthread.h> #include <unistd.h> #define MAX_PATH_LEN 256 #define TIMESTAMP_FORMAT "%Y-%m-%d_%H-%M-%S" typedef struct { time_t ts; char path[MAX_PATH_LEN]; } LidarData; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; void* monitor_thread_func(void* arg) { const char* base_path = "/mnt/ssd/lidar_pcds/"; while (1) { DIR* date_dir = opendir(base_path); if (!date_dir) { perror("无法打开目录"); sleep(1); continue; } struct dirent* date_entry; while ((date_entry = readdir(date_dir)) != NULL) { if (date_entry->d_type == DT_DIR && strcmp(date_entry->d_name, ".") != 0 && strcmp(date_entry->d_name, "..") != 0) { char full_date_path[MAX_PATH_LEN]; snprintf(full_date_path, sizeof(full_date_path), "%s%s", base_path, date_entry->d_name); DIR* pcd_dir = opendir(full_date_path); if (!pcd_dir) { continue; } struct dirent* pcd_entry; while ((pcd_entry = readdir(pcd_dir)) != NULL) { if (pcd_entry->d_type == DT_DIR && strstr(pcd_entry->d_name, "_pcd")) { char full_pcd_path[MAX_PATH_LEN]; snprintf(full_pcd_path, sizeof(full_pcd_path), "%s/%s", full_date_path, pcd_entry->d_name); DIR* pcd_subdir = opendir(full_pcd_path); if (!pcd_subdir) { continue; } int has_non_zero = 0; struct dirent* pcd_file; while ((pcd_file = readdir(pcd_subdir)) != NULL) { if (strstr(pcd_file->d_name, ".pcd")) { char pcd_file_path[MAX_PATH_LEN]; snprintf(pcd_file_path, sizeof(pcd_file_path), "%s/%s", full_pcd_path, pcd_file->d_name); struct stat st; if (stat(pcd_file_path, &st) == 0 && st.st_size > 0) { has_non_zero = 1; break; } } } closedir(pcd_subdir); if (has_non_zero) { char timestamp_str[32]; strncpy(timestamp_str, pcd_entry->d_name, strstr(pcd_entry->d_name, "_pcd") - pcd_entry->d_name); timestamp_str[strstr(pcd_entry->d_name, "_pcd") - pcd_entry->d_name] = '\0'; struct tm tm = {0}; if (strptime(timestamp_str, TIMESTAMP_FORMAT, &tm) != NULL) { time_t ts = mktime(&tm); pthread_mutex_lock(&mutex); printf("时间戳: %ld, 路径: %s\n", (long)ts, full_pcd_path); pthread_mutex_unlock(&mutex); } } } } closedir(pcd_dir); } } closedir(date_dir); sleep(1); } return NULL; } int main() { pthread_t monitor_thread; if (pthread_create(&monitor_thread, NULL, monitor_thread_func, NULL) != 0) { perror("无法创建线程"); return 1; } pthread_join(monitor_thread, NULL); return 0; } 在这个的基础上增加如果两次都没有发现有新的_pcd文件夹生成报错,同时文件夹里面pcd文件大小都是0就报错
最新发布
08-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值