c system()函数导致分段错误
我使用c system()函数导致分段错误,c,C,我使用system函数在我的 使用openwrt发行版作为嵌入式操作系统的设备 int系统(常量字符*命令) 我的程序如下 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);
system
函数在我的
使用openwrt发行版作为嵌入式操作系统的设备
int系统(常量字符*命令)
我的程序如下
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();
}
int check\u file\u dir(字符*名称)
{
int i=0;
char命令[128];
sprintf(命令,“ls/etc/config/%s&>/dev/null”,名称);
printf(“=>command=%s\n”,command);
i=系统(命令);
返回i;
}
作废获取文件信息()
{
字符名[128];
struct dirent*d_文件;
结构统计属性;
字符路径[128];
char s_now[大小为“AAAA-MM-JJTHH:MM:SS.000Z”];
if((dir=opendir(“/etc/config/”)!=NULL)
{
而((d_file=readdir(dir))!=NULL)
{
如果(d_文件->d_名称[0]='.'))
持续
sprintf(路径,“/etc/config/%s”,d_文件->d_名称);
统计(路径和属性);
strftime(s_now,s_now的大小,“%Y-%m-%dT%H:%m:%s.000Z”,localtime(&attr.st_mtime));
}
}
closedir(dir);
int j;
对于(j=0;j
该问题是由调用两次get\u file\u info()
时的system
函数引起的
它们是避免系统seg故障需要采取的任何预防措施?根据此链接
使用execl(“sh”、“sh”、“c”、command,NULL)而不是system我无法重现您的问题,因为您的代码未编译。这是您的程序的工作版本,带有更改注释(并用
testdir
替换/etc/config
):
#包括/*路径_MAX和线_MAX*/
#缺少include/*头include*/
#缺少include/*头include*/
#缺少include/*头include*/
#缺少include/*头include*/
#缺少include/*头include*/
int check_file_dir(字符*名称)
{
int i=0;
char命令[LINE_MAX];/*标准最大命令行长度*/
/*下面的路径是错误的*/
sprintf(命令,“ls%s&>/dev/null”,名称);
printf(“=>command=%s\n”,command);
i=系统(命令);
返回i;
}
作废获取文件信息()
{
字符名称[PATH_MAX];/*标准最大路径长度*/
struct dirent*d_文件;
结构统计属性;
字符路径[path_MAX];/*标准最大路径长度*/
char s_now[大小为“AAAA-MM-JJTHH:MM:SS.000Z”];
缺少DIR*DIR;/*声明*/
int FILE_NUMBER=0;/*声明丢失*/
if((dir=opendir(“testdir”))==NULL)
返回;/*无需继续*/
而((d_file=readdir(dir))!=NULL){
如果(d_文件->d_名称[0]='.'))
持续
sprintf(路径,“testdir/%s”,d_文件->d_名称);
统计(路径和属性);
strftime(s_now,s_now的大小,“%Y-%m-%dT%H:%m:%s.000Z”,
localtime(&attr.st_mtime));/*是否未使用*/
++FILE_NUMBER;/*假设这是您想要做的*/
}
closedir(dir);/*被调用,即使!opendir()*/
int j;
对于(j=0;j
缓冲区大小可能不足。尝试给char命令[]
更多字节。我已经这样做了,将buf大小增加到512,但同样的问题是sprintf(路径,“/etc/config/”,d_文件->d_名称)代码>这是什么?@Sourav说得好@Anis_Stack,sprintf()
的第二个参数需要一些%
修饰符:“/etc/config/%s”
发布您的实际代码get\u name\u from\u conf(“/etc/config”,&name)
甚至不会编译。
#include <limits.h> /* for PATH_MAX and LINE_MAX */
#include <stdio.h> /* header include was missing */
#include <stdlib.h> /* header include was missing */
#include <time.h> /* header include was missing */
#include <dirent.h> /* header include was missing */
#include <sys/stat.h> /* header include was missing */
int check_file_dir(char *name)
{
int i = 0;
char command[LINE_MAX]; /* standard max command line length */
/* path was wrong below */
sprintf(command, "ls %s &> /dev/null", name);
printf("====> command =%s \n", command);
i = system(command);
return i;
}
void get_file_info()
{
char name[PATH_MAX]; /* standard max path length */
struct dirent *d_file;
struct stat attr;
char path[PATH_MAX]; /* standard max path length */
char s_now[sizeof "AAAA-MM-JJTHH:MM:SS.000Z"];
DIR *dir; /* declaration was missing */
int FILE_NUMBER = 0; /* declaration was missing */
if ((dir = opendir("testdir")) == NULL)
return; /* no point to go on */
while ((d_file = readdir(dir)) != NULL) {
if (d_file->d_name[0] == '.')
continue;
sprintf(path, "testdir/%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)); /* not used? */
++FILE_NUMBER; /* assuming this is what you meant to do */
}
closedir(dir); /* was called even if !opendir() */
int j;
for (j = 0; j < FILE_NUMBER; j++) {
sprintf(name, "testdir/file%d", j);
if (check_file_dir(name) != 0)
printf("file doesn't exist \n");
}
}
int main(void) /* int and (void) */
{
get_file_info();
get_file_info();
return 0; /* was missing */
}