Skip to content
Snippets Groups Projects
dmcommon.c 46.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • {
    	struct uci_section *s;
    
    	uci_foreach_sections(package, section_type, s) {
    		if (strcmp(section_name(s), orig_section_name) == 0){
    			return s;
    		}
    	}
    	return NULL;
    }
    
    struct uci_section *get_dup_section_in_dmmap(char *dmmap_package, char *section_type, char *orig_section_name)
    {
    	struct uci_section *s;
    
    
    	uci_path_foreach_option_eq(bbfdm, dmmap_package, section_type, "section_name", orig_section_name, s)
    
    struct uci_section *get_dup_section_in_dmmap_opt(char *dmmap_package, char *section_type, char *opt_name, char *opt_value)
    
    {
    	struct uci_section *s;
    
    
    	uci_path_foreach_option_eq(bbfdm, dmmap_package, section_type, opt_name, opt_value, s)
    	{
    		return s;
    	}
    
    	return NULL;
    }
    
    struct uci_section *get_dup_section_in_dmmap_eq(char *dmmap_package, char* section_type, char*sect_name, char *opt_name, char* opt_value){
    	struct uci_section *s;
    
    	uci_path_foreach_option_eq(bbfdm, dmmap_package, section_type, "section_name", sect_name, s) {
    
    		dmuci_get_value_by_section_string(s, opt_name, &v);
    		if(strcmp(v, opt_value)== 0)
    			return s;
    	}
    	return NULL;
    }
    
    void synchronize_specific_config_sections_with_dmmap(char *package, char *section_type, char *dmmap_package, struct list_head *dup_list)
    {
    	struct uci_section *s, *stmp, *dmmap_sect;
    	FILE *fp;
    	char *v, *dmmap_file_path;
    
    
    	dmasprintf(&dmmap_file_path, "/etc/bbfdm/%s", dmmap_package);
    
    	if (access(dmmap_file_path, F_OK)) {
    		/*
    		 *File does not exist
    		 **/
    		fp = fopen(dmmap_file_path, "w"); // new empty file
    		fclose(fp);
    	}
    	uci_foreach_sections(package, section_type, s) {
    		/*
    		 * create/update corresponding dmmap section that have same config_section link and using param_value_array
    		 */
    		if ((dmmap_sect = get_dup_section_in_dmmap(dmmap_package, section_type, section_name(s))) == NULL) {
    
    			dmuci_add_section_bbfdm(dmmap_package, section_type, &dmmap_sect, &v);
    			DMUCI_SET_VALUE_BY_SECTION(bbfdm, dmmap_sect, "section_name", section_name(s));
    
    		}
    
    		/*
    		 * Add system and dmmap sections to the list
    		 */
    		add_sectons_list_paramameter(dup_list, s, dmmap_sect, NULL);
    	}
    
    	/*
    	 * Delete unused dmmap sections
    	 */
    
    	uci_path_foreach_sections_safe(bbfdm, dmmap_package, section_type, stmp, s) {
    
    		dmuci_get_value_by_section_string(s, "section_name", &v);
    		if(get_origin_section_from_config(package, section_type, v) == NULL){
    
    			dmuci_delete_by_section_unnamed_bbfdm(s, NULL, NULL);
    
    		}
    	}
    }
    
    void synchronize_specific_config_sections_with_dmmap_eq(char *package, char *section_type, char *dmmap_package,char* option_name, char* option_value, struct list_head *dup_list)
    {
    	struct uci_section *s, *stmp, *dmmap_sect;
    	FILE *fp;
    	char *v, *dmmap_file_path;
    
    
    	dmasprintf(&dmmap_file_path, "/etc/bbfdm/%s", dmmap_package);
    
    	if (access(dmmap_file_path, F_OK)) {
    		/*
    		 *File does not exist
    		 **/
    		fp = fopen(dmmap_file_path, "w"); // new empty file
    		fclose(fp);
    	}
    	uci_foreach_option_eq(package, section_type, option_name, option_value, s) {
    		/*
    		 * create/update corresponding dmmap section that have same config_section link and using param_value_array
    		 */
    		if ((dmmap_sect = get_dup_section_in_dmmap(dmmap_package, section_type, section_name(s))) == NULL) {
    
    			dmuci_add_section_bbfdm(dmmap_package, section_type, &dmmap_sect, &v);
    			DMUCI_SET_VALUE_BY_SECTION(bbfdm, dmmap_sect, "section_name", section_name(s));
    
    		}
    
    		/*
    		 * Add system and dmmap sections to the list
    		 */
    		add_sectons_list_paramameter(dup_list, s, dmmap_sect, NULL);
    	}
    
    	/*
    	 * Delete unused dmmap sections
    	 */
    
    	uci_path_foreach_sections_safe(bbfdm, dmmap_package, section_type, stmp, s) {
    
    		dmuci_get_value_by_section_string(s, "section_name", &v);
    		if(get_origin_section_from_config(package, section_type, v) == NULL){
    			dmuci_delete_by_section(s, NULL, NULL);
    		}
    	}
    }
    
    void synchronize_specific_config_sections_with_dmmap_eq_no_delete(char *package, char *section_type, char *dmmap_package, char* option_name, char* option_value, struct list_head *dup_list)
    {
    	struct uci_section *s, *dmmap_sect;
    	FILE *fp;
    	char *v, *dmmap_file_path;
    
    
    	dmasprintf(&dmmap_file_path, "/etc/bbfdm/%s", dmmap_package);
    
    	if (access(dmmap_file_path, F_OK)) {
    		/*
    		 *File does not exist
    		 **/
    		fp = fopen(dmmap_file_path, "w"); // new empty file
    		fclose(fp);
    	}
    	uci_foreach_option_eq(package, section_type, option_name, option_value, s) {
    		/*
    		 * create/update corresponding dmmap section that have same config_section link and using param_value_array
    		 */
    		if ((dmmap_sect = get_dup_section_in_dmmap(dmmap_package, section_type, section_name(s))) == NULL) {
    
    			dmuci_add_section_bbfdm(dmmap_package, section_type, &dmmap_sect, &v);
    			DMUCI_SET_VALUE_BY_SECTION(bbfdm, dmmap_sect, "section_name", section_name(s));
    
    		}
    	}
    
    	dmmap_sect= NULL;
    	s= NULL;
    
    	uci_path_foreach_sections(bbfdm, dmmap_package, section_type, dmmap_sect) {
    
    		dmuci_get_value_by_section_string(dmmap_sect, "section_name", &v);
    		get_config_section_of_dmmap_section("network", "interface", v, &s);
    		add_sectons_list_paramameter(dup_list, s, dmmap_sect, NULL);
    	}
    }
    
    void synchronize_specific_config_sections_with_dmmap_cont(char *package, char *section_type, char *dmmap_package,char* option_name, char* option_value, struct list_head *dup_list)
    {
    	struct uci_section *s, *stmp, *dmmap_sect;
    	FILE *fp;
    	char *v, *dmmap_file_path;
    
    
    	dmasprintf(&dmmap_file_path, "/etc/bbfdm/%s", dmmap_package);
    
    	if (access(dmmap_file_path, F_OK)) {
    		/*
    		 *File does not exist
    		 **/
    		fp = fopen(dmmap_file_path, "w"); // new empty file
    		fclose(fp);
    	}
    	uci_foreach_option_cont(package, section_type, option_name, option_value, s) {
    		/*
    		 * create/update corresponding dmmap section that have same config_section link and using param_value_array
    		 */
    		if ((dmmap_sect = get_dup_section_in_dmmap(dmmap_package, section_type, section_name(s))) == NULL) {
    
    			dmuci_add_section_bbfdm(dmmap_package, section_type, &dmmap_sect, &v);
    			DMUCI_SET_VALUE_BY_SECTION(bbfdm, dmmap_sect, "section_name", section_name(s));
    
    		}
    
    		/*
    		 * Add system and dmmap sections to the list
    		 */
    		add_sectons_list_paramameter(dup_list, s, dmmap_sect, NULL);
    	}
    
    	/*
    	 * Delete unused dmmap sections
    	 */
    
    	uci_path_foreach_sections_safe(bbfdm, dmmap_package, section_type, stmp, s) {
    
    		dmuci_get_value_by_section_string(s, "section_name", &v);
    
    		if(get_origin_section_from_config(package, section_type, v) == NULL){
    			dmuci_delete_by_section(s, NULL, NULL);
    		}
    	}
    }
    
    bool synchronize_multi_config_sections_with_dmmap_eq(char *package, char *section_type, char *dmmap_package, char* dmmap_section, char* option_name, char* option_value, void* additional_attribute, struct list_head *dup_list)
    {
    	struct uci_section *s, *stmp, *dmmap_sect;
    	FILE *fp;
    	char *v, *dmmap_file_path, *pack, *sect;
    	bool found = false;
    
    
    	dmasprintf(&dmmap_file_path, "/etc/bbfdm/%s", dmmap_package);
    
    	if (access(dmmap_file_path, F_OK)) {
    		/*
    		 *File does not exist
    		 **/
    		fp = fopen(dmmap_file_path, "w"); // new empty file
    		fclose(fp);
    	}
    
    	uci_foreach_option_eq(package, section_type, option_name, option_value, s) {
    		found = true;
    		/*
    		 * create/update corresponding dmmap section that have same config_section link and using param_value_array
    		 */
    		if ((dmmap_sect = get_dup_section_in_dmmap(dmmap_package, dmmap_section, section_name(s))) == NULL) {
    
    			dmuci_add_section_bbfdm(dmmap_package, dmmap_section, &dmmap_sect, &v);
    			DMUCI_SET_VALUE_BY_SECTION(bbfdm, dmmap_sect, "section_name", section_name(s));
    			DMUCI_SET_VALUE_BY_SECTION(bbfdm, dmmap_sect, "package", package);
    			DMUCI_SET_VALUE_BY_SECTION(bbfdm, dmmap_sect, "section", section_type);
    
    		}
    
    		/*
    		 * Add system and dmmap sections to the list
    		 */
    		add_sectons_list_paramameter(dup_list, s, dmmap_sect, additional_attribute);
    	}
    
    	/*
    	 * Delete unused dmmap sections
    	 */
    
    	uci_path_foreach_sections_safe(bbfdm, dmmap_package, dmmap_section, stmp, s) {
    
    		dmuci_get_value_by_section_string(s, "section_name", &v);
    		dmuci_get_value_by_section_string(s, "package", &pack);
    		dmuci_get_value_by_section_string(s, "section", &sect);
    		if(v!=NULL && strlen(v)>0 && strcmp(package, pack)==0 && strcmp(section_type, sect)== 0){
    			if(get_origin_section_from_config(package, section_type, v) == NULL){
    				dmuci_delete_by_section(s, NULL, NULL);
    			}
    		}
    	}
    
    	return found;
    }
    
    bool synchronize_multi_config_sections_with_dmmap_eq_diff(char *package, char *section_type, char *dmmap_package, char* dmmap_section, char* option_name, char* option_value, char* opt_diff_name, char* opt_diff_value, void* additional_attribute, struct list_head *dup_list)
    {
    	struct uci_section *s, *stmp, *dmmap_sect;
    	FILE *fp;
    	char *v, *dmmap_file_path, *pack, *sect, *optval;
    	bool found= false;
    
    
    	dmasprintf(&dmmap_file_path, "/etc/bbfdm/%s", dmmap_package);
    
    	if (access(dmmap_file_path, F_OK)) {
    		/*
    		 *File does not exist
    		 **/
    		fp = fopen(dmmap_file_path, "w"); // new empty file
    		fclose(fp);
    	}
    
    	uci_foreach_option_eq(package, section_type, option_name, option_value, s) {
    		found = true;
    		dmuci_get_value_by_section_string(s, opt_diff_name, &optval);
    		if (strcmp(optval, opt_diff_value) != 0) {
    			/*
    			 * create/update corresponding dmmap section that have same config_section link and using param_value_array
    			 */
    			if ((dmmap_sect = get_dup_section_in_dmmap(dmmap_package, dmmap_section, section_name(s))) == NULL) {
    
    				dmuci_add_section_bbfdm(dmmap_package, dmmap_section, &dmmap_sect, &v);
    				DMUCI_SET_VALUE_BY_SECTION(bbfdm, dmmap_sect, "section_name", section_name(s));
    				DMUCI_SET_VALUE_BY_SECTION(bbfdm, dmmap_sect, "package", package);
    				DMUCI_SET_VALUE_BY_SECTION(bbfdm, dmmap_sect, "section", section_type);
    
    			}
    
    			/*
    			 * Add system and dmmap sections to the list
    			 */
    			add_sectons_list_paramameter(dup_list, s, dmmap_sect, additional_attribute);
    		}
    	}
    
    	/*
    	 * Delete unused dmmap sections
    	 */
    
    	uci_path_foreach_sections_safe(bbfdm, dmmap_package, dmmap_section, stmp, s) {
    
    		dmuci_get_value_by_section_string(s, "section_name", &v);
    		dmuci_get_value_by_section_string(s, "package", &pack);
    		dmuci_get_value_by_section_string(s, "section", &sect);
    		if(v!=NULL && strlen(v)>0 && strcmp(package, pack)==0 && strcmp(section_type, sect)== 0){
    			if(get_origin_section_from_config(package, section_type, v) == NULL){
    				dmuci_delete_by_section(s, NULL, NULL);
    			}
    		}
    	}
    
    	return found;
    }
    
    
    void synchronize_system_folders_with_dmmap_opt(char *sysfsrep, char *dmmap_package, char *dmmap_section, char *opt_name, char* inst_opt, struct list_head *dup_list)
    
    	struct uci_section *s, *stmp, *dmmap_sect;
    	FILE *fp;
    	DIR *dir;
    	struct dirent *ent;
    	char *v, *dmmap_file_path, *sysfs_rep_path, *instance= NULL;
    	struct sysfs_dmsection *p;
    	LIST_HEAD(dup_list_no_inst);
    
    	dmasprintf(&dmmap_file_path, "/etc/bbfdm/%s", dmmap_package);
    	if (access(dmmap_file_path, F_OK)) {
    		/*
    		 *File does not exist
    		 **/
    		fp = fopen(dmmap_file_path, "w"); // new empty file
    		fclose(fp);
    	}
    
    	sysfs_foreach_file(sysfsrep, dir, ent) {
    		if(strcmp(ent->d_name, ".")==0 || strcmp(ent->d_name, "..")==0)
    			continue;
    
    		/*
    		 * create/update corresponding dmmap section that have same config_section link and using param_value_array
    		 */
    		dmasprintf(&sysfs_rep_path, "%s/%s", sysfsrep, ent->d_name);
    		if ((dmmap_sect = get_dup_section_in_dmmap_opt(dmmap_package, dmmap_section, opt_name, sysfs_rep_path)) == NULL) {
    			dmuci_add_section_bbfdm(dmmap_package, dmmap_section, &dmmap_sect, &v);
    			DMUCI_SET_VALUE_BY_SECTION(bbfdm, dmmap_sect, opt_name, sysfs_rep_path);
    		}
    
    		dmuci_get_value_by_section_string(dmmap_sect, inst_opt, &instance);
    
    		/*
    		 * Add system and dmmap sections to the list
    		 */
    
    		if(instance == NULL || strlen(instance) <= 0)
    			add_sysfs_sectons_list_paramameter(&dup_list_no_inst, dmmap_sect, ent->d_name, sysfs_rep_path);
    		else
    			add_sysfs_sectons_list_paramameter(dup_list, dmmap_sect, ent->d_name, sysfs_rep_path);
    	}
    	/*
    	 * fusion two lists
    	 */
    	list_for_each_entry(p, &dup_list_no_inst, list) {
    		add_sysfs_sectons_list_paramameter(dup_list, p->dm, p->sysfs_folder_name, p->sysfs_folder_path);
    	}
    	/*
    	 * Delete unused dmmap sections
    	 */
    	uci_path_foreach_sections_safe(bbfdm, dmmap_package, dmmap_section, stmp, s) {
    		dmuci_get_value_by_section_string(s, opt_name, &v);
    		if(isfolderexist(v) == 0){
    			dmuci_delete_by_section_unnamed_bbfdm(s, NULL, NULL);
    		}
    	}
    }
    
    void get_dmmap_section_of_config_section(char* dmmap_package, char* section_type, char *section_name, struct uci_section **dmmap_section){
    
    	struct uci_section* s;
    
    
    	uci_path_foreach_option_eq(bbfdm, dmmap_package, section_type, "section_name", section_name, s){
    
    		*dmmap_section= s;
    		return;
    	}
    	*dmmap_section= NULL;
    }
    
    void get_dmmap_section_of_config_section_eq(char* dmmap_package, char* section_type, char *opt, char* value, struct uci_section **dmmap_section)
    {
    	struct uci_section* s;
    
    
    	uci_path_foreach_option_eq(bbfdm, dmmap_package, section_type, opt, value, s){
    
    		*dmmap_section= s;
    		return;
    	}
    	*dmmap_section= NULL;
    }
    
    void get_config_section_of_dmmap_section(char* package, char* section_type, char *section_name, struct uci_section **config_section)
    {
    	struct uci_section* s;
    
    	uci_foreach_sections(package, section_type, s){
    		if(strcmp(section_name(s), section_name)==0){
    			*config_section= s;
    			return;
    		}
    	}
    	*config_section= NULL;
    }
    
    void check_create_dmmap_package(char *dmmap_package)
    {
    	FILE *fp;
    	char *dmmap_file_path;
    
    
    	dmasprintf(&dmmap_file_path, "/etc/bbfdm/%s", dmmap_package);
    
    	if (access(dmmap_file_path, F_OK)) {
    		/*
    		 *File does not exist
    		 **/
    		fp = fopen(dmmap_file_path, "w"); // new empty file
    		fclose(fp);
    	}
    	dmfree(dmmap_file_path);
    }
    
    int is_section_unnamed(char *section_name)
    {
            int i;
    
            if(strlen(section_name)!=9)
                    return 0;
            if(strstr(section_name, "cfg") != section_name)
                    return 0;
            for(i=3; i<9; i++){
                    if(!isxdigit(section_name[i]))
                            return 0;
            }
            return 1;
    }
    
    
    void add_dmmap_list_section(struct list_head *dup_list, char* section_name, char* instance)
    {
    	struct dmmap_sect *dmsect;
    
    	dmsect = dmcalloc(1, sizeof(struct dmmap_sect));
    	list_add_tail(&dmsect->list, dup_list);
    	dmasprintf(&dmsect->section_name, "%s", section_name);
    	dmasprintf(&dmsect->instance, "%s", instance);
    }
    
    void delete_sections_save_next_sections(char* dmmap_package, char *section_type, char *instancename, char *section_name, int instance, struct list_head *dup_list) 
    {
    	struct uci_section *s, *stmp;
    	char *v=NULL, *lsectname= NULL, *tmp= NULL;
    	int inst;
    
    	asprintf(&lsectname, "%s", section_name);
    
    
    	uci_path_foreach_sections(bbfdm, dmmap_package, section_type, s) {
    
    		dmuci_get_value_by_section_string(s, instancename, &v);
    		inst= atoi(v);
    		if(inst>instance){
    			dmuci_get_value_by_section_string(s, "section_name", &tmp);
    			add_dmmap_list_section(dup_list, lsectname, v);
    			free(lsectname);
    			lsectname= NULL;
    			asprintf(&lsectname, "%s", tmp);
    			free(tmp);
    			tmp= NULL;
    		}
    	}
    
    	if(lsectname != NULL) free(lsectname);
    
    
    
    	uci_path_foreach_sections_safe(bbfdm, dmmap_package, section_type, stmp, s) {
    
    		dmuci_get_value_by_section_string(s, instancename, &v);
    		inst= atoi(v);
    		if(inst>=instance)
    
    			dmuci_delete_by_section_unnamed_bbfdm(s, NULL, NULL);
    
    	}
    }
    
    void update_dmmap_sections(struct list_head *dup_list, char *instancename, char* dmmap_package, char *section_type)
    {
    	struct uci_section *dm_sect;
    	char *v;
    	struct dmmap_sect *p;
    
    	list_for_each_entry(p, dup_list, list) {
    
    		dmuci_add_section_bbfdm(dmmap_package, section_type, &dm_sect, &v);
    
    		dmuci_set_value_by_section(dm_sect, "section_name", p->section_name);
    		dmuci_set_value_by_section(dm_sect, instancename, p->instance);
    	}
    }
    
    struct uci_section *is_dmmap_section_exist(char* package, char* section)
    {
    	struct uci_section *s;
    
    
    	uci_path_foreach_sections(bbfdm, package, section, s) {
    
    		return s;
    	}
    	return NULL;
    }
    
    struct uci_section *is_dmmap_section_exist_eq(char* package, char* section, char* opt, char* value)
    {
    	struct uci_section *s;
    
    
    	uci_path_foreach_option_eq(bbfdm, package, section, opt, value, s) {
    
    		return s;
    	}
    	return NULL;
    }
    
    unsigned char isdigit_str(char *str)
    {
    	if (!(*str)) return 0;
    	while(isdigit(*str++));
    	return ((*(str-1)) ? 0 : 1);
    }
    
    static inline int isword_delim(char c)
    {
    	if (c == ' ' ||
    		c == ',' ||
    		c == '\t' ||
    		c == '\v' ||
    		c == '\r' ||
    		c == '\n' ||
    		c == '\0')
    		return 1;
    	return 0;
    }
    
    char *dm_strword(char *src, char *str)
    {
    	char *ret = src;
    	int len;
    	if (src[0] == '\0')
    		return NULL;
    	len = strlen(str);
    	while ((ret = strstr(ret, str)) != NULL) {
    		if ((ret == src && isword_delim(ret[len])) ||
    			(ret != src && isword_delim(ret[len]) && isword_delim(*(ret - 1))))
    			return ret;
    		ret++;
    	}
    	return NULL;
    }
    
    char **strsplit(const char* str, const char* delim, size_t* numtokens)
    {
        // copy the original string so that we don't overwrite parts of it
        // (don't do this if you don't need to keep the old line,
        // as this is less efficient)
        char *s = strdup(str);
        // these three variables are part of a very common idiom to
        // implement a dynamically-growing array
        size_t tokens_alloc = 1;
        size_t tokens_used = 0;
        char **tokens = calloc(tokens_alloc, sizeof(char*));
        char *token, *strtok_ctx;
        for (token = strtok_r(s, delim, &strtok_ctx);
                token != NULL;
                token = strtok_r(NULL, delim, &strtok_ctx)) {
            // check if we need to allocate more space for tokens
            if (tokens_used == tokens_alloc) {
                tokens_alloc *= 2;
                tokens = realloc(tokens, tokens_alloc * sizeof(char*));
            }
            tokens[tokens_used++] = strdup(token);
        }
        // cleanup
        if (tokens_used == 0) {
            free(tokens);
            tokens = NULL;
        } else {
            tokens = realloc(tokens, tokens_used * sizeof(char*));
        }
        *numtokens = tokens_used;
        free(s);
        return tokens;
    }
    
    char *get_macaddr(char *interface_name)
    {
    	json_object *res;
    	char *device, *mac = "";
    
    	dmubus_call("network.interface", "status", UBUS_ARGS{{"interface", interface_name, String}}, 1, &res);
    	device = dmjson_get_value(res, 1, "device");
    	if(device[0] == '\0')
    		return "";
    	dmubus_call("network.device", "status", UBUS_ARGS{{"name", device, String}}, 1, &res);
    	mac = dmjson_get_value(res, 1, "macaddr");
    	return mac;
    }
    
    char *get_device(char *interface_name)
    {
    	json_object *res;
    	char *device = "";
    
    	dmubus_call("network.interface", "status", UBUS_ARGS{{"interface", interface_name, String}}, 1, &res);
    	device = dmjson_get_value(res, 1, "device");
    	return device;
    }
    
    /*
     * Manage string lists
     */
    
    int is_elt_exit_in_str_list(char *str_list, char *elt)
    {
    	char *pch, *spch, *list;
    	list= dmstrdup(str_list);
    	for (pch = strtok_r(list, " ", &spch); pch != NULL; pch = strtok_r(NULL, " ", &spch)) {
    		if(strcmp(pch, elt) == 0)
    			return 1;
    	}
    	return 0;
    }
    
    void add_elt_to_str_list(char **str_list, char *elt)
    {
    	char *list= NULL;
    	if(*str_list == NULL || strlen(*str_list) == 0){
    		dmasprintf(str_list, "%s", elt);
    		return;
    	}
    	list= dmstrdup(*str_list);
    	dmfree(*str_list);
    	*str_list= NULL;
    	dmasprintf(str_list, "%s %s", list, elt);
    }
    
    void remove_elt_from_str_list(char **iface_list, char *ifname)
    {
    	char *list= NULL, *tmp=NULL;
    	char *pch, *spch;
    	if (*iface_list == NULL || strlen(*iface_list) == 0)
    		return;
    	list= dmstrdup(*iface_list);
    	dmfree(*iface_list);
    	*iface_list= NULL;
    	for (pch = strtok_r(list, " ", &spch); pch != NULL; pch = strtok_r(NULL, " ", &spch)) {
    		if(strcmp(pch, ifname) == 0)
    			continue;
    		if(tmp == NULL)
    			dmasprintf(iface_list, "%s", pch);
    		else
    			dmasprintf(iface_list, "%s %s", tmp, pch);
    		if(tmp){
    			dmfree(tmp);
    			tmp= NULL;
    		}
    		if(*iface_list){
    			tmp= dmstrdup(*iface_list);
    			dmfree(*iface_list);
    			*iface_list= NULL;
    		}
    	}
    	dmasprintf(iface_list, "%s", tmp);
    }
    
    int is_array_elt_exist(char **str_array, char *str, int length)
    {
    	int i;
    
    	for(i=0; i<length; i++){
    		if(strcmp(str_array[i], str) == 0)
    			return 1;
    	}
    	return 0;
    }
    
    int get_shift_time_time(int shift_time, char *local_time, int size)
    {
    	time_t t_time;
    	struct tm *t_tm;
    
    	t_time = time(NULL) + shift_time;
    	t_tm = localtime(&t_time);
    	if (t_tm == NULL)
    		return -1;
    
    	if(strftime(local_time, size, "%FT%T%z", t_tm) == 0)
    		return -1;
    
    	local_time[25] = local_time[24];
    	local_time[24] = local_time[23];
    	local_time[22] = ':';
    	local_time[26] = '\0';
    
    	return 0;
    }
    
    int get_shift_time_shift(char *local_time, char *shift)
    {
    	struct tm tm = {0};
    
    	strptime(local_time,"%FT%T", &tm);
    	sprintf(shift, "%u", (unsigned int)(mktime(&tm) - time(NULL)));
    
    	return 0;
    }
    
    int get_stats_from_ifconfig_command(char *device, char *direction, char *option)
    {
    	char buf[1024], *pch, *pchr, *ret;
    	int pp, r, stats = 0;
    
    	pp = dmcmd("ifconfig", 1, device);
    	if (pp) {
    		r = dmcmd_read(pp, buf, 1024);
    		for(pch = strtok_r(buf, "\n", &pchr); pch != NULL; pch = strtok_r(NULL, "\n", &pchr)) {
    			if(!strstr(pch, direction))
    				continue;
    			ret = strstr(pch, option);
    			if(ret) {
    				strtok_r(ret, ":", &ret);
    				sscanf(ret, "%d", &stats);
    				break;
    			}
    		}
    		close(pp);
    	}
    	return stats;
    }
    
    int command_exec_output_to_array(char *cmd, char **output, int *length)
    {
    	FILE *fp;
    	char out[1035];
    	int i = 0;
    
    	/* Open the command for reading. */
    	fp = popen(cmd, "r");
    	if (fp == NULL)
    		return 0;
    
    	/* Read the output line by line and store it in output array. */
    	while (fgets(out, sizeof(out)-1, fp) != NULL)
    		asprintf(&output[i++], "%s", out);
    
    	*length = i;
    
    	/* close */
    	pclose(fp);
    
    	return 0;
    }
    
    char* int_period_to_date_time_format(int time)
    {
    	char *datetime;
    	int seconds, minutes, hours, days;
    	minutes= time/60;
    	seconds= time%60;
    	hours= minutes/60;
    	minutes= minutes%60;
        days= hours/24;
        hours= hours%24;
        dmasprintf(&datetime, "%dT%d:%d:%d", days, hours, minutes, seconds);
    	return datetime;
    }
    
    
    int isfileexist(char *filepath){
    	if( access( filepath, F_OK ) != -1 )
    	    return 1;
    	else
    		return 0;
    }
    
    int isfolderexist(char *folderpath){
    	DIR* dir = opendir(folderpath);
    	if (dir) {
    	    closedir(dir);
    	    return 1;
    	} else
    		return 0;
    }
    
    
    int copy_temporary_file_to_original_file(char *f1, char *f2)
    {
    	FILE *fp, *ftmp;
    	char buf[512];
    
    	ftmp = fopen(f2, "r");
    	if (ftmp == NULL)
    		return 0;
    
    	fp = fopen(f1, "w");
    	if (fp == NULL) {
    	  fclose(ftmp);
    	  return 0;
    	}
    
    	while( fgets(buf, 512, ftmp) !=NULL )
    	{
    		fprintf(fp, "%s", buf);
    	}
    	fclose(ftmp);
    	fclose(fp);
    	return 1;
    }
    
    
    char* readFileContent(char *filepath){
    	char *str=NULL, *tmp= NULL, *res= NULL;
    	FILE *f = fopen(filepath, "rb");
    
    
    	if(f==NULL)
    		return "";
    	fseek(f, 0, SEEK_END);
    	long fsize = ftell(f);
    	fseek(f, 0, SEEK_SET); //same as rewind(f);
    
    	char *filecontent = malloc(fsize + 1);
    	fread(filecontent, 1, fsize, f);
    	fclose(f);
    
    	filecontent[fsize] = 0;
    
    	for(i=0; i<strlen(filecontent); i++){
    		if(filecontent[i]<48 || (filecontent[i]>57 && filecontent[i]<65) || (filecontent[i]>90 && filecontent[i]<91) || filecontent[i]>122){
    			if(!j)
    				continue;
    			else
    				break;
    		}
    		if (!tmp){
    			dmasprintf(&str, "%c", filecontent[i]);
    			j++;
    			tmp= dmstrdup(str);
    			continue;
    		}
    		j++;
    		dmasprintf(&str, "%s%c", tmp, filecontent[i]);
    		if(tmp){
    			free(tmp);
    			tmp= NULL;
    		}
    		tmp= dmstrdup(str);
    		if(str!=NULL){
    			free(str);
    			str= NULL;
    		}
    	}
    	res= (char *)malloc((j+1)*sizeof(char));
    	strcpy(res, tmp);
    	res[j]= 0;
    	return res;
    
    
    }
    
    void writeFileContent(const char *filepath, const char *data)
    {
        FILE *fp = fopen(filepath, "ab");
        if (fp != NULL)
        {
            fputs(data, fp);
            fclose(fp);
        }
    }