include "head.h" int main(int argc, char *argv[]) { if (argc != 2) { printf("%s<dirname>\n", argv[0]); return -1; } struct dirent *dt; struct stat st; struct tm *tp; struct passwd *pd; struct group *gd; char pathname[300]; DIR *dir = opendir(argv[1]); while((dt= readdir(dir))!=NULL) { if (!strcmp(dt->d_name, ".") || !strcmp(dt->d_name, "..")) { continue; } memset(&stat, 0, sizeof(stat)); snprintf(pathname, strlen(argv[1]) + strlen(dt->d_name) + 2, "%s%s", argv[1], dt->d_name); if (lstat(pathname, &st) < 0) { perror("lstat"); return -1; } // 权限 if (S_ISREG(st.st_mode)) printf("-"); else if (S_ISDIR(st.st_mode)) printf("d"); else if (S_ISCHR(st.st_mode)) printf("c"); else if (S_ISBLK(st.st_mode)) printf("b"); else if (S_ISFIFO(st.st_mode)) printf("p"); else if (S_ISLNK(st.st_mode)) printf("l"); else if (S_ISSOCK(st.st_mode)) printf("s"); printf("111111111111111111111111\n"); int i; char *m = "xwr"; for (i = 8; i >= 0; i--) { if (st.st_mode & 1 << i) { printf("%c", m[i % 3]); } else { printf("-"); } } printf(" "); tp = localtime(&st.st_ctime); printf("%lu %s %s %ld", st.st_nlink, pd->pw_name, gd->gr_name, st.st_size); printf(" "); printf("%d月 ", tp->tm_mon + 1); printf("%d ", tp->tm_mday); printf("%d:%d ", tp->tm_hour, tp->tm_min); printf("%s ", pathname); } closedir(dir); return 0; }这段代码那里有问题
时间: 2024-03-17 13:47:35 浏览: 21
这段代码有一个问题是在使用 memset 函数清空结构体 st 时,传入了错误的参数。应该传入 &st 而不是 stat。正确的代码应该是:
```
memset(&st, 0, sizeof(st));
```
另外,pd 和 gd 指针变量在代码中没有进行初始化,可能会导致未定义的行为。应该使用 getpwuid 和 getgrgid 函数获取对应的用户和组信息。
相关问题
include "head.h" int main(int argc, char *argv[]) { if (argc != 2) { printf("%s<dirname>\n", argv[0]); return -1; } struct dirent *dt; struct stat st; struct tm *tp; struct passwd *pd; struct group *gd; char pathname[300]; DIR *dir = opendir(argv[1]); while((dt= readdir(dir))!=NULL) { if (!strcmp(dt->d_name, ".") || !strcmp(dt->d_name, "..")) { continue; } memset(&stat, 0, sizeof(stat)); snprintf(pathname, strlen(argv[1]) + strlen(dt->d_name) + 2, "%s%s", argv[1], dt->d_name); if (lstat(pathname, &st) < 0) { perror("lstat"); return -1; } // 权限 if (S_ISREG(st.st_mode)) printf("-"); else if (S_ISDIR(st.st_mode)) printf("d"); else if (S_ISCHR(st.st_mode)) printf("c"); else if (S_ISBLK(st.st_mode)) printf("b"); else if (S_ISFIFO(st.st_mode)) printf("p"); else if (S_ISLNK(st.st_mode)) printf("l"); else if (S_ISSOCK(st.st_mode)) printf("s"); printf("111111111111111111111111\n"); int i; char *m = "xwr"; for (i = 8; i >= 0; i--) { if (st.st_mode & 1 << i) { printf("%c", m[i % 3]); } else { printf("-"); } } printf(" "); tp = localtime(&st.st_ctime); printf("%lu %s %s %ld", st.st_nlink, pd->pw_name, gd->gr_name, st.st_size); printf(" "); printf("%d月 ", tp->tm_mon + 1); printf("%d ", tp->tm_mday); printf("%d:%d ", tp->tm_hour, tp->tm_min); printf("%s ", pathname); } closedir(dir); return 0; }
这段代码是一个 C 语言程序,用于实现一个简单的 ls 命令。它通过读取指定目录下的所有文件和子目录,并输出它们的文件类型、权限、大小、创建时间和名称等信息。其中使用了 POSIX 标准库中的 dirent.h、stat.h、time.h、pwd.h 和 grp.h 等头文件,以及相关的数据结构和函数。如果运行该程序时没有指定目录名,则会输出使用说明并返回错误码。
#include <iostream> #include <unistd.h> #include "opencv_face_process.h" #include "socket_server.h" #include "user_mngr.h" #include <QApplication> /* C++ include C */ #ifdef __cplusplus extern "C" { #endif /* C head file */ #ifdef __cplusplus } #endif using namespace std; struct main_mngr_info main_mngr; int main(int argc, char* argv[]) { QApplication qtApp(argc, argv); (void)argc; (void)argv; cout << "hello background_app" << endl; memset(&main_mngr, 0, sizeof(struct main_mngr_info)); /* load config file */ main_mngr.config_ini = iniparser_load(PATH_CONFIG_INI); if(main_mngr.config_ini == NULL) { printf("WARNING: %s: load [%s] failed, will use default value.\n", __FUNCTION__, PATH_CONFIG_INI); //return -1; // will use default value } main_mngr.work_state = WORK_STA_NORMAL; main_mngr.user_handle = -1; main_mngr.mngr_handle = -1; main_mngr.capture_flag = 0; user_mngr_init(); start_face_process_task(); start_socket_server_task(); return qtApp.exec(); // start qt application, message loop ... }
这是一个C++程序的main函数,其中包含了一些头文件的引用和命名空间的声明。接下来创建了一个结构体main_mngr_info的实例main_mngr,并初始化为零。然后加载配置文件,并检查加载是否成功。如果加载失败,会打印警告信息,但程序会继续执行。接着对main_mngr的一些成员进行赋值操作。然后调用user_mngr_init()函数进行用户管理的初始化。接下来启动人脸处理任务和套接字服务器任务。最后通过qtApp.exec()启动Qt应用程序的消息循环。