Mercurial > hg > toybox
changeset 115:19b5567f0a1b
Add readlink, xreadlink(), and change xrealloc() to not fight the stupid
compiler so much.
author | Rob Landley <rob@landley.net> |
---|---|
date | Sun, 29 Apr 2007 19:55:21 -0400 |
parents | ce6956dfc0cf |
children | a0678c2ae9b8 |
files | lib/lib.c lib/lib.h toys/Config.in toys/toylist.h toys/toysh.c |
diffstat | 5 files changed, 49 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/lib/lib.c Mon Apr 23 15:45:55 2007 -0400 +++ b/lib/lib.c Sun Apr 29 19:55:21 2007 -0400 @@ -94,10 +94,12 @@ // 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 @@ 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...
--- a/lib/lib.h Mon Apr 23 15:45:55 2007 -0400 +++ b/lib/lib.h Sun Apr 29 19:55:21 2007 -0400 @@ -40,7 +40,7 @@ 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 *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);
--- a/toys/Config.in Mon Apr 23 15:45:55 2007 -0400 +++ b/toys/Config.in Sun Apr 29 19:55:21 2007 -0400 @@ -194,6 +194,22 @@ The print working directory command prints the current directory. +config READLINK + bool "readlink" + default n + help + usage: readlink + + Show what a symbolic link points to. + +config READLINK_FINAL + bool "readlink -f" + default n + help + usage: readlink [-f] + + -f Show final location, including normal files and multiple symlinks. + config SYNC bool "sync" default n
--- a/toys/toylist.h Mon Apr 23 15:45:55 2007 -0400 +++ b/toys/toylist.h Sun Apr 29 19:55:21 2007 -0400 @@ -94,6 +94,7 @@ USE_MKE2FS(NEWTOY(mke2fs, MKE2FS_OPTSTRING, TOYFLAG_SBIN)) USE_ONEIT(NEWTOY(oneit, "+<1p", TOYFLAG_SBIN)) USE_PWD(NEWTOY(pwd, NULL, TOYFLAG_BIN)) +USE_READLINK(NEWTOY(readlink, "<1f", TOYFLAG_BIN)) USE_TOYSH(OLDTOY(sh, toysh, "c:i", TOYFLAG_BIN)) USE_SYNC(NEWTOY(sync, NULL, TOYFLAG_BIN)) USE_TOUCH(NEWTOY(touch, "l#t:r:mca", TOYFLAG_BIN))
--- a/toys/toysh.c Mon Apr 23 15:45:55 2007 -0400 +++ b/toys/toysh.c Sun Apr 29 19:55:21 2007 -0400 @@ -61,7 +61,7 @@ // Allocate more space if there's no room for NULL terminator. if (!((*cmd)->argc & 7)) - xrealloc((void **)cmd, + *cmd=xrealloc(*cmd, sizeof(struct command) + ((*cmd)->argc+8)*sizeof(char *)); (*cmd)->argv[(*cmd)->argc] = 0; return end;