aboutsummaryrefslogtreecommitdiff
path: root/lib/lib.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2015-11-26 21:16:12 -0600
committerRob Landley <rob@landley.net>2015-11-26 21:16:12 -0600
commit12a487b61f809aa4794578e14efe32d717f22bdb (patch)
treee8a824d4d3c7078f98a2a25c204ac4bdc4d0eade /lib/lib.c
parent0f3d8ee513d63282e72df37b97e35ff1a4e4d646 (diff)
downloadtoybox-12a487b61f809aa4794578e14efe32d717f22bdb.tar.gz
Split do_ps() into get_ps() and show_ps() as a start on implementing --sort.
Change readfileat() to pass back length of read.
Diffstat (limited to 'lib/lib.c')
-rw-r--r--lib/lib.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/lib/lib.c b/lib/lib.c
index b3703052..a16439e7 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -411,14 +411,16 @@ off_t fdlength(int fd)
}
// Read contents of file as a single nul-terminated string.
-// malloc new one if buf=len=0
-char *readfileat(int dirfd, char *name, char *ibuf, off_t len)
+// measure file size if !len, allocate buffer if !buf
+// note: for existing buffers use len = size-1, will set buf[len] = 0
+char *readfileat(int dirfd, char *name, char *ibuf, off_t *plen)
{
+ off_t len = *plen-!!ibuf;
int fd;
char *buf;
if (-1 == (fd = openat(dirfd, name, O_RDONLY))) return 0;
- if (len<1) {
+ if (!len) {
len = fdlength(fd);
// proc files don't report a length, so try 1 page minimum.
if (len<4096) len = 4096;
@@ -426,11 +428,11 @@ char *readfileat(int dirfd, char *name, char *ibuf, off_t len)
if (!ibuf) buf = xmalloc(len+1);
else buf = ibuf;
- len = readall(fd, buf, len-1);
+ *plen = len = readall(fd, buf, len);
close(fd);
if (len<0) {
if (ibuf != buf) free(buf);
- buf = 0;
+ buf = 0;
} else buf[len] = 0;
return buf;
@@ -438,7 +440,7 @@ char *readfileat(int dirfd, char *name, char *ibuf, off_t len)
char *readfile(char *name, char *ibuf, off_t len)
{
- return readfileat(AT_FDCWD, name, ibuf, len);
+ return readfileat(AT_FDCWD, name, ibuf, &len);
}
// Sleep for this many thousandths of a second