[PATCH 2/3] platform: provide getline and getdelim if not available
Timo Teräs
timo.teras at iki.fi
Sat Jun 18 15:56:47 UTC 2011
Simple implementations. Need to check which platforms need the
wrapper.
Signed-off-by: Timo Teräs <timo.teras at iki.fi>
---
include/platform.h | 6 ++++++
libbb/platform.c | 36 ++++++++++++++++++++++++++++++++++++
2 files changed, 42 insertions(+), 0 deletions(-)
diff --git a/include/platform.h b/include/platform.h
index 429220b..be365d2 100644
--- a/include/platform.h
+++ b/include/platform.h
@@ -349,6 +349,7 @@ typedef unsigned smalluint;
#define HAVE_STRSEP 1
#define HAVE_STRSIGNAL 1
#define HAVE_VASPRINTF 1
+#define HAVE_GETDELIM 1
#define HAVE_XTABS 1
#define HAVE_MNTENT_H 1
#define HAVE_NET_ETHERNET_H 1
@@ -455,4 +456,9 @@ extern char *strsep(char **stringp, const char *delim) FAST_FUNC;
extern int vasprintf(char **string_ptr, const char *format, va_list p) FAST_FUNC;
#endif
+#ifndef HAVE_GETDELIM
+extern ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream) FAST_FUNC;
+extern ssize_t getline(char **lineptr, size_t *n, FILE *stream) FAST_FUNC;
+#endif
+
#endif
diff --git a/libbb/platform.c b/libbb/platform.c
index 04b8961..6811ba5 100644
--- a/libbb/platform.c
+++ b/libbb/platform.c
@@ -145,3 +145,39 @@ char* FAST_FUNC stpcpy(char *p, const char *to_add)
return p;
}
#endif
+
+#ifndef HAVE_GETDELIM
+ssize_t FAST_FUNC getdelim(char **lineptr, size_t *n, int delim, FILE *stream)
+{
+ char *line = *lineptr;
+ ssize_t len = 0, alloced = *n;
+ int ch;
+
+ do {
+ ch = fgetc(stream);
+ if (ch == EOF) {
+ if (len)
+ break;
+ return -1;
+ }
+ if (len + 1 > alloced) {
+ alloced = alloced ? alloced * 2 : 120;
+ line = realloc(line, alloced);
+ if (line == NULL)
+ return -1;
+ }
+ line[len++] = ch;
+ } while (ch != delim);
+
+ line[len] = 0;
+ *lineptr = line;
+ *n = alloced;
+ return len;
+}
+
+ssize_t FAST_FUNC getline(char **lineptr, size_t *n, FILE *stream)
+{
+ /* FIXME: could optimize with fgets() */
+ return getdelim(lineptr, n, '\n', stream);
+}
+#endif
--
1.7.1
More information about the busybox
mailing list