diff options
author | Rob Landley <rob@landley.net> | 2007-04-29 19:55:21 -0400 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2007-04-29 19:55:21 -0400 |
commit | 0c93f6c7ab7aab56b39793a2317a9bff16b8ae04 (patch) | |
tree | 275dcaf29f05b6f15cf87327746d6e3c3d07e5ac /lib | |
parent | c92fde0bc75ade9d06c0d843c4693b9e2e338938 (diff) | |
download | toybox-0c93f6c7ab7aab56b39793a2317a9bff16b8ae04.tar.gz |
Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
compiler so much.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/lib.c | 32 | ||||
-rw-r--r-- | lib/lib.h | 3 |
2 files changed, 31 insertions, 4 deletions
@@ -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 (len<size) { + buf[len]=0; + return buf; + } + } +} + /* This might be of use or might not. Unknown yet... @@ -40,7 +40,7 @@ void usage_exit(void); void strlcpy(char *dest, char *src, size_t size); void *xmalloc(size_t size); void *xzalloc(size_t size); -void xrealloc(void **ptr, size_t size); +void *xrealloc(void *ptr, size_t size); void *xstrndup(char *s, size_t n); void *xstrdup(char *s); char *xmsprintf(char *format, ...); @@ -67,6 +67,7 @@ char *utoa(unsigned n); char *itoa(int n); long atolx(char *c); off_t fdlength(int fd); +char *xreadlink(char *name); struct dirtree *read_dirtree_node(char *path); struct dirtree *read_dirtree(char *path, struct dirtree *parent); |