Skip to content

Commit

Permalink
Fix usage of wrong directory to load DMF files (dirname was not actua…
Browse files Browse the repository at this point in the history
…lly prepended); str.c : added str_concat()
  • Loading branch information
jimklimov committed May 17, 2016
1 parent 931ea7d commit d5237b6
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 19 deletions.
5 changes: 3 additions & 2 deletions clients/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ upsmon_SOURCES = upsmon.c upsmon.h upsclient.h
if WITH_NEON
if WITH_SNMP
nutdmfsnmp_reindex_SOURCES = nutdmfsnmp-reindex.c $(top_srcdir)/include/dmfsnmp.h
nutdmfsnmp_reindex_LDADD = $(top_builddir)/common/libnutdmfsnmp.la
# $(LIBNETSNMP_LIBS) $(LIBNEON_LIBS)
nutdmfsnmp_reindex_LDADD = $(top_builddir)/common/libnutdmfsnmp.la \
$(top_builddir)/common/libcommon.la $(top_builddir)/common/libparseconf.la \
$(LIBNETSNMP_LIBS) $(LIBNEON_LIBS)
nutdmfsnmp_reindex_CFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/drivers \
-I$(top_srcdir)/tools/nut-scanner $(LIBNETSNMP_CFLAGS) $(LIBNEON_CFLAGS)
endif
Expand Down
27 changes: 14 additions & 13 deletions common/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
AM_CFLAGS = -I$(top_srcdir)/include

noinst_LTLIBRARIES = libparseconf.la libcommon.la libcommonclient.la
libparseconf_la_SOURCES = parseconf.c

# do not hard depend on '../include/nut_version.h', since it blocks
# 'dist', and is only required for actual build, in which case
# BUILT_SOURCES (in ../include) will ensure nut_version.h will
# be built before anything else
libcommon_la_SOURCES = common.c state.c str.c upsconf.c
libcommonclient_la_SOURCES = common.c state.c str.c
# ensure inclusion of local implementation of missing systems functions
# using LTLIBOBJS. Refer to configure.in -> AC_REPLACE_FUNCS
libcommon_la_LIBADD = libparseconf.la @LTLIBOBJS@
libcommonclient_la_LIBADD = libparseconf.la @LTLIBOBJS@

if WITH_NEON
if WITH_SNMP
# Naming may be clumsy, but the current intention is that DMF technique
Expand All @@ -16,18 +29,6 @@ if WITH_SNMP
libnutdmfsnmp_la_CFLAGS = $(AM_CFLAGS) -I$(top_srcdir)/drivers \
-I$(top_srcdir)/tools/nut-scanner \
$(LIBNETSNMP_CFLAGS) $(LIBNEON_CFLAGS)
libnutdmfsnmp_la_LIBADD = $(LIBNETSNMP_LIBS) $(LIBNEON_LIBS)
libnutdmfsnmp_la_LIBADD = $(LIBNETSNMP_LIBS) $(LIBNEON_LIBS) libcommon.la
endif
endif
libparseconf_la_SOURCES = parseconf.c

# do not hard depend on '../include/nut_version.h', since it blocks
# 'dist', and is only required for actual build, in which case
# BUILT_SOURCES (in ../include) will ensure nut_version.h will
# be built before anything else
libcommon_la_SOURCES = common.c state.c str.c upsconf.c
libcommonclient_la_SOURCES = common.c state.c str.c
# ensure inclusion of local implementation of missing systems functions
# using LTLIBOBJS. Refer to configure.in -> AC_REPLACE_FUNCS
libcommon_la_LIBADD = libparseconf.la @LTLIBOBJS@
libcommonclient_la_LIBADD = libparseconf.la @LTLIBOBJS@
6 changes: 5 additions & 1 deletion common/dmfsnmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <assert.h>

#include "dmfsnmp.h"
#include "str.h"

/*
*
Expand Down Expand Up @@ -1380,7 +1381,10 @@ mibdmf_parse_dir (char *dir_name, mibdmf_parser_t *dmp)
if ( strstr(dir_ent->d_name, ".dmf") )
{
i++;
int res = mibdmf_parse_file(dir_ent->d_name, dmp);
char *file_path = str_concat(3, dir_name, "/", dir_ent->d_name);
assert(file_path);
int res = mibdmf_parse_file(file_path, dmp);
free(file_path);
if ( res != 0 )
{
x++;
Expand Down
38 changes: 38 additions & 0 deletions common/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h> // va_*

#include "str.h"

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

return 1;
}

/* Based on code by "mmdemirbas" posted "Jul 9 '12 at 11:41" to forum page
* http://stackoverflow.com/questions/8465006/how-to-concatenate-2-strings-in-c
* This concatenates the given number of strings into one freshly allocated
* heap object; NOTE that it is up to the caller to free the object afterwards.
*/
char * str_concat(size_t count, ...)
{
va_list ap;
size_t i, len, null_pos;
char* merged = NULL;

// Find required length to store merged string
va_start(ap, count);
len = 1; // room for '\0' in the end
for(i=0 ; i<count ; i++)
len += strlen(va_arg(ap, char*));
va_end(ap);

// Allocate memory to concat strings
merged = (char*)calloc(len,sizeof(char));
if (merged == NULL)
return merged;

// Actually concatenate strings
va_start(ap, count);
null_pos = 0;
for(i=0 ; i<count ; i++)
{
char *s = va_arg(ap, char*);
strcpy(merged+null_pos, s);
null_pos += strlen(s);
}
va_end(ap);

return merged;
}
4 changes: 4 additions & 0 deletions include/str.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ int str_to_ulong_strict(const char *string, unsigned long *number, const int bas
int str_to_double(const char *string, double *number, const int base);
int str_to_double_strict(const char *string, double *number, const int base);

/* Concatenates "count" strings into a dynamically allocated object which
* the caller can use and must free() later on */
char * str_concat(size_t count, ...);

#ifdef __cplusplus
/* *INDENT-OFF* */
}
Expand Down
6 changes: 3 additions & 3 deletions scripts/DMF/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,21 @@ progs: $(PROGS)

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

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

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

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

0 comments on commit d5237b6

Please sign in to comment.