Newer
Older
{
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)
{
return s;
}
return NULL;
}
struct uci_section *get_dup_section_in_dmmap_opt(char *dmmap_package, char *section_type, char *opt_name, char *opt_value)
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", §);
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", §);
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)
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
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){
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){
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
*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);
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
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) {
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
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;
}
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
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");

Omar Kallel
committed
int i, j= 0;
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;

Omar Kallel
committed
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
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;