Skip to content

Commit d5237b6

Browse files
committed
Fix usage of wrong directory to load DMF files (dirname was not actually prepended); str.c : added str_concat()
1 parent 931ea7d commit d5237b6

6 files changed

Lines changed: 67 additions & 19 deletions

File tree

clients/Makefile.am

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ upsmon_SOURCES = upsmon.c upsmon.h upsclient.h
4444
if WITH_NEON
4545
if WITH_SNMP
4646
nutdmfsnmp_reindex_SOURCES = nutdmfsnmp-reindex.c $(top_srcdir)/include/dmfsnmp.h
47-
nutdmfsnmp_reindex_LDADD = $(top_builddir)/common/libnutdmfsnmp.la
48-
# $(LIBNETSNMP_LIBS) $(LIBNEON_LIBS)
47+
nutdmfsnmp_reindex_LDADD = $(top_builddir)/common/libnutdmfsnmp.la \
48+
$(top_builddir)/common/libcommon.la $(top_builddir)/common/libparseconf.la \
49+
$(LIBNETSNMP_LIBS) $(LIBNEON_LIBS)
4950
nutdmfsnmp_reindex_CFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/drivers \
5051
-I$(top_srcdir)/tools/nut-scanner $(LIBNETSNMP_CFLAGS) $(LIBNEON_CFLAGS)
5152
endif

common/Makefile.am

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
AM_CFLAGS = -I$(top_srcdir)/include
44

55
noinst_LTLIBRARIES = libparseconf.la libcommon.la libcommonclient.la
6+
libparseconf_la_SOURCES = parseconf.c
7+
8+
# do not hard depend on '../include/nut_version.h', since it blocks
9+
# 'dist', and is only required for actual build, in which case
10+
# BUILT_SOURCES (in ../include) will ensure nut_version.h will
11+
# be built before anything else
12+
libcommon_la_SOURCES = common.c state.c str.c upsconf.c
13+
libcommonclient_la_SOURCES = common.c state.c str.c
14+
# ensure inclusion of local implementation of missing systems functions
15+
# using LTLIBOBJS. Refer to configure.in -> AC_REPLACE_FUNCS
16+
libcommon_la_LIBADD = libparseconf.la @LTLIBOBJS@
17+
libcommonclient_la_LIBADD = libparseconf.la @LTLIBOBJS@
18+
619
if WITH_NEON
720
if WITH_SNMP
821
# Naming may be clumsy, but the current intention is that DMF technique
@@ -16,18 +29,6 @@ if WITH_SNMP
1629
libnutdmfsnmp_la_CFLAGS = $(AM_CFLAGS) -I$(top_srcdir)/drivers \
1730
-I$(top_srcdir)/tools/nut-scanner \
1831
$(LIBNETSNMP_CFLAGS) $(LIBNEON_CFLAGS)
19-
libnutdmfsnmp_la_LIBADD = $(LIBNETSNMP_LIBS) $(LIBNEON_LIBS)
32+
libnutdmfsnmp_la_LIBADD = $(LIBNETSNMP_LIBS) $(LIBNEON_LIBS) libcommon.la
2033
endif
2134
endif
22-
libparseconf_la_SOURCES = parseconf.c
23-
24-
# do not hard depend on '../include/nut_version.h', since it blocks
25-
# 'dist', and is only required for actual build, in which case
26-
# BUILT_SOURCES (in ../include) will ensure nut_version.h will
27-
# be built before anything else
28-
libcommon_la_SOURCES = common.c state.c str.c upsconf.c
29-
libcommonclient_la_SOURCES = common.c state.c str.c
30-
# ensure inclusion of local implementation of missing systems functions
31-
# using LTLIBOBJS. Refer to configure.in -> AC_REPLACE_FUNCS
32-
libcommon_la_LIBADD = libparseconf.la @LTLIBOBJS@
33-
libcommonclient_la_LIBADD = libparseconf.la @LTLIBOBJS@

common/dmfsnmp.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <assert.h>
3030

3131
#include "dmfsnmp.h"
32+
#include "str.h"
3233

3334
/*
3435
*
@@ -1380,7 +1381,10 @@ mibdmf_parse_dir (char *dir_name, mibdmf_parser_t *dmp)
13801381
if ( strstr(dir_ent->d_name, ".dmf") )
13811382
{
13821383
i++;
1383-
int res = mibdmf_parse_file(dir_ent->d_name, dmp);
1384+
char *file_path = str_concat(3, dir_name, "/", dir_ent->d_name);
1385+
assert(file_path);
1386+
int res = mibdmf_parse_file(file_path, dmp);
1387+
free(file_path);
13841388
if ( res != 0 )
13851389
{
13861390
x++;

common/str.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <limits.h>
2626
#include <stdlib.h>
2727
#include <string.h>
28+
#include <stdarg.h> // va_*
2829

2930
#include "str.h"
3031

@@ -605,3 +606,40 @@ int str_to_double_strict(const char *string, double *number, const int base)
605606

606607
return 1;
607608
}
609+
610+
/* Based on code by "mmdemirbas" posted "Jul 9 '12 at 11:41" to forum page
611+
* http://stackoverflow.com/questions/8465006/how-to-concatenate-2-strings-in-c
612+
* This concatenates the given number of strings into one freshly allocated
613+
* heap object; NOTE that it is up to the caller to free the object afterwards.
614+
*/
615+
char * str_concat(size_t count, ...)
616+
{
617+
va_list ap;
618+
size_t i, len, null_pos;
619+
char* merged = NULL;
620+
621+
// Find required length to store merged string
622+
va_start(ap, count);
623+
len = 1; // room for '\0' in the end
624+
for(i=0 ; i<count ; i++)
625+
len += strlen(va_arg(ap, char*));
626+
va_end(ap);
627+
628+
// Allocate memory to concat strings
629+
merged = (char*)calloc(len,sizeof(char));
630+
if (merged == NULL)
631+
return merged;
632+
633+
// Actually concatenate strings
634+
va_start(ap, count);
635+
null_pos = 0;
636+
for(i=0 ; i<count ; i++)
637+
{
638+
char *s = va_arg(ap, char*);
639+
strcpy(merged+null_pos, s);
640+
null_pos += strlen(s);
641+
}
642+
va_end(ap);
643+
644+
return merged;
645+
}

include/str.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ int str_to_ulong_strict(const char *string, unsigned long *number, const int bas
120120
int str_to_double(const char *string, double *number, const int base);
121121
int str_to_double_strict(const char *string, double *number, const int base);
122122

123+
/* Concatenates "count" strings into a dynamically allocated object which
124+
* the caller can use and must free() later on */
125+
char * str_concat(size_t count, ...);
126+
123127
#ifdef __cplusplus
124128
/* *INDENT-OFF* */
125129
}

scripts/DMF/Makefile.am

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,21 @@ progs: $(PROGS)
6161

6262
progs-all: $(PROGS) $(PROGS_EXPERIMENTAL)
6363

64-
dmf-test: dmf-test.c $(top_srcdir)/common/dmfsnmp.c
64+
dmf-test: dmf-test.c $(top_srcdir)/common/dmfsnmp.c $(top_srcdir)/common/src.c
6565
$(CC) -ggdb -std=c11 -std=gnu99 -Werror -Wall -pedantic -Wc++-compat \
6666
-D_FORTIFY_SOURCE=2 -O -fstack-protector \
6767
-lneon $(AM_CFLAGS) \
6868
-DDEBUG=1 \
6969
-o $@ $^
7070

71-
dmf-reindex: ../../clients/nutdmfsnmp-reindex.c ../../common/dmfsnmp.c
71+
dmf-reindex: $(top_srcdir)/clients/nutdmfsnmp-reindex.c $(top_srcdir)/common/dmfsnmp.c $(top_srcdir)/common/src.c
7272
$(CC) -ggdb -std=c11 -std=gnu99 -Werror -Wall -pedantic -Wc++-compat \
7373
-D_FORTIFY_SOURCE=2 -O -fstack-protector \
7474
-lneon $(AM_CFLAGS) \
7575
-DDEBUG=1 \
7676
-o $@ $^
7777

78-
dmf-lua-test: dmf-test.c ../../common/dmfsnmp.c
78+
dmf-lua-test: dmf-test.c $(top_srcdir)/common/dmfsnmp.c $(top_srcdir)/common/src.c
7979
$(CC) -ggdb -std=c11 -std=gnu99 -Werror -Wall -pedantic -Wc++-compat \
8080
-D_FORTIFY_SOURCE=2 -O -fstack-protector \
8181
-lneon $(AM_CFLAGS) \

0 commit comments

Comments
 (0)