【发布时间】:2016-06-15 09:31:31
【问题描述】:
我使用system 函数在我的
使用 openwrt 发行版作为嵌入式操作系统的设备。
int system(const char *command);
我的程序如下所示
int check_file_dir(char *name)
{
int i = 0;
char command[128];
sprintf(command, "ls /etc/config/%s &> /dev/null", name);
printf("====> command =%s \n", command);
i = system(command);
return i;
}
void get_file_info ()
{
char name[128];
struct dirent *d_file;
struct stat attr;
char path[128];
char s_now[sizeof "AAAA-MM-JJTHH:MM:SS.000Z"];
if ((dir = opendir ("/etc/config/")) != NULL)
{
while ((d_file = readdir (dir)) != NULL)
{
if(d_file->d_name[0] == '.')
continue;
sprintf(path, "/etc/config/%s", d_file->d_name);
stat(path, &attr);
strftime(s_now, sizeof s_now, "%Y-%m-%dT%H:%M:%S.000Z", localtime(&attr.st_mtime));
}
}
closedir (dir);
int j;
for (j = 0; j< FILE_NUMBER; j++)
{
sprintf(name, "/etc/config/file%d", j);
if(check_file_dir(name) !=0)
printf("file doesn't exist \n");
}
}
void main ()
{
get_file_info();
get_file_info();
}
问题是由system 函数导致get_file_info() 被调用两次!
他们有什么预防措施来避免系统段错误?
【问题讨论】:
-
可能是缓冲区大小不足。尝试给
char command[]更多字节。 -
我已经这样做了,将 buf 大小增加到 512,但是同样的问题
-
sprintf(path, "/etc/config/", d_file->d_name);这是什么? -
@Sourav 好点! @Anis_Stack,
sprintf()的第二个参数需要一些%修饰符:"/etc/config/%s" -
发布您的实际代码。
get_name_from_conf("/etc/config", &name)甚至不会编译。
标签: c