[Toybox] getline(), getdelim() and clearenv() compat (was: Re: [PATCH] toybox compiled on OS X 10.6)

Georgi Chorbadzhiyski gf at unixsol.org
Tue Mar 13 03:16:32 PDT 2012


Around 03/13/2012 11:03 AM, Georgi Chorbadzhiyski scribbled:
> Around 03/13/2012 09:59 AM, Georgi Chorbadzhiyski scribbled:
>> On 13.3.2012 г. 07:10, Rob Landley wrote:
>>> 0006 if you could investigate why posix-2008 functions don't work on
>>> macosx that would be nice. It seems like they _should_, I'd like to
>>> understand why they don't before trying to replace them.
>>
>> I'll look into that today.
> 
> Neither getline(), nor getdelim() exist on Android and OS X. I have
> checked several free software projects and they all provide compat
> version of these two functions.

I have implemented getline(), getdelim() /and clearenv()/ and compile
them only when needed. With these compat functions toysh and xargs can
be compiled unchanged on Android and OS X /I have update android and
osx2 branches @github/.

I'm testing for __APPLE__ and __ANDROID__ and it seems to work.

Android will need compat implementation of forkpty() and definitions
of sethostname(), swapoff() and swapon().

-- 
Georgi Chorbadzhiyski
http://georgi.unixsol.org/
-------------- next part --------------
From fe0e19f337f206f657d7198dd08b278887a27cfd Mon Sep 17 00:00:00 2001
From: Georgi Chorbadzhiyski <gf at unixsol.org>
Date: Tue, 13 Mar 2012 11:57:30 +0200
Subject: [PATCH 1/3] Add getdelim() and getline() portability functions.

---
 lib/lib.c |   67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/lib.h |    9 ++++++++
 2 files changed, 76 insertions(+), 0 deletions(-)

diff --git a/lib/lib.c b/lib/lib.c
index a9827dc..400c4f6 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -900,3 +900,70 @@ char *num_to_sig(int sig)
 		if (signames[i].num == sig) return signames[i].name;
 	return NULL;
 }
+
+#if defined(__APPLE__) || defined(__ANDROID__)
+ssize_t getdelim(char **linep, size_t *np, int delim, FILE *stream)
+{
+	int ch;
+	size_t new_len;
+	ssize_t i = 0;
+	char *line, *new_line;
+
+	// Invalid input
+	if (!linep || !np) {
+		errno = EINVAL;
+		return -1;
+	}
+
+	if (*linep == NULL || *np == 0) {
+		*np = 1024;
+		*linep = calloc(1, *np);
+		if (*linep == NULL)
+			return -1;
+	}
+	line = *linep;
+
+	while ((ch = getc(stream)) != EOF) {
+		if (i > *np) {
+			// Need more space
+			new_len  = *np + 1024;
+			new_line = realloc(*linep, new_len);
+			if (!new_line)
+				return -1;
+			*np    = new_len;
+			*linep = new_line;
+		}
+
+		line[i] = ch;
+		if (ch == delim)
+			break;
+		i += 1;
+	}
+
+	if (i > *np) {
+		// Need more space
+		new_len  = i + 2;
+		new_line = realloc(*linep, new_len);
+		if (!new_line)
+			return -1;
+		*np    = new_len;
+		*linep = new_line;
+	}
+	line[i + 1] = '\0';
+
+	return i > 0 ? i : -1;
+}
+
+ssize_t getline(char **linep, size_t *np, FILE *stream) {
+	return getdelim(linep, np, '\n', stream);
+}
+#endif
+
+#if defined(__APPLE__)
+extern char **environ;
+
+int clearenv(void) {
+	*environ = NULL;
+	return 0;
+}
+#endif
diff --git a/lib/lib.h b/lib/lib.h
index ab10eb7..4b1beed 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -131,3 +131,12 @@ void bunzipStream(int src_fd, int dst_fd);
 
 int sig_to_num(char *pidstr);
 char *num_to_sig(int sig);
+
+#if defined(__APPLE__) || defined(__ANDROID__)
+ssize_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
+ssize_t getline(char **lineptr, size_t *n, FILE *stream);
+#endif
+
+#if defined(__APPLE__)
+int clearenv(void);
+#endif
-- 
1.7.5.1



More information about the Toybox mailing list