mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] strings: new command
@ 2016-09-17 17:07 Sam Ravnborg
  2016-09-19  8:38 ` Sam Ravnborg
  2016-09-21  8:09 ` Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Sam Ravnborg @ 2016-09-17 17:07 UTC (permalink / raw)
  To: Barebox List

From 724382cf9aa173b6ced9fd213bbb369d9a5e3739 Mon Sep 17 00:00:00 2001
From: Sam Ravnborg <sam@ravnborg.org>
Date: Sat, 17 Sep 2016 19:01:15 +0200
Subject: [PATCH 1/1] strings: new command

Implement a simple version of strings that will print
printable strings from one or more files.
Strings shall be at least 4 chars before thay are printed

Signed-off-by: Sam Ravnborg <sam@ravnborg.org>
---

I was in a situation where I missed "strings".
So here it is....
I started out with cat.c - but ended up re-writing everything.

It has seen light testing on my x86 box.
Ran it through checkpatch for good measure - fixed a few things.

The location in Makefile seems random, so I just put it next to cat.

Before applying please consider if strings is really
general useful in barebox.


	Sam


 commands/Kconfig   |  10 +++++
 commands/Makefile  |   1 +
 commands/strings.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 132 insertions(+)
 create mode 100644 commands/strings.c

diff --git a/commands/Kconfig b/commands/Kconfig
index 30bf429..2f9b5d8 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -993,6 +993,16 @@ config CMD_SHA512SUM
 
 	  Calculate a SHA512 digest over a FILE or a memory area.
 
+config CMD_STRINGS
+	tristate
+	default y
+	prompt "strings"
+	help
+	  Display printable strings in file(s) to stdout
+
+	  Usage: strings FILE...
+
+
 config CMD_UNCOMPRESS
 	bool
 	select UNCOMPRESS
diff --git a/commands/Makefile b/commands/Makefile
index 601f15f..7b11d73 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -32,6 +32,7 @@ obj-$(CONFIG_CMD_RMDIR)		+= rmdir.o
 obj-$(CONFIG_CMD_CP)		+= cp.o
 obj-$(CONFIG_CMD_RM)		+= rm.o
 obj-$(CONFIG_CMD_CAT)		+= cat.o
+obj-$(CONFIG_CMD_STRINGS)	+= strings.o
 obj-$(CONFIG_CMD_MOUNT)		+= mount.o
 obj-$(CONFIG_CMD_UMOUNT)	+= umount.o
 obj-$(CONFIG_CMD_REGINFO)	+= reginfo.o
diff --git a/commands/strings.c b/commands/strings.c
new file mode 100644
index 0000000..e1e7287
--- /dev/null
+++ b/commands/strings.c
@@ -0,0 +1,121 @@
+/*
+ * strings.c - find strings in a file
+ *
+ * Copyright (c) 2016 Sam Ravnborg <sam@ravnborg.org>
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include <command.h>
+#include <fs.h>
+
+#include <linux/ctype.h>
+
+#define BUFSIZE    (1024)
+#define STRINGBUF  (1024 - 3)
+#define MIN_STRING 4
+
+/**
+ * @param[in] argc Argument count from command line
+ * @param[in] argv List of input arguments
+ */
+static int do_strings(int argc, char *argv[])
+{
+	char *string;
+	char *buf;
+	int args;
+	int err;
+	int ret;
+	int fd;
+	int i;
+	int s;
+
+	if (argc < 2) {
+		printf("strings: no file specified\n");
+		return 1;
+	}
+
+	buf = xmalloc(BUFSIZE);
+	/* Reserve room for newline + null */
+	string = xmalloc(STRINGBUF + 3);
+
+	args = 1;
+	err = 0;
+	s = 0;
+
+	while (args < argc) {
+		fd = open(argv[args], O_RDONLY);
+		if (fd < 0) {
+			err = 1;
+			printf("could not open %s: %s\n",
+				argv[args], errno_str());
+			goto out;
+		}
+
+		while ((ret = read(fd, buf, BUFSIZE)) > 0) {
+			for (i = 0; i < ret; i++) {
+				int nonprint = 0;
+
+				if (isascii(buf[i]) && isprint(buf[i]))
+					string[s++] = buf[i];
+				else
+					nonprint = 1;
+
+				if (nonprint) {
+					if (s >= MIN_STRING || s >= STRINGBUF) {
+						string[s++] = '\n';
+						string[s++] = '\0';
+						puts(string);
+					}
+
+					s = 0;
+				}
+			}
+
+			if (s >= MIN_STRING) {
+				string[s++] = '\n';
+				string[s++] = '\0';
+				puts(string);
+			}
+			s = 0;
+
+			if (ctrlc()) {
+				err = 1;
+				close(fd);
+				goto out;
+			}
+		}
+
+		close(fd);
+		args++;
+	}
+
+out:
+	free(string);
+	free(buf);
+
+	return err;
+}
+
+BAREBOX_CMD_HELP_START(strings)
+BAREBOX_CMD_HELP_TEXT("Display strings from a file")
+BAREBOX_CMD_HELP_END
+
+BAREBOX_CMD_START(strings)
+	.cmd = do_strings,
+	BAREBOX_CMD_DESC("display strings")
+	BAREBOX_CMD_OPTS("FILE...")
+	BAREBOX_CMD_GROUP(CMD_GRP_FILE)
+	BAREBOX_CMD_HELP(cmd_strings_help)
+BAREBOX_CMD_END
-- 
1.8.3.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

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

* Re: [PATCH] strings: new command
  2016-09-17 17:07 [PATCH] strings: new command Sam Ravnborg
@ 2016-09-19  8:38 ` Sam Ravnborg
  2016-09-21  8:09 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sam Ravnborg @ 2016-09-19  8:38 UTC (permalink / raw)
  To: Barebox List

> I was in a situation where I missed "strings".
> So here it is....
> I started out with cat.c - but ended up re-writing everything.
> 
> It has seen light testing on my x86 box.
> Ran it through checkpatch for good measure - fixed a few things.
> 
> The location in Makefile seems random, so I just put it next to cat.
> 
> Before applying please consider if strings is really
> general useful in barebox.

In the end I had no use for strings in barebox, so another reason
to consider if it is really needed.

	Sam

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

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

* Re: [PATCH] strings: new command
  2016-09-17 17:07 [PATCH] strings: new command Sam Ravnborg
  2016-09-19  8:38 ` Sam Ravnborg
@ 2016-09-21  8:09 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2016-09-21  8:09 UTC (permalink / raw)
  To: Sam Ravnborg; +Cc: Barebox List

On Sat, Sep 17, 2016 at 07:07:31PM +0200, Sam Ravnborg wrote:
> From 724382cf9aa173b6ced9fd213bbb369d9a5e3739 Mon Sep 17 00:00:00 2001
> From: Sam Ravnborg <sam@ravnborg.org>
> Date: Sat, 17 Sep 2016 19:01:15 +0200
> Subject: [PATCH 1/1] strings: new command
> 
> Implement a simple version of strings that will print
> printable strings from one or more files.
> Strings shall be at least 4 chars before thay are printed
> 
> Signed-off-by: Sam Ravnborg <sam@ravnborg.org>
> ---
> 
> I was in a situation where I missed "strings".
> So here it is....
> I started out with cat.c - but ended up re-writing everything.
> 
> It has seen light testing on my x86 box.
> Ran it through checkpatch for good measure - fixed a few things.
> 
> The location in Makefile seems random, so I just put it next to cat.
> 
> Before applying please consider if strings is really
> general useful in barebox.

The first thing I tried is "strings /dev/ram0" Which gave me quite a
long list, especially when Linux was bootet before ;)

I first thought this command might be useful for debugging, but probably
it isn't because usually one wants to pipe the result through grep to
limit the output. Also the next interesting thing to know is the
position of a string which this command does not tell us.

I won't apply it, but it's on the list for reference if someone wants to
use it. Maybe he/she can then tell us a good reason why this command should
be merged.

Sascha

-- 
Pengutronix e.K.                           |                             |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
Peiner Str. 6-8, 31137 Hildesheim, Germany | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

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

end of thread, other threads:[~2016-09-21  8:09 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-17 17:07 [PATCH] strings: new command Sam Ravnborg
2016-09-19  8:38 ` Sam Ravnborg
2016-09-21  8:09 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox