0

I use the system function to execute a linux command in my device which use openwrt distribution as embedded OS.

int system(const char *command);

my program is like below

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();
}

the problem is cause by system function when get_file_info() is called twice!

they is any precautions to take to avoid system seg fault ?

Anis_Stack
  • 3,306
  • 4
  • 30
  • 53

2 Answers2

0

I can't reproduce your issue because your code does not compile. This is a working version of your program with annotations of changes (and with testdir replacing /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 */
}
Kusalananda
  • 14,885
  • 3
  • 41
  • 52
-2

according to this link How do I execute a Shell built-in command with a C function?

use execl("sh", "sh", "-c", command, NULL) instead of system

Community
  • 1
  • 1
Anis_Stack
  • 3,306
  • 4
  • 30
  • 53