All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH] Create a function tst_resm_hexd to print a buffer in hex values
@ 2013-05-16 12:20 Alexey Kodanev
  2013-05-20 12:16 ` chrubis
  0 siblings, 1 reply; 3+ messages in thread
From: Alexey Kodanev @ 2013-05-16 12:20 UTC (permalink / raw)
  To: ltp-list; +Cc: vasily.isaenko, Alexey Kodanev

Signed-off-by: Alexey Kodanev <alexey.kodanev@oracle.com>
---
 include/test.h |    2 ++
 lib/tst_res.c  |   34 ++++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 0 deletions(-)

diff --git a/include/test.h b/include/test.h
index e36ca38..17b8e39 100644
--- a/include/test.h
+++ b/include/test.h
@@ -191,6 +191,8 @@ void tst_res(int ttype, char *fname, char *arg_fmt, ...)
 	__attribute__ ((format (printf, 3, 4)));
 void tst_resm(int ttype, char *arg_fmt, ...)
 	__attribute__ ((format (printf, 2, 3)));
+void tst_resm_hexd(int ttype, const char *buf, size_t size, char *arg_fmt, ...)
+	__attribute__ ((format (printf, 4, 5)));
 void tst_brk(int ttype, char *fname, void (*func)(void), char *arg_fmt, ...)
 	__attribute__ ((format (printf, 4, 5)));
 void tst_brkm(int ttype, void (*func)(void), char *arg_fmt, ...)
diff --git a/lib/tst_res.c b/lib/tst_res.c
index ffac6d7..e7c9d5d 100644
--- a/lib/tst_res.c
+++ b/lib/tst_res.c
@@ -675,6 +675,40 @@ void tst_resm(int ttype, char *arg_fmt, ...)
 }
 
 /*
+ * tst_resm_hexd() - Interface to tst_res(), with no filename.
+ * Also, dump specified buffer in hex.
+ */
+void tst_resm_hexd(int ttype, const char *buf, size_t size, char *arg_fmt, ...)
+{
+	char tmesg[USERMESG];
+
+#if DEBUG
+	printf("IN tst_resm_hexd\n");
+	fflush(stdout);
+	fflush(stdout);
+#endif
+
+	EXPAND_VAR_ARGS(tmesg, arg_fmt, USERMESG);
+
+	static const size_t symb_num = 3; /* space + xx */
+
+	size_t offset = strlen(tmesg);
+
+	if ((offset + symb_num * size + 1) >= USERMESG) {
+		printf("%s: %i: line too long\n", __func__, __LINE__);
+		abort();
+	}
+
+	size_t i;
+	for (i = 0; i < size; ++i) {
+		sprintf(tmesg + offset + i * symb_num,
+			" %02x", (unsigned char)buf[i]);
+	}
+
+	tst_res(ttype, NULL, "%s", tmesg);
+}
+
+/*
  * tst_brkm() - Interface to tst_brk(), with no filename.
  */
 void tst_brkm(int ttype, void (*func) (void), char *arg_fmt, ...)
-- 
1.7.1


------------------------------------------------------------------------------
AlienVault Unified Security Management (USM) platform delivers complete
security visibility with the essential security capabilities. Easily and
efficiently configure, manage, and operate all of your security controls
from a single console and one unified framework. Download a free trial.
http://p.sf.net/sfu/alienvault_d2d
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [LTP] [PATCH] Create a function tst_resm_hexd to print a buffer in hex values
  2013-05-16 12:20 [LTP] [PATCH] Create a function tst_resm_hexd to print a buffer in hex values Alexey Kodanev
@ 2013-05-20 12:16 ` chrubis
       [not found]   ` <519A1D9F.9060204@oracle.com>
  0 siblings, 1 reply; 3+ messages in thread
From: chrubis @ 2013-05-20 12:16 UTC (permalink / raw)
  To: Alexey Kodanev; +Cc: vasily.isaenko, ltp-list

Hi!
> ---
>  include/test.h |    2 ++
>  lib/tst_res.c  |   34 ++++++++++++++++++++++++++++++++++
>  2 files changed, 36 insertions(+), 0 deletions(-)

There is doc/man3/tst_res.3 that describes the rest of the interface
that should updated as well (we should at least add a line to the NAME
and SYNOPSIS). Bonus points for cleaning up the docs before that as well
;).

> --- a/lib/tst_res.c
> +++ b/lib/tst_res.c
> @@ -675,6 +675,40 @@ void tst_resm(int ttype, char *arg_fmt, ...)
>  }
>  
>  /*
> + * tst_resm_hexd() - Interface to tst_res(), with no filename.
> + * Also, dump specified buffer in hex.
> + */
> +void tst_resm_hexd(int ttype, const char *buf, size_t size, char *arg_fmt, ...)

I think that the buffer should be better typed as 'const void *' as for
example in memcmp(3).

> +{
> +	char tmesg[USERMESG];
> +
> +#if DEBUG
> +	printf("IN tst_resm_hexd\n");
> +	fflush(stdout);
> +	fflush(stdout);

Why twice?

Ah some of the functions in the file does this too. I would expect that
this was some kind of workaround for obscure UNIX that is no longer
around us. Use only one here and we should remove the double fflush()
from the rest of the code.

> +#endif
> +
> +	EXPAND_VAR_ARGS(tmesg, arg_fmt, USERMESG);
> +
> +	static const size_t symb_num = 3; /* space + xx */
> +
> +	size_t offset = strlen(tmesg);
> +
> +	if ((offset + symb_num * size + 1) >= USERMESG) {
> +		printf("%s: %i: line too long\n", __func__, __LINE__);
> +		abort();

What about printing only the start of the buffer as:

'This is test message: 0x0F 0x04 0x33 0x44 ...'

instead of the abort?

> +	}
> +
> +	size_t i;
> +	for (i = 0; i < size; ++i) {
> +		sprintf(tmesg + offset + i * symb_num,
> +			" %02x", (unsigned char)buf[i]);
> +	}
> +
> +	tst_res(ttype, NULL, "%s", tmesg);
> +}

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
AlienVault Unified Security Management (USM) platform delivers complete
security visibility with the essential security capabilities. Easily and
efficiently configure, manage, and operate all of your security controls
from a single console and one unified framework. Download a free trial.
http://p.sf.net/sfu/alienvault_d2d
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [LTP] [PATCH] Create a function tst_resm_hexd to print a buffer in hex values
       [not found]   ` <519A1D9F.9060204@oracle.com>
@ 2013-05-20 13:15     ` chrubis
  0 siblings, 0 replies; 3+ messages in thread
From: chrubis @ 2013-05-20 13:15 UTC (permalink / raw)
  To: alexey.kodanev; +Cc: vasily.isaenko, ltp-list

Hi!
> >> +
> >> +	EXPAND_VAR_ARGS(tmesg, arg_fmt, USERMESG);
> >> +
> >> +	static const size_t symb_num = 3; /* space + xx */
> >> +
> >> +	size_t offset = strlen(tmesg);
> >> +
> >> +	if ((offset + symb_num * size + 1)>= USERMESG) {
> >> +		printf("%s: %i: line too long\n", __func__, __LINE__);
> >> +		abort();
> > What about printing only the start of the buffer as:
> >
> > 'This is test message: 0x0F 0x04 0x33 0x44 ...'
> >
> > instead of the abort?
> Yes, it's possible. Do I need to calc the header which tst_res() is 
> going to add or simply print first four bytes and check if it fits the 
> tmesg?

I would print as much as we can fit into the buffer, i.e. count leftover
space minus space for ' ...' and divide it by length of one entry (and
not to forget to accound for '\0');

> >> +	}
> >> +
> >> +	size_t i;
> >> +	for (i = 0; i<  size; ++i) {
> >> +		sprintf(tmesg + offset + i * symb_num,
> >> +			" %02x", (unsigned char)buf[i]);
> >> +	}
> >> +
> >> +	tst_res(ttype, NULL, "%s", tmesg);
> >> +}
> Also, I printing buffer without 0x prefix to make it more compact, 
> should I add it?

Sounds good.

-- 
Cyril Hrubis
chrubis@suse.cz

------------------------------------------------------------------------------
AlienVault Unified Security Management (USM) platform delivers complete
security visibility with the essential security capabilities. Easily and
efficiently configure, manage, and operate all of your security controls
from a single console and one unified framework. Download a free trial.
http://p.sf.net/sfu/alienvault_d2d
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2013-05-20 13:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-05-16 12:20 [LTP] [PATCH] Create a function tst_resm_hexd to print a buffer in hex values Alexey Kodanev
2013-05-20 12:16 ` chrubis
     [not found]   ` <519A1D9F.9060204@oracle.com>
2013-05-20 13:15     ` chrubis

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.