c system() 函数导致段错误

标签 c

我使用system函数在我的系统中执行linux命令 使用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() 两次时引起的!

有什么预防措施可以避免系统段故障吗?

最佳答案

我无法重现您的问题,因为您的代码无法编译。这是程序的工作版本,带有更改注释(并用 testdir 替换 /etc/config):

#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 */
}

关于c system() 函数导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37831498/

相关文章:

Windows 终端应用程序中的 C 颜色文本

c - pthread_exit(NULL) 段错误

c - 确定三角形类型的简单程序会产生奇怪的结果

c - 如何使用 ssh 命令,使用 execlp()?

c - GNU 内联汇编程序——汇编指令的语法?

c - 总价的 if-else 代码无法编译

检查 pthread 句柄是否正在休眠

c - 从 C 中的文本文件中删除控件 M

c - 结构中的指针中不允许使用 VLA 有充分的理由吗?

c - 与**字符**一起使用时作为参数的数组 VS 与**整数**一起使用时