mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 1/4] add function to read single line files
Date: Mon, 30 Sep 2013 11:43:05 +0200	[thread overview]
Message-ID: <1380534188-1734-2-git-send-email-s.hauer@pengutronix.de> (raw)
In-Reply-To: <1380534188-1734-1-git-send-email-s.hauer@pengutronix.de>

Often small files are used to store the value od a variable. This
adds a function to easily read such a variable.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 include/libbb.h |  2 ++
 lib/libbb.c     | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/include/libbb.h b/include/libbb.h
index 47b2e08..2fe710c 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -35,4 +35,6 @@ char *simple_itoa(unsigned int i);
 int write_full(int fd, void *buf, size_t size);
 int read_full(int fd, void *buf, size_t size);
 
+char *read_file_line(const char *fmt, ...);
+
 #endif /* __LIBBB_H */
diff --git a/lib/libbb.c b/lib/libbb.c
index e0d7481..6a083f9 100644
--- a/lib/libbb.c
+++ b/lib/libbb.c
@@ -176,3 +176,45 @@ int read_full(int fd, void *buf, size_t size)
 	return insize;
 }
 EXPORT_SYMBOL(read_full);
+
+/*
+ * read_file_line - read a line from a file
+ *
+ * Used to compose a filename from a printf format and to read a line from this
+ * file. All leading and trailing whitespaces (including line endings) are
+ * removed. The returned buffer must be freed with free(). This function is
+ * supposed for reading variable like content into a buffer, so files > 1024
+ * bytes are ignored.
+ */
+char *read_file_line(const char *fmt, ...)
+{
+	va_list args;
+	char *filename;
+	char *buf, *line = NULL;
+	int size, ret;
+	struct stat s;
+
+	va_start(args, fmt);
+	filename = vasprintf(fmt, args);
+	va_end(args);
+
+	ret = stat(filename, &s);
+	if (ret)
+		goto out;
+
+	if (s.st_size > 1024)
+		goto out;
+
+	buf = read_file(filename, &size);
+	if (!buf)
+		goto out;
+
+	line = strim(buf);
+
+	line = xstrdup(line);
+	free(buf);
+out:
+	free(filename);
+	return line;
+}
+EXPORT_SYMBOL_GPL(read_file_line);
-- 
1.8.4.rc3


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

  reply	other threads:[~2013-09-30  9:43 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-30  9:43 bootloader specification for barebox Sascha Hauer
2013-09-30  9:43 ` Sascha Hauer [this message]
2013-09-30  9:43 ` [PATCH 2/4] cdev: store dos partition type in struct cdev Sascha Hauer
2013-09-30  9:43 ` [PATCH 3/4] Implement bootloader spec support for barebox Sascha Hauer
2013-09-30  9:43 ` [PATCH 4/4] add kernel-install tool for bootloader Spec Sascha Hauer
2013-10-14  9:38 ` bootloader specification for barebox Jan Lübbe
2013-10-14  9:43   ` Robert P. J. Day
2013-10-14 10:05     ` Jan Lübbe
2013-10-14 10:07       ` Robert P. J. Day

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1380534188-1734-2-git-send-email-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox