c system()函数导致分段错误

时间:2016-06-15 09:31:31

标签: 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()函数引起的!

他们是采取什么预防措施来避免系统段错误?

2 个答案:

答案 0 :(得分:0)

我无法重现您的问题,因为您的代码无法编译。这是您的程序的工作版本,其中包含更改注释(以及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 */
}

答案 1 :(得分:-2)

根据这个链接 How do I execute a Shell built-in command with a C function?

使用execl(&#34; sh&#34;,&#34; sh&#34;,&#34; -c&#34;,command,NULL)代替系统