From 46409d50e5632b28b88cfa4991fffef9adaa490d Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 15 Jun 2016 15:47:01 -0500 Subject: Add readlink0() and readlinkat0() which null terminate the data. --- lib/lib.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'lib/lib.c') diff --git a/lib/lib.c b/lib/lib.c index 66814a45..b742bd17 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -444,7 +444,8 @@ off_t fdlength(int fd) // Read contents of file as a single nul-terminated string. // measure file size if !len, allocate buffer if !buf -// note: for existing buffers use len = size-1, will set buf[len] = 0 +// Existing buffers need len in *plen +// Returns amount of data read in *plen char *readfileat(int dirfd, char *name, char *ibuf, off_t *plen) { off_t len, rlen; @@ -1130,3 +1131,20 @@ struct group *bufgetgrgid(gid_t gid) return &list->gr; } + +// Always null terminates, returns 0 for failure, len for success +int readlinkat0(int dirfd, char *path, char *buf, int len) +{ + if (!len) return 0; + + len = readlinkat(dirfd, path, buf, len-1); + if (len<1) return 0; + buf[len] = 0; + + return len; +} + +int readlink0(char *path, char *buf, int len) +{ + return readlinkat0(AT_FDCWD, path, buf, len); +} -- cgit v1.2.3