From 0c93f6c7ab7aab56b39793a2317a9bff16b8ae04 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 29 Apr 2007 19:55:21 -0400 Subject: Add readlink, xreadlink(), and change xrealloc() to not fight the stupid compiler so much. --- lib/lib.c | 32 +++++++++++++++++++++++++++++--- lib/lib.h | 3 ++- toys/Config.in | 16 ++++++++++++++++ toys/toylist.h | 1 + toys/toysh.c | 2 +- 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/lib/lib.c b/lib/lib.c index 6c8abb03..1f37eb59 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -94,10 +94,12 @@ void *xzalloc(size_t size) // Die unless we can change the size of an existing allocation, possibly // moving it. (Notice different arguments from libc function.) -void xrealloc(void **ptr, size_t size) +void *xrealloc(void *ptr, size_t size) { - *ptr = realloc(*ptr, size); - if (!*ptr) error_exit("xrealloc"); + ptr = realloc(ptr, size); + if (!ptr) error_exit("xrealloc"); + + return ptr; } // Die unless we can allocate a copy of this many bytes of string. @@ -452,6 +454,30 @@ off_t fdlength(int fd) return pos + 1; } +// This can return null (meaning file not found). It just won't return null +// for memory allocation reasons. +char *xreadlink(char *name) +{ + int len, size = 0; + char *buf = 0; + + // Grow by 64 byte chunks until it's big enough. + for(;;) { + size +=64; + buf = xrealloc(buf, size); + len = readlink(name, buf, size); + + if (len<0) { + free(buf); + return 0; + } + if (lenargc & 7)) - xrealloc((void **)cmd, + *cmd=xrealloc(*cmd, sizeof(struct command) + ((*cmd)->argc+8)*sizeof(char *)); (*cmd)->argv[(*cmd)->argc] = 0; return end; -- cgit v1.2.3