Discussion:
[Linuxptp-devel] [PATCH 2/4] Don't include config.h in util.h
Miroslav Lichvar
2014-10-02 08:38:32 UTC
Permalink
The config module is used by ptp4l only, but util is shared with other
programs.

Signed-off-by: Miroslav Lichvar <***@redhat.com>
---
config.h | 8 --------
phc_ctl.c | 1 +
util.h | 12 +++++++++++-
3 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/config.h b/config.h
index 9b74f12..d580496 100644
--- a/config.h
+++ b/config.h
@@ -51,14 +51,6 @@ struct interface {
#define CFG_IGNORE_USE_SYSLOG (1 << 5)
#define CFG_IGNORE_VERBOSE (1 << 6)

-enum parser_result {
- PARSED_OK,
- NOT_PARSED,
- BAD_VALUE,
- MALFORMED,
- OUT_OF_RANGE,
-};
-
struct config {
/* configuration override */
int cfg_ignore;
diff --git a/phc_ctl.c b/phc_ctl.c
index dc9c29c..461f2ac 100644
--- a/phc_ctl.c
+++ b/phc_ctl.c
@@ -43,6 +43,7 @@
#include "missing.h"
#include "phc.h"
#include "print.h"
+#include "sk.h"
#include "sysoff.h"
#include "util.h"
#include "version.h"
diff --git a/util.h b/util.h
index cf05e49..bb29e11 100644
--- a/util.h
+++ b/util.h
@@ -20,7 +20,6 @@
#ifndef HAVE_UTIL_H
#define HAVE_UTIL_H

-#include "config.h"
#include "ddt.h"

/**
@@ -126,6 +125,17 @@ int is_utc_ambiguous(uint64_t ts);
int leap_second_status(uint64_t ts, int leap_set, int *leap, int *utc_offset);

/**
+ * Values returned by get_ranged_*().
+ */
+enum parser_result {
+ PARSED_OK,
+ NOT_PARSED,
+ BAD_VALUE,
+ MALFORMED,
+ OUT_OF_RANGE,
+};
+
+/**
* Get an integer value from string with error checking and range
* specification.
*
--
1.9.3
Miroslav Lichvar
2014-10-02 08:38:31 UTC
Permalink
Only reentrant functions should be called here.

Signed-off-by: Miroslav Lichvar <***@redhat.com>
---
util.c | 1 -
1 file changed, 1 deletion(-)

diff --git a/util.c b/util.c
index ae66bb1..cb428b1 100644
--- a/util.c
+++ b/util.c
@@ -318,7 +318,6 @@ int get_arg_val_d(int op, const char *optarg, double *val,

static void handle_int_quit_term(int s)
{
- pr_notice("caught signal %d", s);
running = 0;
}
--
1.9.3
Miroslav Lichvar
2014-10-02 08:38:33 UTC
Permalink
Add some functions to work with strings and arrays of pointers that will
be useful later.

Signed-off-by: Miroslav Lichvar <***@redhat.com>
---
incdefs.sh | 3 ++
util.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
util.h | 57 +++++++++++++++++++++++++++++++++++++
3 files changed, 155 insertions(+)

diff --git a/incdefs.sh b/incdefs.sh
index cf00eaf..f4c0c84 100755
--- a/incdefs.sh
+++ b/incdefs.sh
@@ -23,6 +23,9 @@
#
user_flags()
{
+ # Needed for vasprintf().
+ printf " -D_GNU_SOURCE"
+
dirs=$(echo "" | ${CROSS_COMPILE}cpp -Wp,-v 2>&1 >/dev/null | grep ^" /")
for d in $dirs; do
files=$(find $d -type f -name time.h)
diff --git a/util.c b/util.c
index cb428b1..06c3296 100644
--- a/util.c
+++ b/util.c
@@ -18,6 +18,7 @@
*/
#include <errno.h>
#include <signal.h>
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -342,3 +343,97 @@ int is_running(void)
{
return running;
}
+
+char *string_newf(const char *format, ...)
+{
+ va_list ap;
+ char *s;
+
+ va_start(ap, format);
+ if (vasprintf(&s, format, ap) < 0)
+ s = NULL;
+ va_end(ap);
+
+ return s;
+}
+
+void string_append(char **s, const char *str)
+{
+ size_t len1, len2;
+
+ len1 = strlen(*s);
+ len2 = strlen(str);
+ *s = realloc(*s, len1 + len2 + 1);
+ if (*s)
+ memcpy((*s) + len1, str, len2 + 1);
+}
+
+void string_appendf(char **s, const char *format, ...)
+{
+ va_list ap;
+ size_t len1, len2;
+ char *s2;
+
+ len1 = strlen(*s);
+
+ va_start(ap, format);
+ len2 = vasprintf(&s2, format, ap);
+ va_end(ap);
+
+ if (len2 < 0) {
+ *s = NULL;
+ return;
+ }
+
+ *s = realloc(*s, len1 + len2 + 1);
+ if (*s)
+ memcpy((*s) + len1, s2, len2 + 1);
+ free(s2);
+}
+
+void **parray_new(void)
+{
+ void **a = malloc(sizeof(*a));
+
+ if (a)
+ *a = NULL;
+ return a;
+}
+
+void parray_append(void ***a, void *p)
+{
+ parray_extend(a, p, NULL);
+}
+
+void parray_extend(void ***a, ...)
+{
+ va_list ap;
+ int ilen, len, alloced;
+ void *p;
+
+ for (len = 0; (*a)[len]; len++)
+ ;
+ len++;
+
+ va_start(ap, a);
+ for (ilen = 0; va_arg(ap, void *); ilen++)
+ ;
+ va_end(ap);
+
+ /* Reallocate in exponentially increasing sizes. */
+ for (alloced = 1; alloced < len; alloced <<= 1)
+ ;
+ if (alloced < len + ilen) {
+ while (alloced < len + ilen)
+ alloced *= 2;
+ *a = realloc(*a, alloced * sizeof **a);
+ if (!*a)
+ return;
+ }
+
+ va_start(ap, a);
+ while ((p = va_arg(ap, void *)))
+ (*a)[len++ - 1] = p;
+ va_end(ap);
+ (*a)[len - 1] = NULL;
+}
diff --git a/util.h b/util.h
index bb29e11..98b395b 100644
--- a/util.h
+++ b/util.h
@@ -236,4 +236,61 @@ int handle_term_signals(void);
*/
int is_running(void);

+/**
+ * Get an allocated and formatted string. This is a wrapper around asprintf().
+ *
+ * @param format printf() format string.
+ * @param ... printf() arguments.
+ * @return Pointer to the allocated string, NULL on error.
+ */
+#ifdef __GNUC__
+__attribute__ ((format (printf, 1, 2)))
+#endif
+char *string_newf(const char *format, ...);
+
+/**
+ * Reallocate a string and append another string to it.
+ *
+ * @param s String that should be extended, set to NULL on error.
+ * @param str String appended to s.
+ */
+void string_append(char **s, const char *str);
+#ifdef __GNUC__
+__attribute__ ((format (printf, 2, 3)))
+#endif
+/**
+ * Reallocate a string and append a formatted string to it.
+ *
+ * @param s String that should be extended, set to NULL on error.
+ * @param format printf() format string.
+ * @param ... printf() arguments.
+ */
+void string_appendf(char **s, const char *format, ...);
+
+/**
+ * Get an empty array of pointers terminated by NULL.
+ *
+ * @return Pointer to the allocated array, NULL on error.
+ */
+void **parray_new(void);
+
+/**
+ * Append pointer to a NULL-terminated pointer array. The array is reallocated
+ * in exponentially increasing sizes.
+ *
+ * @param a Pointer to pointer array, set to NULL on error.
+ * @param p Pointer appended to the array.
+ */
+void parray_append(void ***a, void *p);
+
+
+/**
+ * Append pointers to a NULL-terminated pointer array. The array is reallocated
+ * in exponentially increasing sizes.
+ *
+ * @param a Pointer to pointer array, set to NULL on error.
+ * @param ... NULL-terminated list of pointers.
+ */
+void parray_extend(void ***a, ...);
+
#endif
--
1.9.3
Richard Cochran
2014-10-03 07:55:33 UTC
Permalink
Post by Miroslav Lichvar
Add some functions to work with strings and arrays of pointers that will
be useful later.
---
incdefs.sh | 3 ++
util.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
util.h | 57 +++++++++++++++++++++++++++++++++++++
3 files changed, 155 insertions(+)
diff --git a/incdefs.sh b/incdefs.sh
index cf00eaf..f4c0c84 100755
--- a/incdefs.sh
+++ b/incdefs.sh
@@ -23,6 +23,9 @@
#
user_flags()
{
+ # Needed for vasprintf().
+ printf " -D_GNU_SOURCE"
But then we have this twice, if clock_adjtime() is found in the
following test.

(V3 will get through the size filter, I promise ;)

Thanks,
Richard
Post by Miroslav Lichvar
+
dirs=$(echo "" | ${CROSS_COMPILE}cpp -Wp,-v 2>&1 >/dev/null | grep ^" /")
for d in $dirs; do
files=$(find $d -type f -name time.h)
diff --git a/util.c b/util.c
index cb428b1..06c3296 100644
--- a/util.c
+++ b/util.c
@@ -18,6 +18,7 @@
*/
#include <errno.h>
#include <signal.h>
+#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -342,3 +343,97 @@ int is_running(void)
{
return running;
}
+
+char *string_newf(const char *format, ...)
+{
+ va_list ap;
+ char *s;
+
+ va_start(ap, format);
+ if (vasprintf(&s, format, ap) < 0)
+ s = NULL;
+ va_end(ap);
+
+ return s;
+}
+
+void string_append(char **s, const char *str)
+{
+ size_t len1, len2;
+
+ len1 = strlen(*s);
+ len2 = strlen(str);
+ *s = realloc(*s, len1 + len2 + 1);
+ if (*s)
+ memcpy((*s) + len1, str, len2 + 1);
+}
+
+void string_appendf(char **s, const char *format, ...)
+{
+ va_list ap;
+ size_t len1, len2;
+ char *s2;
+
+ len1 = strlen(*s);
+
+ va_start(ap, format);
+ len2 = vasprintf(&s2, format, ap);
+ va_end(ap);
+
+ if (len2 < 0) {
+ *s = NULL;
+ return;
+ }
+
+ *s = realloc(*s, len1 + len2 + 1);
+ if (*s)
+ memcpy((*s) + len1, s2, len2 + 1);
+ free(s2);
+}
+
+void **parray_new(void)
+{
+ void **a = malloc(sizeof(*a));
+
+ if (a)
+ *a = NULL;
+ return a;
+}
+
+void parray_append(void ***a, void *p)
+{
+ parray_extend(a, p, NULL);
+}
+
+void parray_extend(void ***a, ...)
+{
+ va_list ap;
+ int ilen, len, alloced;
+ void *p;
+
+ for (len = 0; (*a)[len]; len++)
+ ;
+ len++;
+
+ va_start(ap, a);
+ for (ilen = 0; va_arg(ap, void *); ilen++)
+ ;
+ va_end(ap);
+
+ /* Reallocate in exponentially increasing sizes. */
+ for (alloced = 1; alloced < len; alloced <<= 1)
+ ;
+ if (alloced < len + ilen) {
+ while (alloced < len + ilen)
+ alloced *= 2;
+ *a = realloc(*a, alloced * sizeof **a);
+ if (!*a)
+ return;
+ }
+
+ va_start(ap, a);
+ while ((p = va_arg(ap, void *)))
+ (*a)[len++ - 1] = p;
+ va_end(ap);
+ (*a)[len - 1] = NULL;
+}
diff --git a/util.h b/util.h
index bb29e11..98b395b 100644
--- a/util.h
+++ b/util.h
@@ -236,4 +236,61 @@ int handle_term_signals(void);
*/
int is_running(void);
+/**
+ * Get an allocated and formatted string. This is a wrapper around asprintf().
+ *
+ */
+#ifdef __GNUC__
+__attribute__ ((format (printf, 1, 2)))
+#endif
+char *string_newf(const char *format, ...);
+
+/**
+ * Reallocate a string and append another string to it.
+ *
+ */
+void string_append(char **s, const char *str);
+#ifdef __GNUC__
+__attribute__ ((format (printf, 2, 3)))
+#endif
+/**
+ * Reallocate a string and append a formatted string to it.
+ *
+ */
+void string_appendf(char **s, const char *format, ...);
+
+/**
+ * Get an empty array of pointers terminated by NULL.
+ *
+ */
+void **parray_new(void);
+
+/**
+ * Append pointer to a NULL-terminated pointer array. The array is reallocated
+ * in exponentially increasing sizes.
+ *
+ */
+void parray_append(void ***a, void *p);
+
+
+/**
+ * Append pointers to a NULL-terminated pointer array. The array is reallocated
+ * in exponentially increasing sizes.
+ *
+ */
+void parray_extend(void ***a, ...);
+
#endif
--
1.9.3
------------------------------------------------------------------------------
Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports
Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk
_______________________________________________
Linuxptp-devel mailing list
https://lists.sourceforge.net/lists/listinfo/linuxptp-devel
Miroslav Lichvar
2014-10-02 11:59:39 UTC
Permalink
This was larger than then 40KB list limit, sending again as a
compressed attachment.
--
Miroslav Lichvar
Richard Cochran
2014-10-02 12:20:23 UTC
Permalink
Post by Miroslav Lichvar
This was larger than then 40KB list limit, sending again as a
compressed attachment.
Sorry about that. I bumped the limit up to 100k now in mailman.

Thanks,
Richard
Loading...